Chapter 6 Beginner 57 Questions

Practice Questions — Conditional Statements (if, else, switch)

← Back to Notes
11 Easy
12 Medium
10 Hard

Topic-Specific Questions

Question 1
Easy
What is the output?
int x = 10;
if (x > 5) {
    cout << "Yes";
}
Check if 10 > 5.
Yes
Question 2
Easy
What is the output?
int a = 3, b = 7;
if (a > b) {
    cout << a;
} else {
    cout << b;
}
Compare 3 and 7.
7
Question 3
Easy
What is the output?
int num = 0;
if (num) {
    cout << "Truthy";
} else {
    cout << "Falsy";
}
In C++, what is the truthiness of 0?
Falsy
Question 4
Easy
What is the output?
int x = 15;
if (x >= 10 && x <= 20) {
    cout << "In range";
} else {
    cout << "Out of range";
}
Check if 15 is between 10 and 20 inclusive.
In range
Question 5
Easy
What is the output?
int a = 5, b = 10;
int result = (a > b) ? a : b;
cout << result;
Ternary: if condition is false, the value after : is used.
10
Question 6
Easy
What is the output?
char ch = 'B';
switch (ch) {
    case 'A': cout << "Alpha"; break;
    case 'B': cout << "Beta"; break;
    case 'C': cout << "Gamma"; break;
    default: cout << "Unknown";
}
ch is 'B', find the matching case.
Beta
Question 7
Medium
What is the output?
int x = 5;
if (x = 0) {
    cout << "Zero";
} else {
    cout << "Not zero";
}
Notice the single = sign in the condition.
Not zero
Question 8
Medium
What is the output?
int day = 3;
switch (day) {
    case 1: cout << "Mon ";
    case 2: cout << "Tue ";
    case 3: cout << "Wed ";
    case 4: cout << "Thu ";
            break;
    case 5: cout << "Fri ";
}
No break after case 3. What happens?
Wed Thu
Question 9
Medium
What is the output?
int a = 10, b = 20, c = 15;
if (a > b) {
    if (a > c) cout << a;
    else cout << c;
} else {
    if (b > c) cout << b;
    else cout << c;
}
This finds the maximum of three numbers.
20
Question 10
Medium
What is the output?
int x = 5;
if (x > 10);
{
    cout << "Hello";
}
Notice the semicolon after the if condition.
Hello
Question 11
Medium
What is the output?
int marks = 75;
if (marks >= 90)
    cout << "A";
else if (marks >= 80)
    cout << "B";
else if (marks >= 70)
    cout << "C";
else
    cout << "D";
Check conditions top to bottom. Which is the first to be true?
C
Question 12
Medium
What is the output?
int x = -5;
int abs = (x >= 0) ? x : -x;
cout << abs;
x is negative. Which branch of the ternary is taken?
5
Question 13
Hard
What is the output?
int a = 1, b = 0;
if (a > 0)
    if (b > 0)
        cout << "Both positive";
else
    cout << "Not both positive";
Dangling else: which if does the else bind to?
Not both positive
Question 14
Hard
What is the output?
int x = 3;
switch (x) {
    default: cout << "D ";
    case 1: cout << "1 "; break;
    case 2: cout << "2 ";
    case 3: cout << "3 ";
}
default does not have to be at the end. And case 3 has no break.
3
Question 15
Hard
What is the output?
int x = 5;
switch (x) {
    default: cout << "D ";
    case 1: cout << "1 "; break;
    case 2: cout << "2 ";
}
No case matches x = 5. Where does execution start?
D 1
Question 16
Hard
What is the output?
int a = 5, b = 3;
cout << ((a > b) ? (a - b) : (b - a)) << endl;
cout << ((a < b) ? (a - b) : (b - a)) << endl;
Evaluate each ternary condition separately.
2
-2
Question 17
Easy
In C++, what values are considered truthy and falsy when used in an if condition?
Think about zero versus non-zero.
In C++, 0 is falsy and every non-zero value is truthy. This includes negative numbers: if (-1) is true. For pointers, nullptr (which is 0) is falsy, and any valid pointer is truthy.
Question 18
Medium
What is the dangling else problem in C++? How do you avoid it?
Think about which if an else binds to when there are no braces.
The dangling else problem occurs when an else could associate with more than one if statement. C++ resolves it by binding else to the nearest unmatched if, regardless of indentation. To avoid ambiguity, always use curly braces {} around if and else bodies, even for single statements.
Question 19
Hard
Why can you not use a string or a floating-point number in a switch expression in C++?
Think about how switch is implemented at the machine level.
The switch statement is typically compiled into a jump table or binary search on case values, both of which require integral (integer/char/enum) constants for efficient comparison. Floating-point numbers have precision issues (0.1 + 0.2 != 0.3), making exact comparison unreliable. Strings are objects, not integral types, and comparing them requires calling functions rather than simple integer comparison.
Question 20
Hard
What is the output?
int x = 2;
int y = (x > 0) - (x < 0);
cout << y;
Boolean expressions evaluate to 0 or 1 in C++.
1

