0
0
Cprogramming~20 mins

Array initialization in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of partially initialized array
What is the output of this C program?
C
#include <stdio.h>

int main() {
    int arr[5] = {1, 2};
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
A1 2 0 0 0
B1 2 0 0 1
C1 2 3 4 5
D1 2 0 1 0
Attempts:
2 left
💡 Hint
Uninitialized elements in an array initialized with fewer values are set to zero.
Predict Output
intermediate
2:00remaining
Output of string array initialization
What will this C program print?
C
#include <stdio.h>

int main() {
    char str[6] = "Hello";
    for (int i = 0; i < 6; i++) {
        printf("%d ", str[i]);
    }
    return 0;
}
A72 101 108 108 111 1
B72 101 108 108 111 0
C72 101 108 108 111 32
D72 101 108 108 111 111
Attempts:
2 left
💡 Hint
Remember that strings in C end with a special null character.
Predict Output
advanced
2:00remaining
Output of multidimensional array initialization
What is the output of this C program?
C
#include <stdio.h>

int main() {
    int matrix[2][3] = {{1, 2}, {3}};
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    return 0;
}
A
1 2 0 
3 0 3 
B
1 2 3 
3 0 0 
C
1 2 0 
3 0 0 
D
1 2 0 
3 0 1 
Attempts:
2 left
💡 Hint
Uninitialized elements in nested arrays are set to zero.
Predict Output
advanced
2:00remaining
Value of array element after initialization
What is the value of arr[3] after this code runs?
C
int arr[5] = {0, 1, 2, 3, 4};
arr[3] = arr[1] + arr[2];
A4
B1
C2
D3
Attempts:
2 left
💡 Hint
Look carefully at the assignment to arr[3].
Predict Output
expert
3:00remaining
Output of array initialization with designated initializers
What is the output of this C program?
C
#include <stdio.h>

int main() {
    int arr[5] = {[2] = 10, [4] = 20};
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
A0 0 10 0 20
B10 0 0 20 0
C0 10 0 20 0
D10 20 0 0 0
Attempts:
2 left
💡 Hint
Designated initializers set specific indexes; others default to zero.