A built-in C++ array is declared as int values[5];. Which range contains all and only its valid indices?
10 Building C++ Programs Online Quiz Questions
Use this free practice quiz with 20 questions to review 10 Building C++ Programs, test your knowledge, and prepare for your next test or exam.
In the Counter class, does the declaration int getValue() const indicate that the member function does not modify the Counter object?
- A
True
- B
False
What C++ term describes the result of reading values[5] when values was declared as int values[5];?
Complete the design principle: When passing a built-in array to a function, pass the array's separately so the function knows how many elements it may process.
In the function call doubleValue(&value), followed by the statement *number *= 2, what are the roles of & and *?
- A
&dereferences a pointer and*obtains an address - B
Both
&and*obtain addresses - C
&obtains an address and*dereferences a pointer - D
Both
&and*dereference pointers
If NDEBUG is defined before including <cassert>, are ordinary assertions disabled?
- A
True
- B
False
Which two practices help a function process an array safely when the number of elements matters? Select all correct choices.
- A
Pass the array size as a separate parameter
- B
Assume the function can infer the array length from
const int values[] - C
Use a size-aware type such as
std::arrayorstd::vectorwhen appropriate - D
Omit bounds information because the loop condition will always be safe
What object-oriented programming concept groups data with its operations while controlling access to the data?
In the Counter class, the data member value belongs in the section to prevent unrelated code from changing it directly.
In the integrated bestStudent example, what does the return type const Student* communicate about the result?
- A
A copy of the highest-scoring
Student, which can always be modified - B
A pointer to the highest-scoring array element, with modification through that pointer prevented
- C
The index of the highest-scoring student, with no connection to the array
- D
A pointer to a newly allocated student that must always be deleted by the caller
Which tests does the Materials identify as useful for bestStudent? Select all correct choices.
- A
A one-student array
- B
Several students with tied scores
- C
A case in which the first student has the highest score
- D
A case in which the last student has the highest score
You are designing a small C++ program that reads student scores, determines the best student, and verifies the result. Explain how you would separate input and output, processing, data representation, and testing, and give one benefit of this organization.
A C++ program declares a function and calls it, but the corresponding definition is not included in the linked program. Which error category should you expect?
- A
A runtime error caused by invalid user input
- B
A linker error caused by a missing matching function definition
- C
A logic error caused by a wrong calculation
- D
Undefined behavior caused by reading an array element
In the practical structure of a beginner C++ program, which component coordinates input, processing, and output?
- A
A class definition stores related data and member functions.
- B
The
mainfunction coordinates input, processing, and output. - C
A header file automatically tests every function.
- D
An array declaration determines the program's user interface.
When is a range-based for loop generally the clearest choice for processing an array?
- A
It is required whenever an array has more than four elements.
- B
It is often clearer when the program needs each element but not its index.
- C
It automatically checks every possible pointer dereference.
- D
It allows an array to contain values of different types.
A function has the parameter int largest(const int values[], int size). Why is the separate size parameter needed for this array parameter form?
- A
The function can infer the size from the first element.
- B
The function receives the size automatically from the compiler.
- C
The caller should pass the size separately or use a size-aware container.
- D
The function must use pointer arithmetic to discover the size.
A function receives an int* number and may be called with nullptr. What should it do before evaluating *number?
- A
Check whether the pointer is
nullptrbefore using*number. - B
Always allocate a second integer inside the function.
- C
Convert the pointer to an array before dereferencing it.
- D
Use
deleteon the pointer before modifying the object.
True or false: Defining NDEBUG before including <cassert> disables ordinary assertions.
- A
True
- B
False
A range-based loop computes the total of int values[5] = {8, 3, 9, 4, 6};. What value should the total be?
In the bestStudent example, which C++ operator accesses a member through the pointer winner?