0
0
C++programming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of array traversal with pointer arithmetic
What is the output of the following C++ code?
C++
#include <iostream>
int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *p = arr;
    for (int i = 0; i < 5; ++i) {
        std::cout << *(p + i) << " ";
    }
    return 0;
}
A50 40 30 20 10
B10 20 30 40 50
C10 20 30 40
DCompilation error
Attempts:
2 left
💡 Hint
Pointer arithmetic moves through the array elements in order.
Predict Output
intermediate
2:00remaining
Output of array traversal with range-based for loop
What will this C++ program print?
C++
#include <iostream>
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    for (int x : arr) {
        std::cout << x * 2 << " ";
    }
    return 0;
}
A0 2 4 6 8
B1 2 3 4 5
C2 4 6 8 10
DCompilation error
Attempts:
2 left
💡 Hint
The loop multiplies each element by 2 before printing.
Predict Output
advanced
2:00remaining
Output of nested array traversal
What is the output of this nested loop traversing a 2D array?
C++
#include <iostream>
int main() {
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            std::cout << matrix[i][j] << " ";
        }
    }
    return 0;
}
A1 2 3 4 5 6
BCompilation error
C1 4 2 5 3 6
D1 2 3 1 2 3
Attempts:
2 left
💡 Hint
The outer loop goes through rows, inner loop through columns.
Predict Output
advanced
2:00remaining
Output of array traversal with incorrect loop condition
What happens when this code runs?
C++
#include <iostream>
int main() {
    int arr[] = {5, 10, 15};
    for (int i = 0; i <= 3; ++i) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
A5 10 15 garbage_value or runtime error
B5 10 15 0
CCompilation error
D5 10 15
Attempts:
2 left
💡 Hint
The loop goes one step beyond the array size.
🧠 Conceptual
expert
2:00remaining
Identifying the number of elements traversed
Given the code below, how many elements will be printed?
C++
#include <iostream>
int main() {
    int arr[4] = {7, 14, 21, 28};
    int *ptr = arr + 1;
    for (int i = 0; i < 3; ++i) {
        std::cout << *(ptr + i) << " ";
    }
    return 0;
}
A4
B2
C1
D3
Attempts:
2 left
💡 Hint
Pointer starts at second element, loop runs 3 times.