C++ Arrays MCQ Quiz
Practice 15 basic and 10 tricky MCQs with instant feedback. Continue with the tutorial and interview Q&A links below.
Basic Level (15 Questions)
How do you declare an array of 10 integers in C++?
In C++, arrays are declared with the syntax: type name[size];. So "int array[10];" declares an array of 10 integers.
What is the index of the first element in a C++ array?
In C++ (and most programming languages), array indices start at 0, so the first element is at index 0.
How do you initialize an array with values 1, 2, 3 in C++?
Arrays in C++ are initialized using curly braces {}. The correct syntax is: type name[size] = {values};.
What is the output of this code?
int arr[5] = {1, 2}; cout << arr[3];
When an array is partially initialized, the remaining elements are set to 0. So arr[3] will be 0.
How do you access the third element in an array called 'numbers'?
Since array indices start at 0, the third element is at index 2 (index 0 = first, index 1 = second, index 2 = third).
What is the size of int arr[10] in bytes? (Assume int is 4 bytes)
The array has 10 elements, each of size 4 bytes (assuming int is 4 bytes), so total size is 10 * 4 = 40 bytes.
Which of these correctly declares a multidimensional array?
Multidimensional arrays in C++ are declared with multiple sets of brackets, e.g., type name[size1][size2]...[sizeN].
What does the following code do?
int arr[5] = {1, 2, 3, 4, 5}; cout << sizeof(arr) / sizeof(arr[0]);
sizeof(arr) gives the total size in bytes, sizeof(arr[0]) gives the size of one element. Dividing them gives the number of elements.
Which statement about arrays in C++ is true?
In standard C++, array sizes must be constant expressions known at compile time. Dynamic arrays require pointers and dynamic memory allocation.
What is the output of this code?
int arr[] = {1, 2, 3}; cout << arr[3];
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.
What is the relationship between arrays and pointers in C++?
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.
What is the output of this code?
int arr[2][3] = {{1,2,3},{4,5,6}}; cout << *(*(arr+1)+2);
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.
Which C++11 feature provides a safer alternative to C-style arrays?
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.
What is the time complexity of accessing an element in an array by index?
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.
What does this code do?
int* arr = new int[5];
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.
Tricky Level (10 Questions)
Array index of first element is?
C++ arrays are zero-indexed.
int a[5]; how many elements?
The size in brackets is the element count.
What does `sizeof(a)/sizeof(a[0])` give for array a?
Common idiom for number of elements in a C-style array.
Accessing a[10] on int a[5] is?
Out-of-bounds access is undefined behavior.
2D array int m[2][3] has how many ints?
2 rows × 3 columns = 6 elements.
Which passes entire 1D array to function by address?
Array parameters decay to pointers.
Quick review: pick the best C++ statement (1)
Review the tutorial and Q&A for this topic.
Quick review: pick the best C++ statement (2)
Review the tutorial and Q&A for this topic.
Quick review: pick the best C++ statement (3)
Review the tutorial and Q&A for this topic.
Quick review: pick the best C++ statement (4)
Review the tutorial and Q&A for this topic.