OCA Java SE 8 Building Blocks - OCA Mock Question Building Block 4








Question

What is the output of the following program?

     1: public class Main { 
     2:   private String myStringValue; 
     3:   private boolean myBooleanValue; 
     4:   public static void main(String[] args) { 
     5:     Main wb = new Main(); 
     6:     System.out.print("myBooleanValue = " + wb.myBooleanValue); 
     7:     System.out.print(", myStringValue = " + wb.myStringValue); 
     8:   } 
     9: }  

  1. Line 6 generates a compiler error.
  2. Line 7 generates a compiler error.
  3. There is no output.
  4. myBooleanValue = false, myStringValue = null
  5. myBooleanValue = false, myStringValue =
  6. myBooleanValue = null, myStringValue = null




Answer



D.

Note

Boolean fields initialize to false and references initialize to null, so myBooleanValue is false and myStringValue is null.

public class Main { 
  private String myStringValue; 
  private boolean myBooleanValue; 
  public static void main(String[] args) { 
    Main wb = new Main(); 
    System.out.print("myBooleanValue = " + wb.myBooleanValue); 
    System.out.print(", myStringValue = " + wb.myStringValue); 
  } 
}