0
0
C++programming~20 mins

Else–if ladder in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Else-if Ladder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of an else-if ladder with integer input
What is the output of the following C++ code when num = 15?
C++
#include <iostream>
using namespace std;

int main() {
    int num = 15;
    if (num < 10) {
        cout << "Less than 10";
    } else if (num < 20) {
        cout << "Between 10 and 19";
    } else if (num < 30) {
        cout << "Between 20 and 29";
    } else {
        cout << "30 or more";
    }
    return 0;
}
ABetween 10 and 19
BLess than 10
CBetween 20 and 29
D30 or more
Attempts:
2 left
💡 Hint
Check which condition is true first in the else-if ladder.
Predict Output
intermediate
2:00remaining
Output when multiple else-if conditions are false
What will the program print when score = 85 in this else-if ladder?
C++
#include <iostream>
using namespace std;

int main() {
    int score = 85;
    if (score >= 90) {
        cout << "Grade A";
    } else if (score >= 80) {
        cout << "Grade B";
    } else if (score >= 70) {
        cout << "Grade C";
    } else {
        cout << "Fail";
    }
    return 0;
}
AGrade A
BGrade B
CGrade C
DFail
Attempts:
2 left
💡 Hint
Check the conditions from top to bottom carefully.
🔧 Debug
advanced
2:00remaining
Identify the error in this else-if ladder
What error will this code produce when compiled?
C++
#include <iostream>
using namespace std;

int main() {
    int x = 5;
    if (x > 0)
        cout << "Positive";
    else if x == 0 {
        cout << "Zero";
    } else {
        cout << "Negative";
    }
    return 0;
}
ALogical error, prints wrong output
BRuntime error due to uninitialized variable
CNo error, prints "Positive"
DSyntax error due to missing parentheses in else if condition
Attempts:
2 left
💡 Hint
Check the syntax of the else-if statement carefully.
🧠 Conceptual
advanced
2:00remaining
Understanding else-if ladder flow
Consider this else-if ladder. Which statement is true about how it executes?
C++
if (a > b) {
    // block 1
} else if (a == b) {
    // block 2
} else {
    // block 3
}
AAll blocks can execute if conditions are true
BBlocks 1 and 3 can execute together
COnly one block executes depending on the first true condition
DBlocks 2 and 3 always execute together
Attempts:
2 left
💡 Hint
Think about how else-if ladder chooses which block to run.
Predict Output
expert
2:00remaining
Output of nested else-if ladder with multiple variables
What is the output of this C++ program?
C++
#include <iostream>
using namespace std;

int main() {
    int x = 10, y = 20;
    if (x > y) {
        cout << "X is greater";
    } else if (x == y) {
        cout << "X equals Y";
    } else if (y - x == 10) {
        cout << "Difference is 10";
    } else {
        cout << "Other case";
    }
    return 0;
}
ADifference is 10
BX equals Y
CX is greater
DOther case
Attempts:
2 left
💡 Hint
Check each condition carefully in order.