0
0
C++programming~20 mins

Array initialization in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A1 2 0 1 2
B1 2 3 4 5
C1 2 0 0 1
D1 2 0 0 0
Attempts:
2 left
💡 Hint
Uninitialized elements in an array initialized with fewer values are set to zero.
Predict Output
intermediate
2: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;
}
A0 0 0 0
BGarbage values
C1 1 1 1
DCompilation error
Attempts:
2 left
💡 Hint
Empty braces initialize all elements to zero.
Predict Output
advanced
2: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;
}
ACompilation error
Bbanana
Capple
Dcherry
Attempts:
2 left
💡 Hint
Array holds pointers to string literals; arr[1] is the second element.
Predict Output
advanced
2: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;
}
A1 2 0 3 0 1
B1 2 3 3 0 0
C1 2 0 3 0 0
D1 2 0 3 1 0
Attempts:
2 left
💡 Hint
Uninitialized elements in nested arrays are zero.
🧠 Conceptual
expert
3: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;
}
A
5 0 0 
6 1 1 
B
5 0 0 
5 0 0 
C
5 0 0 
6 0 0 
D
5 0 0 
6 1 0 
Attempts:
2 left
💡 Hint
Static arrays keep their values between function calls.