Challenge - 5 Problems
If–else Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested if–else statements
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { int x = 10; if (x > 5) { if (x < 15) { std::cout << "A"; } else { std::cout << "B"; } } else { std::cout << "C"; } return 0; }
Attempts:
2 left
💡 Hint
Check the conditions step by step starting from the outer if.
✗ Incorrect
x is 10, which is greater than 5, so the outer if is true. Then, x is less than 15, so the inner if is true, printing 'A'.
❓ Predict Output
intermediate2:00remaining
Output with else if ladder
What will this program print?
C++
#include <iostream> int main() { int score = 75; if (score >= 90) { std::cout << "A"; } else if (score >= 80) { std::cout << "B"; } else if (score >= 70) { std::cout << "C"; } else { std::cout << "F"; } return 0; }
Attempts:
2 left
💡 Hint
Check which condition the score satisfies first.
✗ Incorrect
Score is 75, which is not >= 90 or >= 80, but is >= 70, so it prints 'C'.
❓ Predict Output
advanced2:00remaining
Output of if–else with logical operators
What is the output of this code?
C++
#include <iostream> int main() { int a = 5, b = 10; if (a > 0 && b < 5) { std::cout << "X"; } else if (a > 0 || b < 5) { std::cout << "Y"; } else { std::cout << "Z"; } return 0; }
Attempts:
2 left
💡 Hint
Evaluate the logical conditions carefully.
✗ Incorrect
a > 0 is true, b < 5 is false, so first if is false (true && false = false). Second condition is true (true || false = true), so prints 'Y'.
❓ Predict Output
advanced2:00remaining
Value of variable after if–else execution
What is the value of variable result after running this code?
C++
#include <iostream> int main() { int x = 3, result = 0; if (x % 2 == 0) { result = 10; } else { result = 20; } std::cout << result; return 0; }
Attempts:
2 left
💡 Hint
Check if x is even or odd.
✗ Incorrect
x is 3, which is odd, so else block runs setting result to 20.
❓ Predict Output
expert2:00remaining
Behavior of if–else without braces
What will be the output of this code snippet?
C++
#include <iostream> int main() { int x = 0; if (x == 0) std::cout << "Zero"; std::cout << "Done"; else std::cout << "Not zero"; return 0; }
Attempts:
2 left
💡 Hint
Without braces, 'if' controls only the next single statement, and an extra statement before 'else' causes a syntax error.
✗ Incorrect
This code causes a compilation error ('else' without a previous 'if'). Without braces, the 'if' controls only the first 'std::cout << "Zero";'. The next 'std::cout << "Done";' is unconditional and separates the 'if' from the 'else', making the 'else' unmatched.