What does this C++ code print? int x = 12; if (x > 15) { std::cout << "large"; } else if (x > 10) { std::cout << "medium"; } else { std::cout << "small"; }
03 Conditional Control Structures Online Quiz Questions
Use this free practice quiz with 20 questions to review 03 Conditional Control Structures, test your knowledge, and prepare for your next test or exam.
True or false: In a nested C++ conditional, an else belongs to the nearest preceding unmatched if, unless braces establish a different statement structure.
- A
True
- B
False
In C++, when an arithmetic expression with value zero is used as a condition, it is interpreted as .
What C++ keyword phrase introduces an additional condition between an if branch and a final else branch?
Which approach is most appropriate for testing whether an integer value lies between 1 and 10, inclusive?
- A
Use
switchwith a case for every possible value in the range. - B
Use
switchwith the conditionvalue >= 1 && value <= 10. - C
Use
ifwith a compound range condition. - D
Use
switchbecause ranges are discrete constant values.
Select all correct statements about the short-circuit behavior of C++ logical operators.
- A
In
A && B,Bis not evaluated whenAis false. - B
In
A || B,Bis not evaluated whenAis true. - C
denominator != 0 && numerator / denominator > 2can prevent division when the denominator is zero. - D
In
A && B, both operands are always evaluated before the result is known.
True or false: In a C++ switch, reaching a case label automatically exits the switch after that case's statements execute.
- A
True
- B
False
To normally stop execution after a matching case in a C++ switch, place the keyword at the end of that case.
Which C++ logical operator should connect age >= 18 and has_permission when entry requires both adulthood and permission?
Consider a rule that permits entry only when age >= 18 and has_ticket is true. Select all statements that correctly apply nested and compound conditional logic to this rule.
- A
With
age = 17andhas_ticket = true,age >= 18 && has_ticketis false. - B
A nested test can check
has_ticketonly afterage >= 18succeeds. - C
A compound condition can express the two required tests as
age >= 18 && has_ticket. - D
With
age = 17andhas_ticket = true, the nested version must printEnter the event.
Which condition correctly implements this rule: a learner satisfies the requirement if the score is at least 60 and attendance is at least 75, or the learner has special permission?
- A
score >= 60 || attendance >= 75 || has_special_permission - B
(score >= 60 && attendance >= 75) || has_special_permission - C
score >= 60 && (attendance >= 75 || has_special_permission) - D
(score >= 60 || attendance >= 75) && has_special_permission
Which declaration provides a character selector expression suitable for a C++ switch statement?
- A
char command = 's'
- B
double temperature = 21.5
- C
std::string command = "save"
- D
bool is_ready = true
Consider the condition denominator != 0 && numerator / denominator > 2. If denominator is 0, what happens when C++ evaluates this condition?
- A
The program always divides by zero.
- B
The division is skipped.
- C
The program must throw an exception.
- D
The right side is evaluated before the left side.
What happens in this C++ code? int x = -3; if (x) { /* body */ }
- A
The body does not execute because
xis not Boolean. - B
The body executes because
-3is nonzero. - C
The body executes only when
xequals1. - D
The program is ill-formed because
ifrequires a comparison.
True or false: In C++, a < b < c means the same thing as a < b && b < c.
- A
True
- B
False
What does this code print when logged_in is false and is_admin is true?
if (logged_in) {
if (is_admin) {
std::cout << "Administrator\n";
} else {
std::cout << "Not logged in\n";
}
} else {
std::cout << "Not logged in\n";
}
- A
Administrator
- B
A ticket is required.
- C
Not logged in
- D
Nothing is printed.
Which C++ operator should replace OP to execute the body only when is_closed is false? if (OPis_closed) { /* body */ }
What value is assigned to grade when score is 95? if (score >= 90) { grade = 'A'; } else if (score >= 80) { grade = 'B'; } else if (score >= 70) { grade = 'C'; } else { grade = 'F'; }
- A
B
- B
A
- C
C
- D
F
In this switch, how many case labels lead to the output Summer? switch (month) { case 6: case 7: case 8: std::cout << "Summer\n"; break; default: std::cout << "Another season\n"; }
A C++ program contains this condition: if (x = 5) { ... }. Explain what the condition actually does, why its body executes when reached, and rewrite it to test whether x is equal to 5.