Java OCA OCP Practice Question 1620

Question

What will be the result of compiling and running the following program?.

 public class Main {

   String title;//from w ww .  jav  a  2s .  com
   boolean published;

   static int total;
   static double maxPrice;

  public static void main(String[] args) {
    Main main = new Main();
    double price;
    if (true)
      price = 100.00;
    System.out.println("|" + main.title + "|" + main.published + "|" +
                       Main.total + "|" + Main.maxPrice + "|" + price+ "|");
  }
}

Select the one correct answer.

  • (a) The program will fail to compile.
  • (b) The program will compile, and print |null|false|0|0.0|0.0|, when run.
  • (c) The program will compile, and print |null|true|0|0.0|100.0|, when run.
  • (d) The program will compile, and print | |false|0|0.0|0.0|, when run.
  • (e) The program will compile, and print |null|false|0|0.0|100.0|, when run.


(e)

Note

The program will compile.

The compiler can figure out that the local variable price will be initialized, since the value of the condition in the if statement is true.

The two instance variables and the two static variables are all initialized to the respective default value of their type.




PreviousNext

Related