Java OCA OCP Practice Question 1561

Question

Given the class declaration below,

what expression can be used to fill in the blank to return the size variable defined in the Main class, printing 14 at runtime?

package mypkg;// www. j  a v  a2s  . com
 
final public class Main {
   final private int size = 14;
   final protected class Shape {
      private final int size = 25;
      public final int getSize() {
         return                       ;
      }
   }
   final Shape insert = new Shape();
   final public static void main(String[] feed) {
      System.out.print(new Main().insert.getSize());
   }
}
  • A. Main.this.size
  • B. this.size
  • C. this.Main.size
  • D. The code does not compile, regardless of what is placed in the blank.


A.

Note

While this code included a large number of final modifiers, none of them prevent the code from compiling when a valid expression is placed in the blank, making Option D incorrect.

Option B is incorrect since it returns the size variable defined in the Shape member inner class, not the Main class, printing 25 at runtime.

Option C is incorrect because the expression is invalid and does not compile when inserted into the blank.

Finally, Option A is the correct answer because it compiles, properly references the variable size in the Main class, and prints 14 at runtime.




PreviousNext

Related