Java OCA OCP Practice Question 1710

Question

Which lines that are marked will compile in the following code?.

//Filename: A.java
 package mypkg1;//from   w w w.j av  a  2  s. c  o m

 public class A {
  protected int pf;
 }

//Filename: B.java
 package mypkg2;
 import mypkg1.A;

 public class B extends A {
  void action(A obj1, B obj2, C obj3) {
    pf = 10;                // (1)
    obj1.pf = 10;           // (2)
    obj2.pf = 10;           // (3)
    obj3.pf = 10;           // (4)
   }
 }

 class C extends B {
   void action(A obj1, B obj2, C obj3) {
     pf = 10;                // (5)
     obj1.pf = 10;           // (6)
     obj2.pf = 10;           // (7)
     obj3.pf = 10;           // (8)
   }
 }

 class D {
   void action(A obj1, B obj2, C obj3) {
     pf = 10;                // (9)
     obj1.pf = 10;             // (10)
     obj2.pf = 10;             // (11)
     obj3.pf = 10;             // (12)
   }
 }

Select the five correct answers.

  • (a) (1)
  • (b) (2)
  • (c) (3)
  • (d) (4)
  • (e) (5)
  • (f) (6)
  • (g) (7)
  • (h) (8)
  • (i) (9)
  • (j) (10)
  • (k) (11)
  • (l) (12)


(a), (c), (d), (e), and (h)

Note

The lines (1), (3), (4), (5), and (8) will compile.

Keep in mind that a protected member of a superclass is only accessible in a subclass that is in another package, if the member is inherited by an object of the subclass (or by an object of a subclass of this subclass).

This rules out (2), (6), and (7).

The class D does not have any inheritance relationship with any of the other classes, and it does not inherit the field pf.

This rules out the lines from (9) to (12).




PreviousNext

Related