Arrays MCQs (Multiple Choice Questions and Answers) in C#
This article helps students to test their knowledge about arrays in C#.
ARRAYS in C# - MCQs
Q.1 Which of the following statements is correct about the C#.NET code snippet given below?
int[] intMyArr = {11, 3, 5, 9, 4};
Options:
- intMyArr is a reference to an object of System.Array Class.
- intMyArr is a reference to an object of a class that the compiler derives from the System.Array Class.
- intMyArr is a reference to an array of integers.
- intMyArr is a reference to an object created on the stack.
Answer: option 2
Q.2 Which of the following is the correct way to define and initialize an array of 4 integers?
1.
int[] a = {25, 30, 40, 5};
2.
int[] a;
a = new int[3];
a[0] = 25;
a[1] = 30;
a[2] = 40;
a[3] = 5;
3.
int[] a;
a = new int{25, 30, 40, 5};
4.
int[] a;
a = new int[4]{25, 30, 40, 5};
5.
int[] a;
a = new int[4];
a[0] = 25;
a[1] = 30;
a[2] = 40;
a[3] = 5;
Options:
- 1, 2
- 3, 4
- 1, 4, 5
- 2, 4, 5
- 2, 5
Answer: option 3
Q.3. Which of the following is the correct output of the C#.NET code snippet given below?
int[][] a = new int[2][];
a[0] = new int[4]{6, 1, 4, 3};
a[1] = new int[3]{9, 2, 7};
Console.WriteLine(a[1].GetUpperBound(0));
Options:
- 3
- 4
- 7
- 2
Answer: option 4
Q.4. Which of the following is the correct way to obtain the number of elements present in the array given below?
int[] intMyArr = {25, 30, 45, 15, 60};
1.
intMyArr.GetMax;
2.
intMyArr.Highest(0);
3.
intMyArr.GetUpperBound(0);
4.
intMyArr.Length;
5.
intMyArr.GetMaxElements(0);
Options:
- 1, 2
- 3, 4
- 3, 5
- 1, 5
Answer: option 2
Q.5.What will be the output of the C#.NET code snippet given below?
namespace ProgramConsoleApplication
{
class SampleProgram
{
static void Main(string[ ] args)
{
int i, j;
int[ , ] arr = new int[ 2, 2 ];
for(i = 0; i < 2; ++i)
{
for(j = 0; j < 2; ++j)
{
arr[i, j] = i * 17 + i * 17;
Console.Write(arr[ i, j ] + " ");
}
}
}
}
}
Options:
- 0 0 34 34
- 0 0 17 17
- 0 0 0 0
- 17 17 0 0
Answer: option 1
Q.6. Which of the following is the correct output of the C#.NET code snippet given below?
int[ , , ] a = new int[ 3, 2, 3 ];
Console.WriteLine(a.Length);
Options:
- 20
- 4
- 18
- 10
Answer: option 3
Q.7. Which of the following statements are correct about arrays used in C#.NET?
- Arrays can be rectangular or jagged.
- Rectangular arrays have similar rows stored in adjacent memory locations.
- Jagged arrays do not have access to the methods of System.Array Class.
- Rectangular arrays do not have access to the methods of System.Array Class.
- Jagged arrays have dissimilar rows stored in non-adjacent memory locations.
Options:
- 1, 2
- 1, 3, 5
- 3, 4
- 1, 2, 5
Answer: option 4
Q.8. Which of the following statements are correct about the C#.NET code snippet given below?
int[][]intMyArr = new int[2][];
intMyArr[0] = new int[4]{6, 1, 4, 3};
intMyArr[1] = new int[3]{9, 2, 7};
Options:
- intMyArr is a reference to a 2-D jagged array.
- The two rows of the jagged array intMyArr are stored in adjacent memory locations.
- intMyArr[0] refers to the zeroth 1-D array and intMyArr[1] refers to the first 1-D array.
- intMyArr refers to intMyArr[0] and intMyArr[1].
Answer: option 3
Q.9. Which of the following are the correct ways to define an array of 2 rows and 3 columns?
1.
int[ , ] a;
a = new int[2, 3]{{7, 1, 3},{2, 9, 6}};
2.
int[ , ] a;
a = new int[2, 3]{};
3.
int[ , ] a = {{7, 1, 3}, {2, 9,6 }};
4.
int[ , ] a;
a = new int[1, 2];
5.
int[ , ] a;
a = new int[1, 2]{{7, 1, 3}, {2, 9, 6}};
Options:
- 1, 2, 3
- 1, 3
- 2, 3
- 2, 4, 5
Answer: option 2
Q.10. Which of the following statements is correct about the array declaration given below?
int[][][] intMyArr = new int[2][][];
Options:
- intMyArr refers to a 2-D jagged array containing 2 rows.
- intMyArr refers to a 2-D jagged array containing 3 rows.
- intMyArr refers to a 3-D jagged array containing 2 2-D jagged arrays.
- intMyArr refers to a 3-D jagged array containing three 2-D jagged arrays.
Answer: option 3
Q.11. Which of the following statements are correct about the C#.NET code snippet given below?
int[ , ] intMyArr = {{7, 1, 3}, {2, 9, 6}};
- intMyArr represents a rectangular array of 2 rows and 3 columns.
- intMyArr.GetUpperBound(1) will yield 2.
- intMyArr.Length will yield 24.
- intMyArr represents 1-D array of 5 integers.
- intMyArr.GetUpperBound(0) will yield 2.
Options:
- 1, 2
- 2, 3
- 2, 5
- 1, 4
Answer: option 1
Q.12. Which of the following statements are correct about the C#.NET code snippet given below?
int[] a = {11, 3, 5, 9, 4};
- The array elements are created on the stack.
- Reference is created on the stack.
- The array elements are created on the heap.
- On declaring the array a new array class is created which is derived from System.Array Class.
- Whether the array elements are stored in the stack or heap depends upon the size of the array.
Options:
- 1, 2
- 2, 3, 4
- 2, 3, 5
- 4, 5
Answer: option 2
Q.13. Which one of the following statements is correct?
- Array elements can be of integer type only.
- The rank of an Array is the total number of elements it can contain.
- The length of an Array is the number of dimensions in the Array.
- The default value of numeric array elements is zero.
Answer: option 4
Q.14. If a is an array of 5 integers then which of the following is the correct way to increase its size to 10 elements?
Options:
1.
int[] a = new int[5];
int[] a = new int[10];
2.
int[] a = int[5];
int[] a = int[10];
3.
int[] a = new int[5];
a.Length = 10 ;
4.
int[] a = new int[5];
a = new int[10];
Answer: option 4
Q.15. How will you complete the foreach loop in the C#.NET code snippet given below such that it correctly prints all elements of the array a?
int[][]a = new int[2][];
a[0] = new int[4]{6, 1 ,4, 3};
a[1] = new int[3]{9, 2, 7};
foreach (int[ ] i in a)
{
/* Add loop here */
Console.Write(j + " ");
Console.WriteLine();
}
Options:
- foreach (int j = 1; j < a(0).GetUpperBound; j++)
- foreach (int j = 1; j < a.GetUpperBound (0); j++)
- foreach (int j in a.Length)
- foreach (int j in i)
Answer: option 4
Q.16. What will be the output of the following C# code?
static void Main(string[] args)
{
int i, j;
int[, ] arr = new int[ 3, 3];
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
{
arr[i, j] = i * 2 + i * 2;
Console.WriteLine(arr[i, j]);
}
Console.ReadLine();
}
}
Options:
- ) 0, 0, 0 4, 4, 4 8, 8, 8
- ) 4, 4, 4 8, 8, 8 12, 12, 12
- ) .8, 8, 8 12, 12, 12 16, 16, 16
- )0, 0, 0 1, 1, 1 2, 2, 2
Answer: option 1
Q.17. What will be the output of the following C# code?
static void Main(string[] args)
{
char A = 'K';
char B = Convert.ToChar(76);
A++;
B++;
Console.WriteLine(A+ " " +B);
Console.ReadLine();
}
Options:
- M L
- U L
- L M
- A B
Answer: option 3
Q.18. Complete the following C# code with “foreach condition”.
int[][]a = new int[2][];
a[0] = new int[3]{3, 4, 2};
a[1] = new int[2]{8, 5};
foreach( int[]i in a)
{
/* add for loop */
console.write( j+ " ");
console.writeline();
}
Options:
- foreach (int j = 1;(j(<)(a(0).GetUpperBound)); (j++));
- foreach (int j = 1;(j(<)(a.GetUpperBound(0))); (j++));
- foreach (int j in a.Length);
- foreach (int j in i);
Answer: option 4
Q.19. What will be the output of the following C# code?
static void Main(string[] args)
{
double a = 345.09;
byte c = (byte) a;
Console.WriteLine(c);
Console.ReadLine();
}
Options:
- 98
- 89
- 88
- 84
Answer: option 2
Q.20. Which statement is correct about following c# .NET code?
int[] a= {11, 3, 5, 9, 6};
Options:
- ‘a’ is a reference to the array created on the stack
- ‘a’ is a reference to an object created on the stack
- ‘a’ is a reference to an object of a class that compiler drives from ‘System.Array’ class
- None of the mentioned
Answer: option 3
Q.21. What is the advantage of using 2D jagged array over 2D rectangular array?
Options:
- Easy initialization of elements
- Allows unlimited elements as well as rows which had ‘0’ or are empty in nature
- All of the mentioned
- None of the mentioned
Answer: option 2
Q.22. Which statement is correct about following C# code?
int[, ]a={{5, 4, 3},{9, 2, 6}};
Options:
- ’a’ represents 1-D array of 5 integers
- a.GetUpperBound(0) gives 9
- ’a’ represents a rectangular array of 2 columns and 3 arrays
- a.GetUpperBound(0) gives 2
Answer: option 3
Q.23. What will be the output of the following C# code?
static void Main(string[] args)
{
Program p = new Program();
p.display(2, 3, 8);
int []a = { 2, 56, 78, 66 };
Console.WriteLine("example of array");
Console.WriteLine("elements added are");
p.display(a);
Console.ReadLine();
}
public void display(params int[] b)
{
foreach (int i in b)
{
Console.WriteLine("ARRAY IS HAVING:{0}", i);
}
}
Options:
- Compile-time error
- Run time error
- Code runs successfully but prints nothing
- Code runs successfully and prints gave on console
Answer: option 4
Q.24. Which is the correct way of defining and initializing an array of 3 integers?
Options:
1.
int[] a={78, 54};
2.
int[] a;
a = new int[3];
a[1] = 78;
a[2] = 9;
a[3] = 54;
3.
int[] a;
a = new int{78, 9, 54};
4.
int[] a;
a = new int[3]{78, 9, 54};
Answer: option 4
Q.25. Choose selective differences between an array in c# and array in other programming languages.
Options:
- Declaring array in C# the square bracket([]) comes after the type but not after the identifier
- It is necessary to declare the size of an array with its type
- No difference between a declaration of an array in c# as well as in other programming languages
- All of the mentioned
Answer: option 1