What is the purpose of the `else` statement in Java?
A
To repeat code blocks B
To handle the false case of an `if` condition C
To declare variables D
To end a method
Analysis & Theory
`else` provides an alternative block of code to run when the `if` condition is false.
Which of the following is the correct syntax for using `else` in Java?
A
else x > 5 { } B
else { } C
else: { } D
else if() { }
Analysis & Theory
Correct syntax is simply `else { // code }` — no condition is required.
What is the output?
`int x = 3; if(x > 5) System.out.println("High"); else System.out.println("Low");`
A
High B
Low C
Error D
Nothing
Analysis & Theory
`x > 5` is false, so the `else` block runs and prints 'Low'.
Can an `else` block exist without a matching `if` in Java?
A
Yes B
Only inside loops C
No D
Only if nested
Analysis & Theory
`else` must directly follow an `if`. It cannot be used alone.
What happens if the `if` condition is true?
A
The `else` block runs B
Both `if` and `else` run C
Only the `if` block runs D
Neither runs
Analysis & Theory
When the `if` condition is true, only the `if` block is executed.
What is the output?
`int a = 10; if(a < 5) System.out.print("A"); else System.out.print("B");`
A
A B
B C
AB D
Error
Analysis & Theory
`a < 5` is false, so the `else` block runs, printing 'B'.
Can we put multiple statements inside an `else` block?
A
No B
Only if separated by semicolons C
Yes, using curly braces D
Only in loops
Analysis & Theory
Multiple statements can be grouped inside `{ }` in the `else` block.
What is the output?
`int x = 0; if(x != 0) System.out.println("Not Zero"); else System.out.println("Zero");`
A
Zero B
Not Zero C
Error D
Nothing
Analysis & Theory
`x != 0` is false (because x = 0), so `else` runs and prints 'Zero'.
Is the `else` block required after every `if`?
A
Yes B
No C
Only if condition is false D
Only for booleans
Analysis & Theory
The `else` block is optional in Java. You can use only `if` if needed.
What will this print?
`int score = 85;
if(score >= 90) System.out.println("A"); else System.out.println("Not A");`
A
A B
Not A C
Error D
85
Analysis & Theory
`score >= 90` is false, so the `else` block runs and prints 'Not A'.