Java OCA OCP Practice Question 1535

Question

What is the output of the following application?

package mypkg1; //ww w  .java2  s  . co m
  
public class Main { 
   static class Shape {} 
   public static void main(String[] leaves) { 
      int area = 10+5; 
      final class Rectangle extends Shape {  // p1 
         public int getArea() { 
            return area;  // p2 
         } 
      } 
      System.out.print(new Rectangle().getArea()); 
   } 
} 
  • A. 15
  • B. The code does not compile because of line p1.
  • C. The code does not compile because of line p2.
  • D. None of the above


A.

Note

The code compiles without issue and prints 15, making Option A correct and Option D incorrect.

The main() method defines a local class Rectangle that correctly extends Shape, a static nested class, making Option B incorrect.

The method getArea() is permitted to read the variable area, defined in the main() method, since it is effectively final, having a value of 15 when it is defined.

Option C is also incorrect.




PreviousNext

Related