Java OCA OCP Practice Question 3016

Question

Given:

2. interface Shape {  
3.   int size = 7;  
4.   void grow();  
5. }  //from ww  w  .  j  a v  a2 s . c  om
6. class Main implements Shape {  
7.   // static int size = 5;  
8.   // int size = 5;  
9.   public static void main(String[] args) {  
10.     int size = 2;  
11.     new Main().grow();  
12.   }  
13.   public void grow() {  
14.     System.out.println(++size);  
15. } } 

Which are true? (Choose all that apply.)

  • A. As the code stands, the output is 3.
  • B. As the code stands, the output is 8.
  • C. As the code stands, it will NOT compile.
  • D. If line 7 is un-commented, the output is 6.
  • E. If line 8 is un-commented, the output is 6.
  • F. If line 7 is un-commented, the output is 8.
  • G. If line 7 is un-commented, the code will NOT compile.


C, D, and E are correct.

Note

As the code stands, the grow() method will try to increment Shape's size variable, which can't be changed because it's final by default, so the code will not compile.

If either line 7 or 8 is uncommented, then the grow() method will use that line's size variable.




PreviousNext

Related