0
0
Javaprogramming~20 mins

Why conditional statements are needed in Java - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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.");
        }
    }
}
AIt's cool outside.
BCompilation error
CIt's hot outside.
DNo output
Attempts:
2 left
💡 Hint
Check the value of temperature and the condition in the if statement.
🧠 Conceptual
intermediate
1:30remaining
Why do we use conditional statements in programming?
Which of the following best explains why conditional statements are needed in programs?
ATo allow the program to make decisions and choose different actions based on conditions.
BTo repeat a set of instructions multiple times automatically.
CTo store multiple values in a single variable.
DTo define functions that can be reused.
Attempts:
2 left
💡 Hint
Think about how programs can behave differently depending on data or user input.
Predict Output
advanced
2: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");
        }
    }
}
AGrade: C
BGrade: B
CGrade: F
DGrade: A
Attempts:
2 left
💡 Hint
Check which condition the score 75 satisfies first.
🔧 Debug
advanced
2: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");
        }
    }
}
ATypeError: Cannot compare int and boolean
BRuntimeException: Null pointer exception
CNo error, prints 'x is greater than 5'
DSyntaxError: Missing parentheses around condition
Attempts:
2 left
💡 Hint
Check the syntax of the if statement condition.
🚀 Application
expert
2: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");
            }
        }
    }
}
A10
B5
C0
D1
Attempts:
2 left
💡 Hint
Count how many numbers from 1 to 10 are even.