Java OCA OCP Practice Question 369

Question

What will the following code print when compiled and run?

class MyClass{ //w w w .j  av a2s . c  o  m
   int x = 10; 
   static int y = 20; 
} 
class MySubClass extends MyClass{ 
   int x = 30; 
   static int y = 40; 
} 

public class Main  { 
   public static void main (String [] args)  { 
     System.out.println(new MySubClass().x+
                       ", "+
                       new MySubClass().y); 
   } 
} 

Select 1 option

  • A. 10, 40
  • B. 30, 20
  • C. 10, 20
  • D. 30, 40
  • E. 20, 30
  • F. Compilation error.


Correct Option is  : D

Note

System.out.println (new MySubClass ().x);

The reference is of type MySubClass and so MySubClass's x will be accessed.




PreviousNext

Related