0
0
C++programming~20 mins

Multi-dimensional arrays in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multi-dimensional arrays Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested loop accessing 2D array
What is the output of the following C++ code?
C++
#include <iostream>
int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            std::cout << arr[i][j] << " ";
        }
    }
    return 0;
}
A1 4 2 5 3 6
B1 2 3 4 5 6
C6 5 4 3 2 1
D1 2 3 6 5 4
Attempts:
2 left
💡 Hint
Think about how the nested loops iterate rows then columns.
Predict Output
intermediate
2:00remaining
Value of element after assignment in 3D array
What is the value of arr[1][0][2] after running this code?
C++
#include <iostream>
int main() {
    int arr[2][2][3] = {{{1,2,3},{4,5,6}}, {{7,8,9},{10,11,12}}};
    arr[1][0][2] = arr[0][1][1] + arr[1][1][0];
    std::cout << arr[1][0][2];
    return 0;
}
A15
B11
C9
DError
Attempts:
2 left
💡 Hint
Check the values at arr[0][1][1] and arr[1][1][0] and add them.
🔧 Debug
advanced
2:00remaining
Identify the error in 2D array initialization
What error will this code produce?
C++
#include <iostream>
int main() {
    int arr[2][2] = {1, 2, 3, 4, 5};
    std::cout << arr[1][1];
    return 0;
}
ARuntime error: out of bounds access
BOutputs 5
COutputs 4
DCompilation error: too many initializers
Attempts:
2 left
💡 Hint
Check how many values are allowed for a 2x2 array.
Predict Output
advanced
2:00remaining
Sum of elements in jagged 2D array simulation
What is the output of this code simulating a jagged array using pointers?
C++
#include <iostream>
int main() {
    int row1[] = {1, 2};
    int row2[] = {3, 4, 5};
    int* arr[2] = {row1, row2};
    int sizes[2] = {2, 3};
    int sum = 0;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < sizes[i]; j++) {
            sum += arr[i][j];
        }
    }
    std::cout << sum;
    return 0;
}
A12
B14
C15
DError
Attempts:
2 left
💡 Hint
Add all elements in both rows carefully.
🧠 Conceptual
expert
2:00remaining
Memory layout of multi-dimensional arrays
In C++, how are multi-dimensional arrays stored in memory?
ARow-major order: rows stored contiguously, then columns
BEach element stored at random memory locations
CRandom order depending on compiler
DColumn-major order: columns stored contiguously, then rows
Attempts:
2 left
💡 Hint
Think about how arrays are laid out in C++ standard.