0
0
Javaprogramming~5 mins

If–else statement in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ANothing happens
BThe code inside the if block runs
CThe program stops
DThe code inside the else block runs
Which keyword is used to check an additional condition after an if in Java?
Aelif
Belseif
Celse
Delse 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>}
AError
BNo
CYes
DNothing
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
Which of these is a correct if–else statement in Java?
Aif (x > 5) { System.out.println("Hi"); } else { System.out.println("Bye"); }
Bif x > 5 then { print("Hi"); } else { print("Bye"); }
Cif (x > 5) print("Hi"); else print("Bye");
Dif x > 5 { System.out.println("Hi"); } else { System.out.println("Bye"); }
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.