An array in C++ is a collection of elements of the same type stored in contiguous memory locations. It allows storing multiple values under a single name, accessed by an index starting from 0.
Click to reveal answer
beginner
How do you declare an integer array of size 5 in C++?
You declare it as: int arr[5]; This creates an array named arr that can hold 5 integers.
Click to reveal answer
beginner
How to initialize an array with values at the time of declaration?
You can initialize an array like this: int arr[3] = {10, 20, 30}; This sets arr[0] = 10, arr[1] = 20, arr[2] = 30.
Click to reveal answer
intermediate
What happens if you provide fewer initializers than the array size?
The remaining elements are automatically set to zero. Example: int arr[5] = {1, 2}; arr[0] = 1, arr[1] = 2, arr[2] = 0, arr[3] = 0, arr[4] = 0.
Click to reveal answer
intermediate
Can you declare an array without specifying its size in C++?
Yes, if you initialize it at the same time. Example: int arr[] = {5, 10, 15}; The compiler counts the elements and sets the size to 3 automatically.
Click to reveal answer
How do you declare an array of 4 doubles named 'scores' in C++?
Adouble scores{};
Bdouble scores();
Cdouble scores;
Ddouble scores[4];
✗ Incorrect
Option D correctly declares an array of 4 doubles. Option B is a function declaration, C is a single double variable, and A is invalid syntax for array declaration.
What is the value of arr[3] after this declaration? int arr[5] = {1, 2, 3};
A0
B3
CUndefined
D5
✗ Incorrect
Only first 3 elements are initialized; the rest are set to 0 by default.
Which of these is a valid way to declare and initialize an array with 3 integers?
Aint arr[3] = {5, 10, 15};
Bint arr = {5, 10, 15};
Cint arr[3]; arr = {5, 10, 15};
Dint arr(3) = {5, 10, 15};
✗ Incorrect
Option A is correct. B is invalid because arrays cannot be assigned like that. C tries to assign after declaration which is invalid. D is invalid syntax.
If you declare int arr[] = {7, 8, 9, 10};, what is the size of arr?
AUndefined
B0
C4
DDepends on compiler
✗ Incorrect
The compiler counts the number of elements in the initializer list and sets the array size to 4.
What is the index of the first element in a C++ array?
A-1
B0
C1
DDepends on array size
✗ Incorrect
Array indexing in C++ starts at 0.
Explain how to declare and initialize an integer array of size 5 with values 1 to 5 in C++.
Use int arr[5] = {...}; syntax.
You got /3 concepts.
Describe what happens if you declare an array with size 5 but initialize only 2 elements.
Think about default values for uninitialized elements.
You got /3 concepts.
Practice
(1/5)
1. Which of the following is the correct way to declare an integer array of size 5 in C++?
easy
A. int arr = new int[5];
B. array<int> arr(5);
C. int arr[5];
D. int arr();
Solution
Step 1: Understand basic array declaration syntax
In C++, to declare a fixed-size array, we use the syntax type name[size];.
Step 2: Check each option
int arr[5]; uses int arr[5]; which is the correct syntax for declaring an integer array of size 5. int arr = new int[5]; uses dynamic allocation syntax which is valid but not a declaration. array<int> arr(5); uses a non-standard syntax. int arr(); declares a function, not an array.
Final Answer:
int arr[5]; -> Option C
Quick Check:
Array declaration = int arr[5]; [OK]
Hint: Use type name[size]; for fixed-size arrays [OK]
Common Mistakes:
Using parentheses instead of square brackets
Confusing dynamic allocation with declaration
Using non-standard syntax
2. Which of the following is the correct syntax to initialize an array of 3 integers with values 1, 2, and 3 in C++?
easy
A. int arr[3] = {1, 2, 3};
B. int arr = {1, 2, 3};
C. int arr[3] = (1, 2, 3);
D. int arr[3] = [1, 2, 3];
Solution
Step 1: Recall array initialization syntax
In C++, arrays are initialized using curly braces with values separated by commas, like int arr[size] = {values};.
Step 2: Evaluate each option
int arr[3] = {1, 2, 3}; correctly uses curly braces with values. int arr = {1, 2, 3}; misses the size in declaration. int arr[3] = (1, 2, 3); uses parentheses which is invalid. int arr[3] = [1, 2, 3]; uses square brackets which is incorrect for initialization.
Hint: Initialize arrays with curly braces and commas [OK]
Common Mistakes:
Omitting array size in declaration
Using parentheses or square brackets for initialization
Missing commas between values
3. What will be the output of the following C++ code?
int arr[4] = {10, 20};
for(int i = 0; i < 4; i++) {
std::cout << arr[i] << " ";
}
medium
A. 10 20 0 20
B. 10 20 0 0
C. 10 20 10 20
D. 10 20 garbage garbage
Solution
Step 1: Understand partial array initialization
When an array is partially initialized, the remaining elements are set to zero automatically in C++.
Step 2: Trace the loop output
The first two elements are 10 and 20 as given. The last two elements are 0 by default. So the output prints: 10 20 0 0
Final Answer:
10 20 0 0 -> Option B
Quick Check:
Partial init fills rest with 0 = 10 20 0 0 [OK]
Hint: Uninitialized array elements default to 0 if partially initialized [OK]
Common Mistakes:
Assuming uninitialized elements have garbage values
Confusing partial initialization with full initialization
Misreading loop bounds
4. Identify the error in the following C++ code snippet:
int arr[];
arr = {1, 2, 3};
medium
A. Array size must be specified during declaration
B. Array elements must be initialized with parentheses
C. Missing semicolon after array declaration
D. Cannot assign values to array after declaration like this
Solution
Step 1: Check array declaration rules
In C++, arrays must have size specified or be initialized at declaration. Here, int arr[]; is invalid because size or initializer is missing.
Step 2: Understand assignment to arrays
Arrays cannot be assigned values after declaration using = { ... }. This syntax is invalid. Initialization must happen at declaration.
Final Answer:
Cannot assign values to array after declaration like this -> Option D
Quick Check:
Arrays can't be assigned with braces after declaration [OK]
Hint: Initialize arrays only at declaration, no later assignment [OK]
Common Mistakes:
Trying to assign array values after declaration
Omitting array size without initializer
Confusing array assignment with pointer assignment
5. You want to declare and initialize a 2D integer array with 2 rows and 3 columns, where all elements are zero. Which is the best way to do this in C++?
hard
A. int arr[2][3] = {0};
B. int arr[2][3] = {{0,0,0},{0,0,0}};
C. int arr[2][3]; memset(arr, 0, sizeof(arr));
D. int arr[2][3] = {0, 0, 0, 0, 0, 0};
Solution
Step 1: Understand zero initialization of multi-dimensional arrays
In C++, initializing the first element to zero with {0} sets all elements to zero by default.
Step 2: Compare options for simplicity and correctness
int arr[2][3] = {0}; uses {0} which zero-initializes all elements. int arr[2][3] = {{0,0,0},{0,0,0}}; explicitly lists zeros but is longer. int arr[2][3]; memset(arr, 0, sizeof(arr)); uses memset which works but is less clean. int arr[2][3] = {0, 0, 0, 0, 0, 0}; lists all zeros but is verbose.
Final Answer:
int arr[2][3] = {0}; -> Option A
Quick Check:
Use {0} to zero initialize entire array [OK]
Hint: Use {0} to zero initialize all elements in arrays [OK]
Common Mistakes:
Not knowing {0} zero initializes entire array
Using memset without including <cstring>
Writing verbose initializers unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.