Challenge - 5 Problems
Array Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of partially initialized array
What is the output of the following C++ code snippet?
DSA C++
int arr[5] = {1, 2}; for(int i = 0; i < 5; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Uninitialized elements in a partially initialized array are set to zero.
✗ Incorrect
In C++, when an array is partially initialized, the remaining elements are set to zero.
Here, arr[0] = 1, arr[1] = 2, and arr[2], arr[3], arr[4] are zero.
❓ Predict Output
intermediateOutput of array initialization with size deduction
What is the output of this C++ code snippet using array size deduction?
DSA C++
int arr[] = {5, 10, 15}; for(int i = 0; i < 3; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Array size is automatically set to the number of initializer elements.
✗ Incorrect
The array size is deduced to 3 because of the three initializer values.
All elements are initialized as given.
❓ Predict Output
advancedOutput of multidimensional array initialization
What is the output of this C++ code snippet with a 2D array partially initialized?
DSA C++
int arr[2][3] = {{1, 2}, {3}}; for(int i = 0; i < 2; i++) { for(int j = 0; j < 3; j++) { std::cout << arr[i][j] << " "; } }
Attempts:
2 left
💡 Hint
Uninitialized elements in multidimensional arrays are zero.
✗ Incorrect
In the first row, elements are 1, 2, and the last is zero (not initialized).
In the second row, first element is 3, others are zero.
🔧 Debug
advancedIdentify the error in array initialization
What error does the following C++ code produce?
DSA C++
int arr[3] = {1, 2, 3, 4};
Attempts:
2 left
💡 Hint
Array size is fixed and cannot have more initializers than its size.
✗ Incorrect
The array size is 3 but 4 values are given, causing a compilation error.
🧠 Conceptual
expertNumber of elements in an array after initialization
Given the declaration
int arr[] = {7, 8, 9, 10};, how many elements does arr contain?Attempts:
2 left
💡 Hint
Array size is deduced from the number of initializer elements.
✗ Incorrect
The array size is automatically set to 4 because there are 4 initializer values.
