Java OCA OCP Practice Question 2792

Question

Given:.


class Printer {//from  ww  w  .  j a  v a 2 s  .co  m
   protected int print(int x) {
      return x * 2;
   }
}

class Shape {
   void go(Printer m) {
      System.out.print(m.print(21) + " ");
   }
}

public class Main extends Printer {
   public static void main(String[] args) {
      System.out.print(new Main().print(11) + " ");
      new Shape().go(new Main());
      new Shape().go(new Printer());
   }

   int print(int x) {
      return x * 3;
   }
}

What is the result? (Choose all that apply.)

  • A. 22 42 42
  • B. 22 63 63
  • C. 33 42 42
  • D. 33 63 42
  • E. Compilation fails.
  • F. An exception is thrown at runtime.


E is correct.

Note

Main.print() will not compile because it's attempting to override using a weaker access modifier.




PreviousNext

Related