C++ Arrays MCQ Quiz

Test your knowledge of C++ arrays with this interactive quiz. Select the correct answer for each question and see immediate feedback.

Medium Level Questions Medium

1

How do you declare an array of 10 integers in C++?

Correct Answer: A) int array[10];

In C++, arrays are declared with the syntax: type name[size];. So "int array[10];" declares an array of 10 integers.

2

What is the index of the first element in a C++ array?

Correct Answer: A) 0

In C++ (and most programming languages), array indices start at 0, so the first element is at index 0.

3

How do you initialize an array with values 1, 2, 3 in C++?

Correct Answer: A) int arr[3] = {1, 2, 3};

Arrays in C++ are initialized using curly braces {}. The correct syntax is: type name[size] = {values};.

4

What is the output of this code?
int arr[5] = {1, 2}; cout << arr[3];

Correct Answer: A) 0

When an array is partially initialized, the remaining elements are set to 0. So arr[3] will be 0.

5

How do you access the third element in an array called 'numbers'?

Correct Answer: A) numbers[2]

Since array indices start at 0, the third element is at index 2 (index 0 = first, index 1 = second, index 2 = third).

6

What is the size of int arr[10] in bytes? (Assume int is 4 bytes)

Correct Answer: B) 40

The array has 10 elements, each of size 4 bytes (assuming int is 4 bytes), so total size is 10 * 4 = 40 bytes.

7

Which of these correctly declares a multidimensional array?

Correct Answer: A) int arr[3][4];

Multidimensional arrays in C++ are declared with multiple sets of brackets, e.g., type name[size1][size2]...[sizeN].

8

What does the following code do?
int arr[5] = {1, 2, 3, 4, 5}; cout << sizeof(arr) / sizeof(arr[0]);

Correct Answer: B) Prints the number of elements in the array

sizeof(arr) gives the total size in bytes, sizeof(arr[0]) gives the size of one element. Dividing them gives the number of elements.

9

Which statement about arrays in C++ is true?

Correct Answer: A) Array size must be known at compile time

In standard C++, array sizes must be constant expressions known at compile time. Dynamic arrays require pointers and dynamic memory allocation.

10

What is the output of this code?
int arr[] = {1, 2, 3}; cout << arr[3];

Correct Answer: C) Garbage value

Accessing arr[3] is out of bounds since the array has indices 0, 1, and 2 only. This results in undefined behavior, typically printing a garbage value.

Advanced Level Questions Advanced

11

What is the relationship between arrays and pointers in C++?

Correct Answer: A) Arrays decay to pointers in most expressions

In most contexts, array names decay to pointers to their first element. This is why arrays are effectively passed by reference when passed to functions.

12

What is the output of this code?
int arr[2][3] = {{1,2,3},{4,5,6}}; cout << *(*(arr+1)+2);

Correct Answer: D) 6

arr+1 points to the second row {4,5,6}, *(arr+1) is the second row, *(arr+1)+2 points to the third element of the second row, and *(*(arr+1)+2) dereferences to get the value 6.

13

Which C++11 feature provides a safer alternative to C-style arrays?

Correct Answer: A) std::array

std::array (from C++11) provides a fixed-size array container that is safer than C-style arrays, with bounds checking via at() and knowledge of its own size.

14

What is the time complexity of accessing an element in an array by index?

Correct Answer: A) O(1)

Array access by index is constant time O(1) because the address of any element can be calculated directly using the base address and index.

15

What does this code do?
int* arr = new int[5];

Correct Answer: B) Dynamically allocates an array of 5 integers on the heap

The new operator dynamically allocates memory on the heap. "new int[5]" allocates an array of 5 integers and returns a pointer to the first element.