Q. What is the output of the following code?
Code:
int x = 5;
if (x > 10) {
System.out.println(“x is greater than 10”);
} else if (x > 0) {
System.out.println(“x is greater than 0”);
} else {
System.out.println(“x is less than or equal to 0”);
}
β
Correct Answer: (B)
x is greater than 0
Explanation: The if-else statement tests the value of x against each condition in turn. Since x is greater than 0 but not greater than 10, the code for the second condition is executed.