Challenge - 5 Problems
If Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Java if statement code?
Consider the following Java code snippet. What will it print when run?
Java
int x = 10; if (x > 5) { System.out.println("Greater than 5"); } else { System.out.println("5 or less"); }
Attempts:
2 left
💡 Hint
Check the condition x > 5 and what it means for x = 10.
✗ Incorrect
Since x is 10, which is greater than 5, the if condition is true, so it prints "Greater than 5".
❓ Predict Output
intermediate2:00remaining
What will this nested if-else print?
Look at this Java code. What is the output?
Java
int a = 3; int b = 7; if (a > 5) { System.out.println("A is big"); } else if (b > 5) { System.out.println("B is big"); } else { System.out.println("Neither is big"); }
Attempts:
2 left
💡 Hint
Check the values of a and b and the order of conditions.
✗ Incorrect
a is 3 (not > 5), so first if is false. b is 7 (> 5), so else if is true, printing "B is big".
❓ Predict Output
advanced2:00remaining
What is the output of this if statement with boolean logic?
What does this Java code print?
Java
boolean flag = false; int num = 8; if (!flag && num < 10) { System.out.println("Condition met"); } else { System.out.println("Condition not met"); }
Attempts:
2 left
💡 Hint
Remember !flag means flag is false, so !flag is true.
✗ Incorrect
flag is false, so !flag is true. num is 8, which is less than 10. Both conditions true, so prints "Condition met".
❓ Predict Output
advanced2:00remaining
What error does this Java if statement code cause?
What happens when you run this code?
Java
int x = 5; if (x == 5) { System.out.println("x is five"); }
Attempts:
2 left
💡 Hint
Check the line endings carefully.
✗ Incorrect
The first line is missing a semicolon at the end, causing a compilation syntax error.
❓ Predict Output
expert2:00remaining
What is the value of variable result after this if-else code?
After running this Java code, what is the value of result?
Java
int a = 4; int b = 9; int result; if (a > b) { result = a - b; } else if (a == b) { result = a + b; } else { result = b - a; }
Attempts:
2 left
💡 Hint
Compare a and b and see which condition is true.
✗ Incorrect
a is 4, b is 9. a > b is false, a == b is false, so else runs: result = b - a = 9 - 4 = 5.