Recall & Review
beginner
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) {<br> // code if true<br>} else {<br> // code if false<br>}
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) {<br> System.out.println("Pass");<br>} else {<br> System.out.println("Fail");<br>}<br>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?
✗ 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?
✗ Incorrect
Java uses 'else if' (two words) to check another condition after an if.
What is the output of this code?<br>int x = 10;<br>if (x > 5) {<br> System.out.println("Yes");<br>} else {<br> System.out.println("No");<br>}
✗ 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?
✗ 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?
✗ Incorrect
Java requires parentheses around the condition and braces for blocks.
Explain how an if–else statement controls the flow of a Java program.
Think about how the program chooses which code to run.
You got /4 concepts.
Write a simple Java if–else statement that prints 'Adult' if age is 18 or more, otherwise prints 'Minor'.
Use comparison and print statements.
You got /5 concepts.