Mixed & Application Questions

Question 1
Easy
Write a program that takes an integer and prints whether it is positive, negative, or zero.
Use if-else if-else with two conditions.
#include 
using namespace std;
int main() {
    int n;
    cin >> n;
    if (n > 0) cout << "Positive" << endl;
    else if (n < 0) cout << "Negative" << endl;
    else cout << "Zero" << endl;
    return 0;
}
Question 2
Easy
What is the output?
int a = 10;
if (a == 10)
    cout << "Ten ";
cout << "Done";
Without braces, only the first statement after if is in the body.
Ten Done
Question 3
Easy
What is the output?
int x = 7;
if (x > 5 && x < 10)
    cout << "Yes";
else
    cout << "No";
Is 7 greater than 5 AND less than 10?
Yes
Question 4
Medium
What is the output?
int x = 5;
if (x > 3)
    if (x > 10)
        cout << "A";
    else
        cout << "B";
else
    cout << "C";
Apply the dangling else rule carefully.
B
Question 5
Medium
Write a program that takes a character and checks if it is a vowel or consonant (assume lowercase English letters only).
Use switch-case with fall-through for the 5 vowels.
#include 
using namespace std;
int main() {
    char ch;
    cin >> ch;
    switch (ch) {
        case 'a': case 'e': case 'i': case 'o': case 'u':
            cout << ch << " is a vowel" << endl;
            break;
        default:
            cout << ch << " is a consonant" << endl;
    }
    return 0;
}
Question 6
Medium
What is the output?
int a = 5, b = 10, c = 5;
if (a == c)
    cout << "A ";
if (a == b)
    cout << "B ";
if (b != c)
    cout << "C ";
These are three separate if statements, not if-else if.
A C
Question 7
Medium
What is the output?
int x = 10;
if (x > 5 || x < 3) {
    cout << "Yes";
} else {
    cout << "No";
}
With ||, if the first condition is true, the whole expression is true.
Yes
Question 8
Hard
What is the output?
int a = 2, b = 3, c = 4;
int result = (a > b) ? a : (b > c) ? b : c;
cout << result;
Evaluate the outer ternary first, then the nested one.
4
Question 9
Hard
What is the output?
int x = 0;
switch (x) {
    case 0:
        cout << "Zero ";
    case 1:
        cout << "One ";
    default:
        cout << "Default ";
}
No break statements anywhere. What happens after case 0 matches?
Zero One Default
Question 10
Easy
Write a program that checks if a given year is a leap year.
Leap if divisible by 4, but not 100, unless also divisible by 400.
#include 
using namespace std;
int main() {
    int year;
    cin >> year;
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        cout << year << " is a leap year" << endl;
    else
        cout << year << " is not a leap year" << endl;
    return 0;
}
Question 11
Hard
What is the output?
int a = 5;
if (a > 2)
    cout << "A ";
if (a > 3)
    cout << "B ";
if (a > 4)
    cout << "C ";
if (a > 5)
    cout << "D ";
