Challenge - 5 Problems
Array Bounds Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0.
✗ Incorrect
The array has elements indexed 0 to 4. arr[3] is the fourth element, which is 40.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
C++ does not check array bounds at runtime.
✗ Incorrect
Accessing arr[5] is out of bounds and causes undefined behavior. It may print garbage or crash.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
sizeof(arr) gives total bytes, sizeof(arr[0]) gives bytes per element.
✗ Incorrect
The array has 10 elements. Dividing total size by element size gives 10.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Pointer arithmetic moves by element size.
✗ Incorrect
p points to arr[0]. Adding 2 moves to arr[2], which is 15.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about what happens when you access memory not allocated for the array.
✗ Incorrect
C++ arrays do not check bounds at runtime, so accessing outside the array can read or write invalid memory, causing undefined behavior.