What is the purpose of an if–else statement in Java?
An if–else statement lets the program choose between two paths: one if a condition is true, and another if it is false.
Click to reveal answer
beginner
Write the basic syntax of an if–else statement in Java.
if (condition) { // code if true } else { // code if false }
Click to reveal answer
beginner
What happens if the condition in an if statement is false and there is no else block?
The program skips the if block and continues with the next code after it, doing nothing inside the if block.
Click to reveal answer
intermediate
Can you have multiple else if blocks in Java? What is their purpose?
Yes, multiple else if blocks let you check several conditions in order, running the code for the first true condition.
Click to reveal answer
beginner
Explain with an example how if–else helps in decision making in programs.
Example: if (score >= 50) { System.out.println("Pass"); } else { System.out.println("Fail"); } This decides if a student passes or fails based on score.
Click to reveal answer
What will happen if the condition in an if statement is true?
ANothing happens
BThe code inside the if block runs
CThe program stops
DThe code inside the else block runs
✗ Incorrect
If the condition is true, the code inside the if block executes.
Which keyword is used to check an additional condition after an if in Java?
Aelif
Belseif
Celse
Delse if
✗ Incorrect
Java uses 'else if' (two words) to check another condition after an if.
What is the output of this code? int x = 10; if (x > 5) { System.out.println("Yes"); } else { System.out.println("No"); }
AError
BNo
CYes
DNothing
✗ Incorrect
Since 10 is greater than 5, the if block runs and prints 'Yes'.
What happens if no else block is provided and the if condition is false?
AThe program skips the if block and continues
BThe if block runs anyway
CThe program crashes
DThe else block runs
✗ Incorrect
Without else, if the condition is false, the program skips the if block and continues.
Which of these is a correct if–else statement in Java?