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
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.
Advanced Level Questions Advanced
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.