🇯🇵 日本語 | 🇺🇸 English

Lesson 21: Quiz (Arrays)

Test your understanding of array indexing, sizeof, and out-of-bounds access.

Question 1 — Array initialization

int a[5] = {10, 20, 30, 40, 50};
printf("%d\n", a[2]);

What does this print?

20
30
10
Compile error
Explanation: Array indices start at 0. a[0]=10, a[1]=20, a[2]=30.

Question 2 — Number of elements

int a[] = {3, 1, 4, 1, 5, 9};
printf("%lu\n", sizeof(a) / sizeof(a[0]));

What does this print?

4
6
24
1
Explanation: sizeof(a) is the whole array's size in bytes (24). sizeof(a[0]) is the size of one element (4).
24 / 4 = 6. This is the classic idiom for getting an array's length.

Question 3 — Out-of-bounds access

int a[3] = {10, 20, 30};
printf("%d\n", a[3]);

What happens?

0
30
Compile error
Undefined behavior (garbage value)
Explanation: a[3] is out of range (valid indices run from a[0] to a[2]).
C does not flag a compile error, but the read is undefined behavior — anything can happen, and it's a classic source of security bugs.

Question 4 — Sum with a for loop

int a[4] = {2, 4, 6, 8};
int sum = 0;
for (int i = 0; i < 4; i++) {
    sum += a[i];
}
printf("sum = %d\n", sum);

What does this print?

sum = 16
sum = 20
sum = 10
sum = 8
Explanation: 2+4+6+8 = 20. This is the standard pattern for summing every element of an array with a for loop.

Question 5 — Partial initialization

int a[5] = {1, 2};
printf("%d %d %d\n", a[0], a[2], a[4]);

What does this print?

1 2 (indeterminate)
1 0 0
Compile error
1 1 1
Explanation: When the initializer list is shorter than the array size, the remaining elements are zero-filled.
  • a[0]=1, a[1]=2 (explicitly set)
  • a[2], a[3], a[4] are automatically 0
This is also why the idiom int a[5] = {0}; zero-initializes every element.

Question 6 — Accessing a 2D array

int m[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};
printf("%d\n", m[1][2]);

What does this print?

3
5
6
2
Explanation: m[row][col] uses 0-based indexing for both dimensions.
  • m[0] = {1, 2, 3} (first row)
  • m[1] = {4, 5, 6} (second row)
  • m[1][2] = the third element of the second row = 6
It helps to picture the array as a matrix.

Question 7 — Modifying an array

int a[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
    a[i] *= 2;
}
printf("%d\n", a[3]);

What does this print?

4
8
6
10
Explanation: The loop doubles every element.
  • Original: {1, 2, 3, 4, 5}
  • After doubling: {2, 4, 6, 8, 10}
  • a[3] = 4 × 2 = 8
You can modify any array element with a[i] = value.

Question 8 — sizeof returns total bytes

// Assume int is 4 bytes
int a[10];
printf("%lu\n", sizeof(a));

What does this print?

10
40
4
8
Explanation: sizeof(array) returns the total size in bytes of the array.
  • Size of one element: sizeof(int) = 4 bytes
  • Number of elements: 10
  • Total: 4 × 10 = 40 bytes
Note: when you pass an array to a function, it "decays" to a pointer, so sizeof returns the size of a pointer instead (typically 8). If you need the element count inside the function, pass it as a separate argument.

Question 9 — Array name and pointer

int a[3] = {10, 20, 30};
printf("%d\n", *a);

What does this print?

10
30
An address value
Compile error
Explanation: Inside an expression, an array name is treated as a pointer to its first element.
  • a is equivalent to &a[0] (the address of the first element)
  • *a is equivalent to a[0] (the value of the first element)
  • So *a = 10
This relationship is also the foundation of pointer arithmetic: *(a+i) and a[i] mean the same thing.

Result

Answer all the questions to see your score.
Home