0
0
Javaprogramming~20 mins

If statement in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If Statement Master
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 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");
}
ACompilation error
B5 or less
CGreater than 5
DNo output
Attempts:
2 left
💡 Hint
Check the condition x > 5 and what it means for x = 10.
Predict Output
intermediate
2: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");
}
ANeither is big
BB is big
CA is big
DCompilation error
Attempts:
2 left
💡 Hint
Check the values of a and b and the order of conditions.
Predict Output
advanced
2: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");
}
ANo output
BCondition not met
CCompilation error
DCondition met
Attempts:
2 left
💡 Hint
Remember !flag means flag is false, so !flag is true.
Predict Output
advanced
2: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");
}
ASyntaxError: missing semicolon
BPrints: x is five
CRuntimeException
DNo output
Attempts:
2 left
💡 Hint
Check the line endings carefully.
Predict Output
expert
2: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;
}
A5
B-5
C13
D0
Attempts:
2 left
💡 Hint
Compare a and b and see which condition is true.