Each if is independent. Check all four conditions.
A B C
Question 12
Medium
Write a program that takes three integers and prints them in ascending order using only if-else statements (no arrays or sorting algorithms).
Compare pairs and rearrange using swaps or nested conditions.
#include 
using namespace std;
int main() {
    int a, b, c;
    cin >> a >> b >> c;
    if (a > b) { int t = a; a = b; b = t; }
    if (b > c) { int t = b; b = c; c = t; }
    if (a > b) { int t = a; a = b; b = t; }
    cout << a << " " << b << " " << c << endl;
    return 0;
}
Question 13
Hard
What is the output?
int x = 1;
if (x)
    if (x - 1)
        cout << "A";
    else
        cout << "B";
else
    cout << "C";
x is 1 (truthy). x-1 is 0 (falsy). Apply the dangling else rule.
B

Multiple Choice Questions

MCQ 1
What is the output of if (0) cout << "Yes"; else cout << "No";?
  • A. Yes
  • B. No
  • C. Compilation error
  • D. No output
Answer: B
B is correct. In C++, 0 is falsy. The condition is false, so the else block executes and prints No.
MCQ 2
Which of the following is the correct ternary operator syntax in C++?
  • A. condition ? true : false
  • B. condition : true ? false
  • C. if condition then true else false
  • D. condition ?? true : false
Answer: A
A is correct. The ternary syntax is condition ? value_if_true : value_if_false.
MCQ 3
What happens when no case matches in a switch and there is no default?
  • A. Compilation error
  • B. Runtime error
  • C. The switch block is skipped entirely
  • D. The first case executes
Answer: C
C is correct. If no case matches and there is no default, the entire switch body is skipped. No error occurs.
MCQ 4
Which data types can be used in a switch expression in C++?
  • A. int, char, float
  • B. int, char, string
  • C. int, char, enum
  • D. Any data type
Answer: C
C is correct. switch requires an integral or enumeration type: int, char, short, long, bool, or enum. float, double, and string are not allowed.
MCQ 5
In C++, what is the value of the expression (5 > 3)?
  • A. true
  • B. 1
  • C. 5
  • D. Both A and B
Answer: D
D is correct. In C++, a boolean true has the integer value 1. The expression (5 > 3) evaluates to true, which is 1 when used in an integer context.
MCQ 6
What is the output?
int x = 5; if (x = 10) cout << x; else cout << 0;
  • A. 5
  • B. 10
  • C. 0
  • D. Compilation error
Answer: B
B is correct. x = 10 is an assignment, not comparison. It assigns 10 to x and returns 10, which is truthy. So the if body runs and prints x, which is now 10.
MCQ 7
In the dangling else problem, the else binds to:
  • A. The first if in the code
  • B. The if matching the indentation
  • C. The nearest unmatched if
  • D. It causes a compilation error
Answer: C
C is correct. C++ ignores indentation. The else always associates with the nearest preceding if that does not already have an else.
MCQ 8
What does the break statement do inside a switch-case?
  • A. Exits the program
  • B. Exits the current function
  • C. Exits the switch block
  • D. Skips to the default case
Answer: C
C is correct. break exits the switch block entirely, preventing fall-through to subsequent cases.
MCQ 9
What is the output?
int x = 3; switch(x) { case 1: case 2: case 3: cout << "Low"; break; case 4: case 5: cout << "High"; break; }
  • A. Low
  • B. High
  • C. LowHigh
  • D. No output
Answer: A
A is correct. x is 3, which matches case 3. Cases 1, 2, 3 are grouped via fall-through. The break after cout << "Low" prevents further execution.
MCQ 10
Which of the following is an infinite loop caused by a conditional bug?
  • A. if (true) {}
  • B. while (x = 1) { x++; }
  • C. for (int i=0; i<10; i++) {}
  • D. switch(1) { default: break; }
Answer: B
B is correct. x = 1 is an assignment (not comparison). It assigns 1 to x every iteration and returns 1 (truthy), so the loop never ends. The x++ has no effect because x is reset to 1 at the start of each check.
MCQ 11
What is the output?
int a = 0, b = 0; if (a++ && ++b) cout << a << b; else cout << a << b;
  • A. 00
  • B. 10
  • C. 11
  • D. 01
