Challenge - 5 Problems
Array Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Uninitialized elements in an array initialized with fewer values are set to zero.
✗ Incorrect
In C, when you initialize an array with fewer elements than its size, the remaining elements are automatically set to zero.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Remember that strings in C end with a special null character.
✗ Incorrect
The string "Hello" is stored as characters followed by a null terminator '\0' which has ASCII value 0.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Uninitialized elements in nested arrays are set to zero.
✗ Incorrect
In multidimensional arrays, missing elements in inner braces are set to zero, and missing inner braces initialize remaining elements to zero.
❓ Predict Output
advanced2: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];
Attempts:
2 left
💡 Hint
Look carefully at the assignment to arr[3].
✗ Incorrect
arr[3] is assigned the sum of arr[1] (1) and arr[2] (2), so arr[3] becomes 3.
❓ Predict Output
expert3: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; }
Attempts:
2 left
💡 Hint
Designated initializers set specific indexes; others default to zero.
✗ Incorrect
Only arr[2] and arr[4] are set explicitly; others are zero by default.