True or false: For a built-in C++ array containing N elements, the valid indexes are 0 through N - 1.
6 Arrays and Collections Online Quiz Questions
Use this free practice quiz with 20 questions to review 6 Arrays and Collections, test your knowledge, and prepare for your next test or exam.
What is the value of values after this declaration? int values[4] = {7};
- A
{7, 7, 7, 7}
- B
{7, 0, 0, 0}
- C
{0, 0, 0, 7}
- D
The declaration is invalid because every element needs an initializer
What is the worst-case time complexity of the introductory linear search described for an array?
Complete both statements. If fewer initializers are supplied than an array contains, the remaining elements are . If the bound is omitted in int primes[] = {2, 3, 5};, the bound is from the initializer list.
Which range-based for loop correctly allows each element of int values[] = {1, 2, 3}; to be modified in place?
- A
for (int value : values) - B
for (int& value : values) - C
for (const int& value : values) - D
for (int* value : values)
True or false: A function that receives a built-in array parameter automatically knows how many elements the original array contains.
- A
True
- B
False
Given int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};, what integer value does matrix[1][2] produce?
Select all correct statements about std::array<int, 4>.
- A
It stores exactly the number of elements specified by its second template argument.
- B
It automatically changes size whenever elements are added.
- C
It provides a
.size()member function. - D
It supports standard container operations and iterators.
Complete the rule for a two-dimensional array parameter: in void printMatrix(const int matrix[][3], std::size_t rows), the inner dimension has elements, and only the bound may be omitted.
Which statement correctly computes a fractional average from an integer sum and an integer element count count?
- A
double average = sum / count; - B
double average = static_cast<int>(sum) / count; - C
double average = static_cast<double>(sum) / count; - D
int average = static_cast<double>(sum / count);
Select all practices that represent a problem described in the material.
- A
Using
i <= 5to traverseint values[5] - B
Reading
values[0]from an uninitialized automatic array - C
Using
i < 5to traverse all five elements - D
Assuming
sizeof(values)inside an array-parameter function gives the original array's element count
Explain why a function that accepts a built-in array parameter should usually receive an element count separately. Then describe one alternative that preserves the array's complete type and size.
In what order are the elements stored contiguously for int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};?
- A
1, 4, 2, 5, 3, 6
- B
1, 2, 3, 4, 5, 6
- C
1, 2, 4, 5, 3, 6
- D
1, 3, 5, 2, 4, 6
True or false: A built-in C++ array can be copied and assigned as an ordinary object, just like a std::array.
- A
True
- B
False
Which range contains all valid indexes for the declaration int scores[4];?
- A
0 through 4
- B
0 through 3
- C
1 through 4
- D
1 through 3
What does char word[] = "cat"; contain?
- A
The array contains only the three visible letters.
- B
The array contains the letters plus a space character.
- C
The array contains the letters plus a terminating null character.
- D
The declaration is invalid because the bound is omitted.
Which range-based for loop correctly doubles every element of int values[] = {2, 4, 6};?
- A
int value : values - B
const int& value : values - C
int& value : values - D
const auto value : values
What are the elements of int values[5] = {10, 20}; after initialization?
- A
{10, 20, 10, 20, 10} - B
{10, 20, 0, 0, 0} - C
{10, 20}with only two elements - D
The declaration is invalid because five initializers are required
What is the average of int scores[] = {80, 75, 90, 85};? Give the result as a decimal value.
How many elements does int primes[] = {2, 3, 5, 7, 11}; contain?