Challenge - 5 Problems
Array Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of partially initialized array
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { int arr[5] = {1, 2}; for(int i = 0; i < 5; ++i) { std::cout << arr[i] << " "; } return 0; }
Attempts:
2 left
💡 Hint
Uninitialized elements in an array initialized with fewer values are set to zero.
✗ Incorrect
In C++, when an array is partially initialized, the remaining elements are zero-initialized automatically.
❓ Predict Output
intermediate2:00remaining
Output of array initialization with all zeros
What will be the output of this C++ code?
C++
#include <iostream> int main() { int arr[4] = {}; for(int i = 0; i < 4; ++i) { std::cout << arr[i] << " "; } return 0; }
Attempts:
2 left
💡 Hint
Empty braces initialize all elements to zero.
✗ Incorrect
Using empty braces {} initializes all elements of the array to zero in C++.
❓ Predict Output
advanced2:00remaining
Output of array initialization with string literals
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { const char* arr[3] = {"apple", "banana", "cherry"}; std::cout << arr[1] << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Array holds pointers to string literals; arr[1] is the second element.
✗ Incorrect
The array stores pointers to string literals. arr[1] points to "banana".
❓ Predict Output
advanced2:00remaining
Output of multidimensional array initialization
What is the output of this C++ code?
C++
#include <iostream> int main() { 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] << " "; } } return 0; }
Attempts:
2 left
💡 Hint
Uninitialized elements in nested arrays are zero.
✗ Incorrect
In multidimensional arrays, missing elements in inner braces are zero-initialized.
🧠 Conceptual
expert3:00remaining
Behavior of static array initialization inside a function
Consider this C++ code. What will be the output after the function is called twice?
C++
#include <iostream> void func() { static int arr[3] = {5}; for(int i = 0; i < 3; ++i) { std::cout << arr[i] << " "; arr[i] += 1; } std::cout << std::endl; } int main() { func(); func(); return 0; }
Attempts:
2 left
💡 Hint
Static arrays keep their values between function calls.
✗ Incorrect
Static arrays are initialized once. On first call, arr is {5,0,0}. After increment, arr becomes {6,1,1}. On second call, updated values print.