Answer: B
B is correct. a++ returns 0 (post-increment: uses old value), which is falsy. Due to short-circuit evaluation, ++b is never executed. After the expression, a is 1 (incremented) and b is 0 (never touched). The else branch prints 10.
MCQ 12
What is the output?
int x = 10; int y = (x > 5) ? (x < 15) ? 1 : 2 : 3; cout << y;
  • A. 1
  • B. 2
  • C. 3
  • D. Compilation error
Answer: A
A is correct. Outer ternary: x > 5 (true), so evaluate inner ternary: x < 15 (true), returns 1. y = 1.
MCQ 13
What is the value of z?
int x = 5, y = 10; int z = (x > 0) + (y > 0) + (x + y > 0);
  • A. 0
  • B. 1
  • C. 3
  • D. Compilation error
Answer: C
C is correct. Each boolean expression evaluates to 1 (true): (5>0)=1, (10>0)=1, (15>0)=1. Sum: 1+1+1=3. This exploits boolean-to-integer conversion in C++.
MCQ 14
Can the default case appear before other cases in a switch block?
  • A. No, it must be the last case
  • B. Yes, and it works correctly
  • C. Yes, but it causes undefined behavior
  • D. Only if there are no other cases
Answer: B
B is correct. The default case can appear anywhere in the switch block. If no case matches, execution jumps to default regardless of its position. Fall-through rules still apply from default to subsequent cases.

Coding Challenges

Challenge 1: Triangle Type Checker

Easy
Take three side lengths from the user and determine if they form a valid triangle. If valid, print whether it is equilateral, isosceles, or scalene.
Sample Input
Enter three sides: 5 5 5
Sample Output
Equilateral triangle
Triangle inequality: sum of any two sides must be greater than the third side.
#include <iostream>
using namespace std;
int main() {
    int a, b, c;
    cout << "Enter three sides: ";
    cin >> a >> b >> c;
    if (a + b > c && b + c > a && a + c > b) {
        if (a == b && b == c)
            cout << "Equilateral triangle" << endl;
        else if (a == b || b == c || a == c)
            cout << "Isosceles triangle" << endl;
        else
            cout << "Scalene triangle" << endl;
    } else {
        cout << "Not a valid triangle" << endl;
    }
    return 0;
}

Challenge 2: Simple Calculator with switch

Easy
Build a calculator that takes two numbers and an operator (+, -, *, /) and prints the result. Handle division by zero.
Sample Input
12 * 5
Sample Output
12 * 5 = 60
Use switch-case on the operator character. Print an error for division by zero and unknown operators.
#include <iostream>
using namespace std;
int main() {
    double a, b;
    char op;
    cin >> a >> op >> b;
    switch (op) {
        case '+': cout << a << " + " << b << " = " << a + b << endl; break;
        case '-': cout << a << " - " << b << " = " << a - b << endl; break;
        case '*': cout << a << " * " << b << " = " << a * b << endl; break;
        case '/': 
            if (b != 0) cout << a << " / " << b << " = " << a / b << endl;
            else cout << "Error: division by zero" << endl;
            break;
        default: cout << "Unknown operator" << endl;
    }
    return 0;
}

Challenge 3: Quadratic Equation Solver

Medium
Take coefficients a, b, c of a quadratic equation ax^2 + bx + c = 0. Compute the discriminant and print the nature and values of roots.
Sample Input
Enter a b c: 1 -5 6
Sample Output
Two distinct real roots: 3 and 2
Handle three cases: discriminant > 0 (two real roots), == 0 (one repeated root), < 0 (complex roots). Use sqrt() from <cmath>.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
    double a, b, c;
    cout << "Enter a b c: ";
    cin >> a >> b >> c;
    double disc = b * b - 4 * a * c;
    if (disc > 0) {
        double r1 = (-b + sqrt(disc)) / (2 * a);
        double r2 = (-b - sqrt(disc)) / (2 * a);
        cout << "Two distinct real roots: " << r1 << " and " << r2 << endl;
    } else if (disc == 0) {
        double r = -b / (2 * a);
        cout << "One repeated root: " << r << endl;
    } else {
        double real = -b / (2 * a);
        double imag = sqrt(-disc) / (2 * a);
        cout << "Complex roots: " << real << " + " << imag << "i and " << real << " - " << imag << "i" << endl;
    }
    return 0;
}

