C# Multiple Choice Questions – Array

This section of our 1000+ C# multiple choice questions focuses on array and it’s properties in C# Programming Language.

1. What will be the output of the following C# code?

  1. static void Main(string[] args)
  2. {
  3.     int i, j;
  4.     int[, ] arr = new int[ 3, 3];
  5.     for (i = 0; i < 3; ++i)
  6.     {
  7.         for (j = 0; j < 3; ++j)
  8.         {
  9.             arr[i, j] = i * 2 + i * 2;
  10.             Console.WriteLine(arr[i, j]);
  11.         }
  12.         Console.ReadLine();
  13.     }
  14. }

a) 0, 0, 0  4, 4, 4  8, 8, 8
b) 4, 4, 4  8, 8, 8   12, 12, 12
c) 8, 8, 8  12, 12, 12  16, 16, 16
d) 0, 0, 0  1, 1, 1  2, 2, 2
View Answer

Answer: a
Explanation: Since, with each value of of ‘i’ the value of ‘j’ is executed three times i.e
for i = 0, j = 0, 0, 0, i = 1, j = 2, 2, 2.
Output:

advertisement
advertisement
0, 0, 0 4, 4, 4 8, 8, 8

2. What will be the output of the following C# code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.  static void Main(string[] args)
  2.  {
  3.      char A = 'K';
  4.      char B = Convert.ToChar(76);
  5.      A++;
  6.      B++;
  7.      Console.WriteLine(A+ "  " +B);
  8.      Console.ReadLine();
  9.  }

a) M L
b) U L
c) L M
d) A B
View Answer

Answer: c
Explanation: “++” increments the value of character by 1. A and B are given values K and 76, when we use increment operator their values increments by 1, A and B becomes L and M.
Output:

advertisement
L, M.

3. Complete the following C# code with “foreach condition”.

advertisement
  1. int[][]a = new int[2][];
  2. a[0] = new int[3]{3, 4, 2};
  3. a[1] = new int[2]{8, 5};
  4. foreach( int[]i in a)
  5. {
  6. /* add for loop */
  7. console.write( j+ " ");
  8. console.writeline();
  9. }

a) foreach (int j = 1;(j(<)(a(0).GetUpperBound)); (j++));
b) foreach (int j = 1;(j(<)(a.GetUpperBound(0))); (j++));
c) foreach (int j in a.Length);
d) foreach (int j in i);
View Answer

Answer: d
Explanation: None.

4. What will be the output of the following C# code?

  1. static void Main(string[] args)
  2. {
  3.     double a = 345.09;
  4.     byte c = (byte) a;
  5.     Console.WriteLine(c);
  6.     Console.ReadLine();
  7. }

a) 98
b) 89
c) 88
d) 84
View Answer

Answer: b
Explanation: Type casting a larger variable into a smaller variable results in modules of larger variable by range of smaller variable. a is ‘345.09’ which is larger than byte range ie -128 to 127.
Output :

89

5. Which statement is correct about following c#.NET code?

int[] a= {11, 3, 5, 9, 6};

a) ‘a’ is a reference to the array created on stack
b) ‘a’ is a reference to an object created on stack
c) ‘a’ is a reference to an object of a class that compiler drives from ‘System.Array’ class
d) None of the mentioned
View Answer

Answer: c
Explanation: A perfect way of defining single array in C# which is derived automatically from class ‘System.Array’.

6. What is the advantage of using 2D jagged array over 2D rectangular array?
a) Easy initialization of elements
b) Allows unlimited elements as well as rows which had ‘0’ or are empty in nature
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: b
Explanation: In many applications where 2 dimensional arrays are used, not all rows need to have all the elements i.e they are sparse. Many rows have 0 elements. In such cases it is better to use 2D jagged arrays as they allow unequal number of elements in each row and also allow for empty rows.

7. Which statement is correct about following C# code?

int[, ]a={{5, 4, 3},{9, 2, 6}};

a)’a’ represents 1-D array of 5 integers
b) a.GetUpperBound(0) gives 9
c)’a’ represents rectangular array of 2 columns and 3 arrays
d) a.GetUpperBound(0) gives 2
View Answer

Answer: c
Explanation: By definition.

8. What will be the output of the following C# code?

  1. static void Main(string[] args)
  2. {
  3.     Program p = new Program();
  4.     p.display(2, 3, 8);
  5.     int []a = { 2, 56, 78, 66 };
  6.     Console.WriteLine("example of array");
  7.     Console.WriteLine("elements added are");
  8.     p.display(a);
  9.     Console.ReadLine();
  10. }
  11. public void display(params int[] b)
  12. {
  13.     foreach (int i in b)
  14.     {
  15.         Console.WriteLine("ARRAY IS HAVING:{0}", i);
  16.     }
  17. }

a) Compile time error
b) Run time error
c) Code runs successfully but prints nothing
d) Code runs successfully and prints given on console
View Answer

Answer: d
Explanation: Object ‘p’ makes a call to invoke function display() and hence consecutively prints the output. Array ‘a’ is declared with elements again object ‘p’ makes a call to display() and hence, consecutively prints the output with given elements.
Output:

        ARRAY IS HAVING:2
        ARRAY IS HAVING:3
        ARRAY IS HAVING:8
        elements added are:
        ARRAY IS HAVING:2
        ARRAY IS HAVING:56
        ARRAY IS HAVING:78
        ARRAY IS HAVING:66

9. Which is the correct way of defining and initializing an array of 3 integers?
a) int[] a={78, 54};
b)

   int[] a;
   a = new int[3];
   a[1] = 78;
   a[2] = 9;
   a[3] = 54;

c)

   int[] a;
   a = new int{78, 9, 54};

d)

   int[] a;
   a = new int[3]{78, 9, 54};
View Answer
Answer: d
Explanation: None.

10. Choose selective differences between an array in c# and array in other programming languages.
a) Declaring array in C# the square bracket([]) comes after the type but not after identifier
b) It is necessary to declare size of an array with its type
c) No difference between declaration of array in c# as well as in other programming languages
d) All of the mentioned
View Answer

Answer: a
Explanation: i. When declaring an array in C#, the square brackets ([]) come after the type, not the identifier. Brackets after the identifier are not legal syntax in C#.
example :

int[] IntegerArray;

ii. The size of the array is not part of its type as it is in the C language. This allows to declare an array and assign any array of int objects to it, regardless of the array’s length.

    int[] IntegerArray; 
    IntegerArray = new int[10]; 
    IntegerArray = new int[50];

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.