Challenge - 5 Problems
If Statement Mastery
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 the following C# code?
C Sharp (C#)
int x = 10; int y = 20; if (x > 5) { if (y < 15) Console.WriteLine("A"); else Console.WriteLine("B"); } else { Console.WriteLine("C"); }
Attempts:
2 left
💡 Hint
Check the conditions inside both if statements carefully.
✗ Incorrect
The outer if checks if x > 5, which is true (10 > 5). Then the inner if checks if y < 15, which is false (20 < 15 is false). So the else branch of the inner if runs, printing "B".
❓ Predict Output
intermediate2:00remaining
If-else if-else chain output
What will be printed when this code runs?
C Sharp (C#)
int score = 75; if (score >= 90) Console.WriteLine("Excellent"); else if (score >= 70) Console.WriteLine("Good"); else Console.WriteLine("Try again");
Attempts:
2 left
💡 Hint
Check which condition matches the score value first.
✗ Incorrect
The score is 75, which is not >= 90, so the first if is false. The else if checks if score >= 70, which is true, so it prints "Good" and skips the else.
❓ Predict Output
advanced2:00remaining
If statement with multiple conditions
What is the output of this code snippet?
C Sharp (C#)
int a = 5; int b = 10; if (a > 0 && b < 5) Console.WriteLine("X"); else if (a > 0 || b < 5) Console.WriteLine("Y"); else Console.WriteLine("Z");
Attempts:
2 left
💡 Hint
Evaluate each condition carefully using logical AND and OR.
✗ Incorrect
The first if condition (a > 0 && b < 5) is false because b < 5 is false (10 < 5 is false). The else if condition (a > 0 || b < 5) is true because a > 0 is true. So it prints "Y".
❓ Predict Output
advanced2:00remaining
If statement with assignment inside condition
What will be printed by this code?
C Sharp (C#)
int x = 0; if ((x = 5) > 3) Console.WriteLine(x); else Console.WriteLine("No");
Attempts:
2 left
💡 Hint
Remember that assignment inside if condition changes the variable value.
✗ Incorrect
The assignment (x = 5) sets x to 5, then checks if 5 > 3, which is true. So it prints the value of x, which is 5.
🧠 Conceptual
expert3:00remaining
If statement execution flow with multiple conditions and side effects
Consider the following code. What is the final value of variable result after execution?
C Sharp (C#)
int a = 2; int b = 3; int result = 0; if (a++ > 2 && ++b > 3) result = a + b; else result = a - b;
Attempts:
2 left
💡 Hint
Remember how post-increment and pre-increment operators work and short-circuit evaluation.
✗ Incorrect
The condition a++ > 2 evaluates as 2 > 2 (false), but a is incremented to 3 after. Because the first condition is false, the second condition (++b > 3) is not evaluated due to short-circuit AND. b remains 3. So the else branch runs: result = a - b = 3 - 3 = 0.