C++ Array MCQ

Arrays are a foundational concept in C++ programming, allowing you to store a collection of items (typically of the same type) at contiguous memory locations. As you start with C++ or look to refresh your knowledge, this quiz will test your understanding of arrays in the language. 

Try your hand at these ten multiple-choice questions, and check your answers below for explanations. Let's jump in!

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

a) int array(5);
b) int array[5];
c) int[5] array;
d) array

Answer:

b) int array[5];

Explanation:

Arrays in C++ are declared by specifying the type, followed by the array name and its size in square brackets.

2. Which index refers to the first element of an array?

a) 0
b) 1
c) -1
d) None of the above

Answer:

a) 0

Explanation:

In C++, array indices start at 0. So, the first element of an array is accessed using index 0.

3. What is the size of an array int arr[10];?

a) 10 bytes
b) 40 bytes
c) 100 bytes
d) Cannot be determined

Answer:

b) 40 bytes

Explanation:

Each int typically occupies 4 bytes. Therefore, an array of 10 integers will occupy 10 x 4 = 40 bytes.

4. How do you determine the number of elements in an array named data?

a) sizeof(data)
b) data.length
c) sizeof(data) / sizeof(data[0])
d) data.size()

Answer:

c) sizeof(data) / sizeof(data[0])

Explanation:

To get the number of elements, you divide the total size of the array by the size of a single element.

5. What will be the value of arr[2] after the following code is executed?

  int arr[5] = {10, 20};
a) 10
b) 20
c) 0
d) Undefined

Answer:

c) 0

Explanation:

If an array is initialized with fewer values than its size, the remaining elements get automatically initialized to zero.

6. Which of the following is NOT a correct way to initialize an array?

a) int arr[3] = {1, 2, 3};
b) int arr[] = {1, 2, 3};
c) int arr[3] = (1, 2, 3);
d) int arr[3] = {1, 2};

Answer:

c) int arr[3] = (1, 2, 3);

Explanation:

Arrays are initialized using curly braces { }, not parentheses ( ).

7. Which of the following will give a compilation error?

a) int arr[0];
b) int arr[-1];
c) int arr[];
d) All of the above

Answer:

d) All of the above

Explanation:

You cannot declare an array of size 0, a negative size, or without a size if it's not being initialized immediately.

8. What happens if you try to access an array element using an index that is out of bounds?

a) A compilation error occurs.
b) The program will definitely crash.
c) Undefined behavior.
d) The element will be automatically added to the array.

Answer:

c) Undefined behavior.

Explanation:

Accessing an element out of bounds does not cause a compilation error in C++, but it results in undefined behavior, which could be anything, including a crash or incorrect results.

9. Which of the following is a two-dimensional array?

a) int arr[5];
b) int arr[5][5];
c) int arr[][];
d) int[5] arr;

Answer:

b) int arr[5][5];

Explanation:

A two-dimensional array is essentially an "array of arrays". It's represented by rows and columns, as in option b.



Comments