1) What is the correct output of given code snippets?
int [,,]arr = new int[2,2,3];
Console.WriteLine(arr.Length);
- 7
- 12
- 18
- 8
Correct answer: 2
12
In the above code snippet, we are creating a 3-Dimensional array, then the length of the array will be 2 x 2 x 3 = 12
3) What is the correct way to find total number of elements in given array?
int []arr = {1,2,3,4,5};
- arr.TotalItems();
- arr.ItemsCount();
- arr.Length;
- arr.Count;
Correct answer: 3
arr.Length;
The Length property is used to get the total number of elements in an array.
4) If we do not assign values in an integer array, what will be the default integer array elements?
- 1
- ' ' (Space)
- 0
- -1
Correct answer: 3
0
In C#.NET, 0 is the default value of array elements.
5) What is the correct output of given code snippets?
static void Main(string[] args)
{
int[] a = new int[5];
a[1] = 2;
a[3] = 4;
Console.WriteLine(a[0] + "," + a[1] + "," + a[2] + "," + a[3] + "," + a[4]);
}
- 0,2,0,4,0
- 1,2,3,4,5
- -1,2,-1,4,-1
- 0,0,0,0
Correct answer: 1
0,2,0,4,0
In the code, only index 1 and 3 are assigned with the 2 and 4, rest all elements contain the default values (0).