Challenge - 5 Problems
Array Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of array traversal with pointer arithmetic
What is the output of this C program?
C
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; int *p = arr; for (int i = 0; i < 5; i++) { printf("%d ", *(p + i)); } return 0; }
Attempts:
2 left
💡 Hint
Remember that pointer arithmetic moves through the array elements in order.
✗ Incorrect
The pointer p points to the first element of the array. Adding i moves it to the i-th element. So *(p + i) accesses each element in order, printing 10 20 30 40 50.
❓ Predict Output
intermediate2:00remaining
Output of array traversal with index and pointer
What will be printed by this C code?
C
#include <stdio.h> int main() { int arr[3] = {1, 2, 3}; int *p = arr; for (int i = 0; i < 3; i++) { printf("%d ", p[i]); } return 0; }
Attempts:
2 left
💡 Hint
Pointer p points to the first element, so p[i] accesses elements by index.
✗ Incorrect
Using p[i] is equivalent to *(p + i), so it prints the array elements in order: 1 2 3.
❓ Predict Output
advanced2:00remaining
Output of nested array traversal
What is the output of this C program?
C
#include <stdio.h> int main() { int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { printf("%d ", matrix[i][j]); } } return 0; }
Attempts:
2 left
💡 Hint
The outer loop goes through rows, inner loop through columns.
✗ Incorrect
The code prints each element row by row: first row 1 2 3, then second row 4 5 6.
❓ Predict Output
advanced2:00remaining
Output of pointer traversal with increment
What does this C program print?
C
#include <stdio.h> int main() { int arr[] = {7, 8, 9}; int *p = arr; for (int i = 0; i < 3; i++) { printf("%d ", *p++); } return 0; }
Attempts:
2 left
💡 Hint
The *p++ means print value at p, then move p to next element.
✗ Incorrect
The pointer p starts at arr[0]. Each loop prints *p then increments p, so prints 7 8 9.
❓ Predict Output
expert2:00remaining
Value of variable after complex array traversal
What is the value of variable sum after running this C code?
C
#include <stdio.h> int main() { int arr[4] = {2, 4, 6, 8}; int *p = arr + 1; int sum = 0; for (int i = 0; i < 3; i++) { sum += *(p++); } printf("%d", sum); return 0; }
Attempts:
2 left
💡 Hint
Pointer p starts at arr[1], sum adds three elements from there.
✗ Incorrect
p points to arr[1] = 4, then 6, then 8. Sum = 4 + 6 + 8 = 18.