0
0
C++programming~20 mins

Array size and bounds in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Bounds Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of accessing array within bounds
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    std::cout << arr[3] << std::endl;
    return 0;
}
A50
B40
C30
DCompilation error
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0.
Predict Output
intermediate
2:00remaining
Output when accessing array out of bounds
What happens when this C++ code runs?
C++
#include <iostream>
int main() {
    int arr[3] = {1, 2, 3};
    std::cout << arr[5] << std::endl;
    return 0;
}
ACompilation error
B3
CUndefined behavior, may print garbage value
DRuntime error: out of bounds exception
Attempts:
2 left
💡 Hint
C++ does not check array bounds at runtime.
Predict Output
advanced
2:00remaining
Size of array using sizeof operator
What is the output of this C++ program?
C++
#include <iostream>
int main() {
    int arr[10];
    std::cout << sizeof(arr) / sizeof(arr[0]) << std::endl;
    return 0;
}
A10
B40
C4
DCompilation error
Attempts:
2 left
💡 Hint
sizeof(arr) gives total bytes, sizeof(arr[0]) gives bytes per element.
Predict Output
advanced
2:00remaining
Behavior of pointer arithmetic on arrays
What does this C++ code print?
C++
#include <iostream>
int main() {
    int arr[4] = {5, 10, 15, 20};
    int* p = arr;
    std::cout << *(p + 2) << std::endl;
    return 0;
}
A5
B10
CCompilation error
D15
Attempts:
2 left
💡 Hint
Pointer arithmetic moves by element size.
🧠 Conceptual
expert
2:00remaining
Why does accessing out-of-bounds array cause undefined behavior?
Which statement best explains why accessing an array out of its declared bounds in C++ leads to undefined behavior?
ABecause C++ does not perform automatic bounds checking, so accessing invalid memory can cause unpredictable results.
BBecause the compiler automatically resizes the array to fit the access.
CBecause the operating system blocks any out-of-bounds access and throws an exception.
DBecause arrays in C++ are dynamically sized and always safe to access.
Attempts:
2 left
💡 Hint
Think about what happens when you access memory not allocated for the array.