Challenge 4: Income Tax Calculator

Medium
Calculate income tax using Indian tax slabs: 0-250000: nil, 250001-500000: 5%, 500001-1000000: 20%, above 1000000: 30%. Tax is computed on each slab separately (not flat rate on total income).
Sample Input
Enter annual income: 750000
Sample Output
Tax: 37500
Use if-else if chain. Compute tax incrementally for each slab.
#include <iostream>
using namespace std;
int main() {
    double income;
    cout << "Enter annual income: ";
    cin >> income;
    double tax = 0;
    if (income > 1000000) {
        tax += (income - 1000000) * 0.30;
        income = 1000000;
    }
    if (income > 500000) {
        tax += (income - 500000) * 0.20;
        income = 500000;
    }
    if (income > 250000) {
        tax += (income - 250000) * 0.05;
    }
    cout << "Tax: " << tax << endl;
    return 0;
}

Challenge 5: Min/Max of Four Numbers Without Library

Medium
Take four integers and find the minimum and maximum using only ternary operators. Do not use any library functions or arrays.
Sample Input
Enter four numbers: 12 5 23 8
Sample Output
Min: 5 Max: 23
Use chained ternary operators. No min/max functions, no arrays, no loops.
#include <iostream>
using namespace std;
int main() {
    int a, b, c, d;
    cout << "Enter four numbers: ";
    cin >> a >> b >> c >> d;
    int minAB = (a < b) ? a : b;
    int minCD = (c < d) ? c : d;
    int minVal = (minAB < minCD) ? minAB : minCD;
    int maxAB = (a > b) ? a : b;
    int maxCD = (c > d) ? c : d;
    int maxVal = (maxAB > maxCD) ? maxAB : maxCD;
    cout << "Min: " << minVal << endl;
    cout << "Max: " << maxVal << endl;
    return 0;
}

Challenge 6: Day Name with switch and Weekday/Weekend

Easy
Take a day number (1-7) and print the day name. Also print whether it is a weekday or weekend.
Sample Input
Enter day (1-7): 6
Sample Output
Saturday (Weekend)
Use switch for the day name. Use a second condition (or grouped switch) for weekday/weekend.
#include <iostream>
using namespace std;
int main() {
    int day;
    cout << "Enter day (1-7): ";
    cin >> day;
    string name;
    bool weekend = false;
    switch (day) {
        case 1: name = "Monday"; break;
        case 2: name = "Tuesday"; break;
        case 3: name = "Wednesday"; break;
        case 4: name = "Thursday"; break;
        case 5: name = "Friday"; break;
        case 6: name = "Saturday"; weekend = true; break;
        case 7: name = "Sunday"; weekend = true; break;
        default: cout << "Invalid day" << endl; return 1;
    }
    cout << name << (weekend ? " (Weekend)" : " (Weekday)") << endl;
    return 0;
}

Challenge 7: Character Classifier

Hard
Take a character and classify it as: uppercase letter, lowercase letter, digit, whitespace, or special character. Print the ASCII value as well.
Sample Input
Enter a character: @
Sample Output
@ is a special character (ASCII: 64)
Use if-else if with character ranges ('A'-'Z', 'a'-'z', '0'-'9'). Do not use library functions like isupper().
#include <iostream>
using namespace std;
int main() {
    char ch;
    cout << "Enter a character: ";
    cin >> ch;
    cout << ch << " is ";
    if (ch >= 'A' && ch <= 'Z')
        cout << "an uppercase letter";
    else if (ch >= 'a' && ch <= 'z')
        cout << "a lowercase letter";
    else if (ch >= '0' && ch <= '9')
        cout << "a digit";
    else if (ch == ' ' || ch == '\t' || ch == '\n')
        cout << "a whitespace character";
    else
        cout << "a special character";
    cout << " (ASCII: " << (int)ch << ")" << endl;
    return 0;
}

Need to Review the Concepts?

Go back to the detailed notes for this chapter.

Read Chapter Notes

Want to learn C++ with a live mentor?

Explore our C++ Masterclass