Multiple choice technology programming languages

Which four are true? (Choose four.)

  1. Has-a relationships should never be encapsulated.

  2. Has-a relationships should be implemented using inheritance.

  3. Has-a relationships can be implemented using instance variables.

  4. Is-a relationships can be implemented using the extends keyword.

  5. Is-a relationships can be implemented using the implements keyword.

  6. An array or a collection can be used to implement a one-to-many has-a relationship.

Reveal answer Fill a bubble to check yourself
C,D,E,F Correct answer
Explanation

Has-a relationships (composition/aggregation) represent 'has-a' connections between classes and should use instance variables to hold references - option C is correct. Arrays/collections implement one-to-many has-a relationships - option F is correct. Has-a should NOT use inheritance (that's for is-a) - options A and B are wrong. Is-a relationships represent inheritance and use 'extends' for classes - option D is correct, or 'implements' for interfaces - option E is correct. So C, D, E, F are true.

AI explanation

Has-a relationships (composition/aggregation) are naturally implemented with instance variables, e.g. a Car has an Engine field, and collections/arrays are a common way to model one-to-many has-a relationships (a Garage has many Cars). Is-a relationships are implemented via extends (class inheritance) or implements (interface implementation), not via has-a mechanisms — which is why "has-a via inheritance" and "has-a should never be encapsulated" are false.