0
0
Cprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Mastery
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 program?
C
#include <stdio.h>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    printf("%d\n", arr[3]);
    return 0;
}
A40
BRuntime error
C30
D50
Attempts:
2 left
💡 Hint
Remember 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 <stdio.h>

int main() {
    int arr[3] = {1, 2, 3};
    printf("%d\n", arr[5]);
    return 0;
}
AUndefined behavior (may print garbage or crash)
BCompile-time error
C0
D3
Attempts:
2 left
💡 Hint
Accessing outside array size is not safe in C.
🧠 Conceptual
advanced
2: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;
}
A10
B40
C4
DDepends on compiler, but usually 10
Attempts:
2 left
💡 Hint
sizeof returns size in bytes. int is usually 4 bytes.
📝 Syntax
advanced
2:00remaining
Which array declaration is invalid?
Which of these array declarations will cause a compilation error in C?
Aint arr[5];
Bint arr[] = {1, 2, 3};
Cint arr[1];
Dint arr[3] = {1, 2, 3, 4};
Attempts:
2 left
💡 Hint
Array size must match initializer elements or be omitted.
🔧 Debug
expert
3: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;
}
ABecause the loop condition should be i < 3 instead of i <= 2
BBecause arr is not initialized properly
CBecause the loop accesses arr[2], which is out of bounds causing undefined behavior
DBecause printf format specifier is incorrect
Attempts:
2 left
💡 Hint
Check the loop condition and array size carefully.