If Else in Java MCQs Exercise II

Ques 1 If Else


What happens if the condition in an if statement is false?

A The code inside the if block executes
B The code inside the else block executes
C The code inside both if and else blocks executes
D The code inside neither if nor else blocks executes

Ques 2 If Else


How many else blocks can be associated with a single if statement?

A Zero
B One
C Multiple
D It depends on the condition

Ques 3 If Else


Which of the following is the correct way to nest if-else statements in Java?

A if (condition1) { if (condition2) { // code block } }
B if (condition1) { // code block } else { if (condition2) { // code block } }
C if (condition1) { else if (condition2) { // code block } }
D if (condition1) { // code block } elseif (condition2) { // code block }

Ques 4 If Else


What is the output of the following code snippet?

int x = 5;
int y = 10;
if (x > 5) {
   if (y > 10)
      System.out.println("A");
   else
      System.out.println("B");
} else {
   System.out.println("C");
}

A A
B B
C C
D The code will not compile due to an error

x is 5, and the condition x > 5 is false.
Since the outer if condition is false, the else block executes.
Therefore, System.out.println("C"); is executed.

Ques 5 If Else


What is the output of the following code snippet?

int x = 5;
int y = 10;
int z = x > y ? x++ : y++;
System.out.println(z);

A 5
B 6
C 10
D 11