Java OCA OCP Practice Question 1875

Question

Given the following type and reference declarations, which assignment is legal?.

// Type declarations:
interface Interface1 {}
interface Interface2 {}
class MyClass1 implements Interface1 {}
class MyClass2 implements Interface2 {}
class MyClass3 extends MyClass1 implements Interface2 {}

// Reference declarations:
  MyClass1 obj1;//  w  ww . j a  v a  2s.c  o m
  MyClass2 obj2;
  MyClass3 obj3;

Select the one correct answer.

  • (a) obj2 = obj1;
  • (b) obj3 = obj1;
  • (c) obj3 = obj2;
  • (d) Interface1 a = obj2;
  • (e) Interface1 b = obj3;
  • (f) Interface2 c = obj1;


(e)

Note

Only the assignment Interface1 b = obj3 is valid.

The assignment is allowed, since MyClass3 extends MyClass1, which implements Interface1.

The assignment obj2 = obj1 is not legal, since MyClass1 is not a subclass of MyClass2.

The assignments obj3 = obj1 and obj3 = obj2 are not legal, since neither MyClass1 nor MyClass2 is a subclass of MyClass3.

The assignment Interface1 a = obj2 is not legal, since MyClass2 does not implement Interface1.

Assignment Interface2 c = obj1 is not legal, since MyClass1 does not implement Interface2.




PreviousNext

Related