Challenge - 5 Problems
Conditional Mastery
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 code with conditional statements?
Look at the Java code below. What will it print when run?
Java
public class Main { public static void main(String[] args) { int temperature = 30; if (temperature > 25) { System.out.println("It's hot outside."); } else { System.out.println("It's cool outside."); } } }
Attempts:
2 left
💡 Hint
Check the value of temperature and the condition in the if statement.
✗ Incorrect
The variable temperature is 30, which is greater than 25, so the if block runs and prints "It's hot outside." The else block is skipped.
🧠 Conceptual
intermediate1:30remaining
Why do we use conditional statements in programming?
Which of the following best explains why conditional statements are needed in programs?
Attempts:
2 left
💡 Hint
Think about how programs can behave differently depending on data or user input.
✗ Incorrect
Conditional statements let programs decide what to do next based on conditions. This makes programs flexible and able to handle different situations.
❓ Predict Output
advanced2:00remaining
What is the output of this nested if-else Java code?
Analyze the Java code and select the exact output it produces.
Java
public class Main { public static void main(String[] args) { int score = 75; if (score >= 90) { System.out.println("Grade: A"); } else if (score >= 80) { System.out.println("Grade: B"); } else if (score >= 70) { System.out.println("Grade: C"); } else { System.out.println("Grade: F"); } } }
Attempts:
2 left
💡 Hint
Check which condition the score 75 satisfies first.
✗ Incorrect
The score 75 is not >= 90 or >= 80, but it is >= 70, so the program prints "Grade: C".
🔧 Debug
advanced2:00remaining
What error does this Java code produce?
This Java code tries to use an if statement but has a mistake. What error will it cause?
Java
public class Main { public static void main(String[] args) { int x = 10; if (x > 5) { System.out.println("x is greater than 5"); } } }
Attempts:
2 left
💡 Hint
Check the syntax of the if statement condition.
✗ Incorrect
In Java, the condition in an if statement must be inside parentheses. Missing them causes a syntax error.
🚀 Application
expert2:30remaining
How many times will the message print?
Consider this Java code that uses a conditional inside a loop. How many times will it print "Number is even"?
Java
public class Main { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { System.out.println("Number is even"); } } } }
Attempts:
2 left
💡 Hint
Count how many numbers from 1 to 10 are even.
✗ Incorrect
The loop runs from 1 to 10. The condition checks if the number is even. Even numbers between 1 and 10 are 2,4,6,8,10 — 5 times.