True or false: In C++, using the integer expression 0 as the condition of an if statement selects the false branch.
3 Selection and Control Structures Online Quiz Questions
Use this free practice quiz with 20 questions to review 3 Selection and Control Structures, test your knowledge, and prepare for your next test or exam.
Given the condition A || B, when is B evaluated under C++ short-circuit evaluation?
- A
Bis always evaluated beforeA - B
Bis evaluated only ifAis false - C
Bis evaluated only ifAis true - D
Neither operand is evaluated if
Ais false
Which C++ logical operator reverses a Boolean condition, such as changing finished to its opposite value?
Complete the statement: In an if/else structure, the block executes when the if condition is false.
Select all situations for which a C++ switch statement is an appropriate choice.
- A
Selecting among fixed menu values stored in an integer
- B
Testing whether a score is within a range such as 80 through 89
- C
Selecting among fixed values of an enumeration
- D
Combining unrelated conditions such as
isAdmin && hasId
Complete the statement: If no case matches the switch expression and a fallback label is present, control transfers to .
What is the maximum number of default labels allowed in one C++ switch statement?
Select all statements that correctly describe break and fallthrough in a C++ switch statement.
- A
Executing
breakexits theswitchstatement - B
Omitting
breakautomatically exits after the current case - C
Omitting
breakcan cause execution to continue into the next case - D
A
breakchanges the value of the switch expression
What is the primary purpose of the C++ conditional operator ?:?
- A
It always evaluates both value alternatives and returns their sum
- B
It executes a sequence of statements in both branches
- C
It selects one of two values based on a condition
- D
It compares two values and returns a Boolean result
Explain when a C++ programmer should choose if or an if/else if chain, switch, or the conditional operator ?:. Include one reason not to use a deeply nested conditional expression.
What happens when C++ evaluates if (value = 5) rather than if (value == 5)?
- A
It compares
valuewith 5 and enters the branch only if they are equal - B
It assigns 5 to
value, then the resulting nonzero value makes the condition true - C
It produces a compilation error because assignment cannot appear in a condition
- D
It assigns 0 to
valuebecause=is the equality operator
True or false: Direct equality comparison of floating-point values can be unreliable because many decimal fractions cannot be represented exactly in binary floating-point.
- A
True
- B
False
Given int count = 0;, what happens when the program executes if (count) { std::cout << "There are items\n"; }?
- A
The message is printed because every integer is true.
- B
The message is not printed because zero converts to
false. - C
The program fails to compile because
countis not abool. - D
The message is printed only when
countis negative.
True or false: In the expression A && B, B is evaluated only if A is true.
- A
True
- B
False
What is the effect of executing if (value = 5) { ... } in C++?
- A
It tests whether
valueis equal to5. - B
It always makes the condition false.
- C
It assigns
5tovalue, and the nonzero result makes the condition true. - D
It produces a compilation error because assignments cannot appear in conditions.
Which C++ selection construct is designed to compare one integral or enumeration expression with several fixed constant values?
What value is assigned to grade by this code when score is 95? if (score >= 90) { grade = 'A'; } else if (score >= 80) { grade = 'B'; } else { grade = 'F'; }
- A
gradebecomes'A'. - B
gradebecomes'B'because 95 is also at least 80. - C
graderemains uninitialized. - D
All matching branches execute in sequence.
What C++ operator compactly selects one of two values based on a condition, as in age >= 18 ? "adult" : "minor"?
What output is produced by the switch statement when option has the value 4 and the statement contains cases 1, 2, and 3 plus a default that prints Unknown option?
- A
Starting - B
Settings - C
Unknown option - D
Nothing, because a switch must always have a matching case
Which selection structure is the clearest choice for assigning grades based on ranges such as score >= 90, score >= 80, and score >= 70?
- A
A
switchwith a case for every possible score - B
An
if/else ifchain - C
A conditional expression with several nested
?:operators - D
A
switchusingscore >= 90as a case label