0
0
C++programming~5 mins

Array initialization in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is array initialization in C++?
Array initialization means giving values to the elements of an array when you create it. This sets the starting values for the array.
Click to reveal answer
beginner
How do you initialize an integer array of size 5 with values 1, 2, 3, 4, 5 in C++?
int arr[5] = {1, 2, 3, 4, 5};
Click to reveal answer
intermediate
What happens if you initialize an array with fewer values than its size?
The remaining elements are set to zero automatically.
Click to reveal answer
intermediate
Can you initialize an array without specifying its size? How?
Yes. You can let the compiler count the values: int arr[] = {1, 2, 3}; The size becomes 3 automatically.
Click to reveal answer
intermediate
What is the difference between partial initialization and no initialization of an array?
Partial initialization sets some elements and the rest become zero. No initialization leaves elements with garbage (random) values.
Click to reveal answer
How do you initialize an array of 4 integers with all zeros in C++?
Aint arr[4] = 0;
Bint arr[4] = {0};
Cint arr[4] = {};
Dint arr[4];
What is the size of the array: int arr[] = {10, 20, 30}; ?
A0
B3
C10
DUndefined
If you write int arr[5] = {1, 2}; what are the values of arr[2], arr[3], and arr[4]?
A1, 2, 3
B1, 2, 0
CGarbage values
D0, 0, 0
Which of these is NOT a valid way to initialize an array in C++?
Aint arr[3] = {5, 6, 7};
Bint arr[] = {1, 2, 3};
Cint arr[3] = 1, 2, 3;
Dint arr[3] = {};
What is the value of arr[0] after this code? int arr[3] = {};
A0
BGarbage
CUninitialized
D1
Explain how to initialize an array in C++ and what happens if you provide fewer values than the array size.
Think about using braces {} and what the compiler does with missing values.
You got /3 concepts.
    Describe the difference between initializing an array with all zeros and leaving it uninitialized.
    Consider what values you get if you don't set any values explicitly.
    You got /3 concepts.