Java OCA OCP Practice Question 2819

Question

Given:

3. class Shape {  
4.   private static int x = 6;  
5.   public static class MyClass {   
6.     void go() { System.out.print("roll " + x++); }  
7. } }  
8. public class Main {  
9.   public static void main(String[] args) {  
10.     // insert code here  
11. } } 

And the three code fragments:

  • I. new Shape.MyClass().go();
  • II. Shape t = new Shape(); t.MyClass().go();
  • III. Shape.MyClass w = new Shape.MyClass(); w.go();

Assuming we insert a single fragment at line 10, which are true? (Choose all that apply.)

  • A. Once compiled, the output will be "roll 6"
  • B. Once compiled, the output will be "roll 7"
  • C. Fragment I, inserted at line 10, will compile.
  • D. Fragment II, inserted at line 10, will compile.
  • E. Fragment III, inserted at line 10, will compile.
  • F. Taken separately, class Shape will not compile on its own.


A, C, and E are correct.

Note

The weird, static-specific syntax to use so-called "static inner classes" from outside the enclosing class is demonstrated with fragments I and III.




PreviousNext

Related