0
0
Cprogramming~20 mins

Array traversal in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A10 20 30 40 50
B50 40 30 20 10
C10 20 30 40 0
DCompilation error
Attempts:
2 left
💡 Hint
Remember that pointer arithmetic moves through the array elements in order.
Predict Output
intermediate
2: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;
}
A1 2 3
B3 2 1
C0 0 0
DRuntime error
Attempts:
2 left
💡 Hint
Pointer p points to the first element, so p[i] accesses elements by index.
Predict Output
advanced
2: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;
}
A1 4 2 5 3 6
B1 2 3 4 5 6
C6 5 4 3 2 1
DCompilation error
Attempts:
2 left
💡 Hint
The outer loop goes through rows, inner loop through columns.
Predict Output
advanced
2: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;
}
A7 7 7
B9 8 7
C7 8 9
DCompilation error
Attempts:
2 left
💡 Hint
The *p++ means print value at p, then move p to next element.
Predict Output
expert
2: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;
}
A14
BRuntime error
C20
D18
Attempts:
2 left
💡 Hint
Pointer p starts at arr[1], sum adds three elements from there.