OCA Java SE 8 Mock Exam - OCA Mock Question 11








Question

What is the result of the following class? (Choose all that apply)

     1: public class Main { 
     2:  private static int $; 
     3:  public static void main(String[] main) { 
     4:    String My_Value; 
     5:    System.out.print($); 
     6:    System.out.print(My_Value); 
     7:  }
     8:} 
  1. Compiler error on line 1.
  2. Compiler error on line 2.
  3. Compiler error on line 4.
  4. Compiler error on line 5.
  5. Compiler error on line 6.
  6. 0 null
  7. null null




Answer



E.

Note

E is correct because local variables require assignment before referencing.

D is incorrect because class and instance variables have default values and allow referencing.

My_Value defaults to a null value.

Options A, B, and C are incorrect because identifiers may begin with a letter, underscore, or dollar sign.

Options F and G are incorrect because the code does not compile.

If My_Value was an instance variable, the code would compile and output 0 null.

The following code changes the My_Value to instance variable.

public class Main { 
  private static int $; 
  private static String My_Value; 
  public static void main(String[] main) { 
    System.out.print($); 
    System.out.print(My_Value); 
  }
} 

The code above generates the following result.