Java OCA OCP Practice Question 2911

Question

Given:

2. class Shape {  
3.   public class Rectangle {   
4.     static int count = 0;  
5.     void go() { System.out.print(" pump " + ++count); }  
6.   }  //from  w  ww .j  a  v a  2s.  com
7.   public Rectangle getRectangle() { return new Rectangle(); }  
8. }  
9. public class Main {  
10.   public static void main(String[] args) {  
11.     Shape e = new Shape();  
12.     // Shape.Rectangle p = e.getRectangle();  
13.     e.Rectangle p = e.getRectangle();  
14.     p.go();  p.go();  
15. } } 

In order for the code to compile and produce the output " pump 1 pump 2", which are true? (Choose all that apply.).

  • A. The code is correct as it stands.
  • B. Line 4 must be changed. count can't be declared "static"
  • C. Line 12 must be un-commented, and line 13 must be removed.
  • D. Somewhere in the code, a second instance of Rectangle must be instantiated.
  • E. There are errors in the code that must be fixed, outside of lines 4, 12, and 13.


B and C are correct.

Note

Regular inner classes cannot have static declarations, and line 12 (not line 13), uses the correct syntax to create an inner class instance from outside the enclosing class.

The rest of the code is correct.




PreviousNext

Related