Java OCA OCP Practice Question 1760

Question

What will be the result of attempting to compile and run the following class?.

public class Main {
  public static void main(String[] args) {
    if (true)
    if (false)
    System.out.println("a");
    else
    System.out.println("b");
  }
}

Select the one correct answer.

  • (a) The code will fail to compile because the syntax of the if statement is incorrect.
  • (b) The code will fail to compile because the compiler will not be able to determine which if statement the else clause belongs to.
  • (c) The code will compile correctly and display the letter a, when run.
  • (d) The code will compile correctly and display the letter b, when run.
  • (e) The code will compile correctly, but will not display any output.


(d)

Note

The program will display the letter b when run.

The second if statement is evaluated since the boolean expression of the first if statement is true.

The else clause belongs to the second if statement.

Since the boolean expression of the second if statement is false, the if block is skipped and the else clause is executed.




PreviousNext

Related