What is the purpose of using `else if` in Java?
A
To repeat a block of code B
To run multiple actions based on different conditions C
To store more variables D
To exit a loop
Analysis & Theory
`else if` lets you check multiple conditions after the initial `if`.
What is the output?
`int x = 10;
if(x < 5) System.out.print("Low"); else if(x < 15) System.out.print("Medium"); else System.out.print("High");`
A
Low B
Medium C
High D
Error
Analysis & Theory
x = 10, so `x < 5` is false, `x < 15` is true → 'Medium' is printed.
Which is the correct structure of an `if-else if-else` block?
A
if () {} else () {} else if () {} B
if {} else if {} else {} C
if () else if () else {} D
if () {} else if () {} else {}
Analysis & Theory
Correct syntax: `if (condition) {}` → `else if (condition) {}` → `else {}`.
How many `else if` statements can be used after an `if`?
A
Only 1 B
Only 2 C
Only 3 D
Unlimited
Analysis & Theory
You can use as many `else if` blocks as needed to check multiple conditions.
What is the output?
`int marks = 75;
if(marks >= 90) System.out.print("A"); else if(marks >= 80) System.out.print("B"); else if(marks >= 70) System.out.print("C"); else System.out.print("D");`
A
A B
B C
C D
D
Analysis & Theory
75 >= 90: false, 75 >= 80: false, 75 >= 70: true → prints 'C'.
What happens if more than one `else if` condition is true?
A
All `else if` blocks run B
Only the first true `else if` runs C
It causes an error D
It skips the `if` block
Analysis & Theory
Only the first `true` condition’s block runs, then the rest are skipped.
What will be printed?
`int num = -5;
if(num > 0) System.out.print("Positive"); else if(num == 0) System.out.print("Zero"); else System.out.print("Negative");`
A
Positive B
Zero C
Negative D
Error
Analysis & Theory
-5 is not > 0, and not == 0, so it goes to `else` → 'Negative'.
Can an `else if` block exist without an `if`?
A
Yes B
No C
Only in a loop D
Only with a `switch`
Analysis & Theory
`else if` must follow an `if`. It cannot be used standalone.
Which condition will be checked if the `if` condition is true?
A
All `else if` conditions B
None of the `else if` or `else` blocks C
Only the last `else` block D
Next `if` block
Analysis & Theory
If the `if` condition is true, all `else if` and `else` blocks are skipped.
What will be the output?
`int day = 2;
if(day == 1) System.out.print("Mon"); else if(day == 2) System.out.print("Tue"); else System.out.print("Other");`
A
Mon B
Tue C
Other D
Error
Analysis & Theory
day == 2 is true → 'Tue' is printed.