Challenge - 5 Problems
Array Mastery
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 program?
C
#include <stdio.h> int main() { int arr[5] = {10, 20, 30, 40, 50}; printf("%d\n", arr[3]); return 0; }
Attempts:
2 left
💡 Hint
Remember 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 <stdio.h> int main() { int arr[3] = {1, 2, 3}; printf("%d\n", arr[5]); return 0; }
Attempts:
2 left
💡 Hint
Accessing outside array size is not safe in C.
✗ Incorrect
Accessing arr[5] is beyond the declared size of 3, causing undefined behavior. It may print garbage or crash.
🧠 Conceptual
advanced2:00remaining
Size of array in bytes
Given this code, what is the output of the program?
C
#include <stdio.h> int main() { int arr[10]; printf("%zu\n", sizeof(arr)); return 0; }
Attempts:
2 left
💡 Hint
sizeof returns size in bytes. int is usually 4 bytes.
✗ Incorrect
The array has 10 integers. Each int is 4 bytes on most systems. So total size is 10 * 4 = 40 bytes.
📝 Syntax
advanced2:00remaining
Which array declaration is invalid?
Which of these array declarations will cause a compilation error in C?
Attempts:
2 left
💡 Hint
Array size must match initializer elements or be omitted.
✗ Incorrect
Declaring int arr[3] with 4 initializers is invalid and causes a compile error.
🔧 Debug
expert3:00remaining
Why does this array access cause a crash?
Consider this code snippet. Why does it crash when run?
C
#include <stdio.h> int main() { int arr[2] = {5, 10}; for (int i = 0; i <= 2; i++) { printf("%d\n", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition and array size carefully.
✗ Incorrect
The array has size 2, valid indices are 0 and 1. Accessing arr[2] is out of bounds and causes a crash.