C++ Conditional Control Statements MCQ Quiz
Test your knowledge of C++ conditional statements (if, else, switch) and control flow with this interactive quiz. Select the correct answer for each question and see immediate feedback.
Medium Level Questions
What is the output of the following code?
int x = 5;
if (x = 0) cout << "Zero";
else if (x > 0) cout << "Positive";
else cout << "Negative";
Medium
The condition uses assignment (=) instead of equality (==), so x is set to 0, which evaluates to false. However, the if statement condition uses assignment (=) not comparison (==), so x becomes 0 and the condition evaluates to false, but the code still runs and prints "Zero" because of the way the condition is structured.
Which of the following is NOT a valid conditional statement in C++? Medium
C++ uses parentheses () for conditional expressions in if statements, not square brackets []. The other options are all valid: A uses a variable as condition (evaluates to true if non-zero), B uses assignment (valid but often a logical error), and C uses proper equality comparison.
What is the output of the following code?
int x = 10;
if (x > 5)
if (x > 8)
cout << "A";
else
cout << "B";
Medium
The else clause is associated with the nearest if statement. In this case, the else is associated with the inner if (x > 8), not the outer if (x > 5). Since x is 10, both conditions are true, so "A" is printed. The else part is not executed.
Which data type is NOT allowed in a switch statement condition in C++? Medium
Switch statements in C++ can only work with integral types (int, char, enum) and since C++11, with enumeration classes. Floating-point types like float and double are not allowed because exact comparison of floating-point values is problematic due to precision issues.
What is the output of the following code?
int x = 2;
switch(x) {
case 1: cout << "One";
case 2: cout << "Two";
case 3: cout << "Three";
default: cout << "Default";
}
Medium
In switch statements, execution falls through to the next case unless a break statement is used. Since there are no break statements, when x is 2, it executes case 2, then continues to case 3, and then the default case.
Which ternary operator expression is equivalent to: if (a > b) max = a; else max = b; Medium
Both expressions are valid and equivalent to the if-else statement. Option A uses the ternary operator to return the appropriate value, while option C uses the ternary operator to choose which assignment to execute.
What is the output of the following code?
int x = 0;
if (x = 0) cout << "Zero";
else cout << "Not Zero";
Medium
The condition uses assignment (=) instead of comparison (==). x is assigned the value 0, and the assignment expression itself evaluates to 0, which is false. Therefore, the else block is executed, printing "Not Zero".
Which of the following is a valid switch statement? Medium
Option B is valid - it has multiple cases without code between them (fall-through). Option A is invalid because switch cannot use float values. Option C is invalid because it has duplicate case values. Option D is invalid because it has multiple default cases.
What is the output of the following code?
int x = 5;
if (x < 10)
if (x > 3) cout << "A";
else cout << "B";
Medium
The else clause is associated with the nearest if statement (if (x > 3)). Since x is 5, both conditions (x < 10 and x > 3) are true, so "A" is printed. The else part is not executed.
Which of the following is true about the conditional (ternary) operator in C++? Medium
The ternary operator (?:) in C++ can be used as an lvalue (in some cases), it returns a value that can be assigned or used in expressions, and it can be nested to create complex conditional expressions.
Advanced Level Questions
What is the output of the following code?
int x = 1;
switch(x) {
default: cout << "Default";
case 1: cout << "One";
case 2: cout << "Two";
}
Advanced
The default case in a switch statement doesn't have to be at the end. However, when x is 1, it matches case 1, so that case is executed. Since there's no break statement, execution falls through to case 2. The default case is not executed because there's a matching case.
What is the value of y after this code executes?
int x = 5, y = 0;
if (x > 2)
if (x > 4)
y = 1;
else
y = 2;
Advanced
The else clause is associated with the nearest if statement (if (x > 4)). Since x is 5, both outer and inner conditions are true, so y is set to 1. The else part is not executed.
Which of the following statements about switch-case is FALSE? Advanced
The default case in a switch statement is optional, not mandatory. Option B is true due to fall-through behavior. Option A is true - case labels must be constant expressions. Option D is false - variables cannot be declared in case blocks without braces due to scope issues.
What is the output of the following code?
int x = 0;
if (x++ == 0 && ++x == 2)
cout << "True: " << x;
else
cout << "False: " << x;
Advanced
The expression uses short-circuit evaluation. x++ == 0 evaluates to true (x becomes 1), but since it's true, the second part (++x == 2) is evaluated. ++x increments x to 2, but 2 == 2 is true. So the overall condition is true && true = true. However, there's a mistake in the explanation - the correct output should be True: 2. Let me re-evaluate: x starts at 0. x++ == 0 is true (x becomes 1). Since first condition is true, second condition is evaluated: ++x makes x=2, and 2==2 is true. So overall condition is true, and x is 2. The correct answer should be A) True: 2.
What is the value of result after this code executes?
int x = 5;
int result = (x > 2) ? (x < 10) ? 1 : 2 : 3;
Advanced
This is a nested ternary operator. The expression evaluates as: (x > 2) ? ((x < 10) ? 1 : 2) : 3. Since x is 5, both x > 2 and x < 10 are true, so the result is 1.
What is the output of the following code?
int x = 10;
if (x = 5)
cout << "x is 5";
else if (x = 0)
cout << "x is 0";
else
cout << "x is " << x;
Advanced
The conditions use assignment (=) instead of comparison (==). x is first assigned the value 5, and the assignment expression evaluates to 5 (non-zero, which is true). So the first if block executes, printing "x is 5". The else if and else blocks are not executed.
Which of the following is a valid use of switch statement with C++17 features? Advanced
C++17 introduced init-statements for switch statements, allowing variable declaration in the switch condition. However, the variable must still be of an integral or enumeration type. Option B uses string (not allowed), option C uses float (not allowed), but option A uses auto which could deduce to an integral type.
What is the output of the following code?
int x = 1;
switch(x) {
case 1: int y = 5; cout << y; break;
case 2: y = 10; cout << y; break;
}
Advanced
This code will not compile because we cannot jump past the initialization of variable y. If case 1 is executed, y is initialized, but if case 2 is executed directly, it would skip the initialization. To fix this, we need to create a block with braces: case 1: { int y = 5; cout << y; break; }
What is the value of result after this code executes?
int x = 5;
int result = x > 3 ? x < 7 ? 1 : 2 : x > 0 ? 3 : 4;
Advanced
This is a complex nested ternary operator. The expression evaluates as: x > 3 ? (x < 7 ? 1 : 2) : (x > 0 ? 3 : 4). Since x is 5, both x > 3 and x < 7 are true, so the result is 1.
What is the output of the following code?
int x = 0;
if (x = 0)
cout << "A";
else if (x == 0)
cout << "B";
else
cout << "C";
Advanced
The first condition uses assignment (=): x is set to 0, and the assignment expression evaluates to 0 (false). So the first if block is skipped. The else if condition uses comparison (==): x == 0 is true (x is 0), so "B" is printed.