Java OCA OCP Practice Question 2368

Question

Given the following statements, choose the corresponding code implementation:

  • Drawing HAS-A Shape.
  • Shape IS-A Cone.
  • Cone HAS-A Drawing.
  • Dot IS-A Shape.
a  class Drawing { String Shape; }
   class Cone { String Drawing; }
   class Shape extends Cone {}
   class Dot extends Shape {}

b  class Drawing {Shape Shape;}
   class Cone {Drawing Drawing;}
   class Shape extends Cone {}
   class Dot extends Shape {}

c  class Drawing {Shape aVar;}
   class Cone {Drawing age;}
   class Shape extends Cone {}
   class Dot extends Shape {}

d  class Drawing {Shape var;}
   interface Cone {Drawing a;}
   interface Shape implements Cone {}
   interface Dot implements Shape {}


b, c

Note

An IS-A or a HAS-A relationship is defined between the types of the variables, and not their names.

Option (a) is incorrect.

Class Drawing HAS-A String and not Shape.

Option (b) is correct.

Class Drawing defines a variable Shape of type Shape.

So Drawing HAS-A Shape.

It's acceptable to define a variable with the name of its class.

Class Cone defines a variable Drawing of type Drawing.

So it satisfies the relationship Cone HAS-A Drawing.

Class Shape extends class Cone, so it satisfies Shape IS-A Cone.

Class Dot extends class Shape.

So it satisfies Dot IS-A Shape.

Option (c) is also correct.

Class Drawing defines a variable aVar of type Shape.

So Drawing HAS-A Shape.

Class Cone defines a variable age of type Drawing.

So it satisfies the relationship Cone HAS-A Drawing.

Class Shape extends class Cone, so it satisfies Shape IS-A Cone.

Class Dot extends class Shape.

So it satisfies Dot IS-A Shape.

Option (d) is incorrect.

An interface can't implement another interface.

It can only extend it.




PreviousNext

Related