C# Arrays MCQ

1. How do you declare an array of integers in C#?

a) int[] myArray;
b) array<int> myArray;
c) int myArray[];
d) int array[] myArray;

Answer:

a) int[] myArray;

Explanation:

In C#, arrays are declared with the type followed by square brackets, as in int[].

2. What will be the default value of elements in a newly created int array in C#?

a) 0
b) 1
c) null
d) -1

Answer:

a) 0

Explanation:

By default, all elements in a new integer array in C# are initialized to zero.

3. How can you determine the length of an array in C#?

a) array.length
b) array.size
c) array.Length
d) array.count

Answer:

c) array.Length

Explanation:

The Length property is used to find the number of elements in an array in C#.

4. Which of these is a correct way to initialize an array in C#?

a) int[] array = new int[5]{1, 2, 3, 4, 5};
b) int[] array = new int[]{1, 2, 3, 4, 5};
c) int array[] = {1, 2, 3, 4, 5};
d) int[] array = new int[5](1, 2, 3, 4, 5);

Answer:

b) int[] array = new int[]{1, 2, 3, 4, 5};

Explanation:

This syntax initializes an array with specific values without specifying the size.

5. How do you access the third element in an array named 'data' in C#?

a) data[2];
b) data[3];
c) data(2);
d) data(3);

Answer:

a) data[2];

Explanation:

Array indices in C# start at 0, so the third element is accessed with data[2].

6. What is the output of the following C# code?

    int[] nums = {1, 2, 3};
    Console.WriteLine(nums[1]);
a) 1
b) 2
c) 3
d) Error

Answer:

b) 2

Explanation:

The index 1 corresponds to the second element in the array, which is 2.

7. How do you create a two-dimensional array in C#?

a) int[,] multiArray = new int[2,3];
b) int[][] multiArray = new int[2][3];
c) int[2][3] multiArray = new int[][];
d) int[,] multiArray = { {1, 2}, {3, 4, 5} };

Answer:

a) int[,] multiArray = new int[2,3];

Explanation:

A two-dimensional array is declared with two sets of square brackets and initialized with the 'new' keyword followed by the array dimensions.

8. What does the following code do in C#?

Array.Sort(myArray);
a) Reverses the array
b) Sorts the array in descending order
c) Sorts the array in ascending order
d) Shuffles the array randomly

Answer:

c) Sorts the array in ascending order

Explanation:

Array.Sort() sorts the elements of an array in ascending order.

9. Which method is used to reverse the elements of an array in C#?

a) Array.Reverse()
b) Array.Flip()
c) Array.Invert()
d) Reverse.Array()

Answer:

a) Array.Reverse()

Explanation:

Array.Reverse() is used to reverse the order of the elements in an array.

10. In C#, what exception is thrown if you access an array index that is out of bounds?

a) ArrayIndexOutOfBoundsException
b) IndexOutOfRangeException
c) ArgumentOutOfRangeException
d) InvalidOperationException

Answer:

b) IndexOutOfRangeException

Explanation:

Accessing an invalid array index in C# results in an IndexOutOfRangeException.

11. What is the correct way to declare a jagged array in C#?

a) int[][] jaggedArray;
b) int[,] jaggedArray;
c) int[] jaggedArray[];
d) int[][,] jaggedArray;

Answer:

a) int[][] jaggedArray;

Explanation:

A jagged array is an array of arrays, and it is declared with two sets of square brackets.

12. How do you copy an array in C#?

a) Array.Copy(sourceArray, destinationArray, length);
b) sourceArray.Copy(destinationArray, length);
c) destinationArray = sourceArray.Copy(length);
d) destinationArray = sourceArray;

Answer:

a) Array.Copy(sourceArray, destinationArray, length);

Explanation:

Array.Copy() is a static method used to copy elements from one array to another.

13. Which keyword is used to iterate over an array in C#?

a) foreach
b) for
c) in
d) iterate

Answer:

a) foreach

Explanation:

The foreach loop is commonly used for iterating over the elements of an array in C#.

14. What is the default value for elements in a boolean array in C#?

a) true
b) false
c) null
d) 0

Answer:

b) false

Explanation:

By default, all elements in a new boolean array in C# are initialized to false.

15. How do you initialize an array of strings with 3 elements in C#?

a) string[] names = new string[3]{"John", "Jane", "Doe"};
b) string[] names = new string[]{"John", "Jane", "Doe"};
c) string[] names = {"John", "Jane", "Doe"};
d) All of the above

Answer:

d) All of the above

Explanation:

All the options are valid ways to initialize an array of strings with three elements in C#.

16. How do you check if an array is empty in C#?

a) array.IsEmpty()
b) array.Length == 0
c) array.Count() == 0
d) array == null

Answer:

b) array.Length == 0

Explanation:

The Length property of the array is used to check if it is empty by comparing it to 0.

17. Which method is used to find a specific element in an array in C#?

a) Array.Find()
b) Array.Search()
c) Array.Get()
d) Array.Lookup()

Answer:

a) Array.Find()

Explanation:

Array.Find() is used to search for an element that matches a certain condition in an array.

18. What is the result of using the Clone() method on an array in C#?

a) It creates a deep copy of the array.
b) It creates a shallow copy of the array.
c) It sorts the array.
d) It reverses the array.

Answer:

b) It creates a shallow copy of the array.

Explanation:

The Clone() method creates a shallow copy of the array, meaning it copies the elements but not the objects they reference.

19. How do you declare an array with initial size but without initializing the elements in C#?

a) int[] nums = new int[5];
b) int[] nums = new int[]{5};
c) int[] nums = new int[] {0, 0, 0, 0, 0};
d) int[] nums = {5};

Answer:

a) int[] nums = new int[5];

Explanation:

This syntax declares an array with a specified size (5 in this case) without initializing the elements.

20. Which of the following is not a valid array declaration in C#?

a) int[] arr = new int[5];
b) var arr = new int[5];
c) int[] arr = new int[]{1, 2, 3, 4, 5};
d) int arr[] = new int[5];

Answer:

d) int arr[] = new int[5];

Explanation:

The correct syntax for array declaration in C# does not include the type after the array brackets.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top