C++ Programming Questions and Answers – Class Relationships

«
»

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Class Relationships”.

1. What is the class relationship?
a) A relationship between classes that tells how they are related
b) A relationship between classes that tells how much power one class has over other class
c) A relationship between classes that tells which parts of a class is visible to other classes
d) All of the mentioned
View Answer

Answer: d
Explanation: The Class relationship is a concept that helps us in differentiating how one class is related to other classes, the power of one over other and which part one class can be accessed by other class.
advertisement

2. How many types of class relationships are there?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: d
Explanation: There are basically four types of class relationships namely Inheritance, Aggregation, Composition and Association relationships between classes.

3. Which type of relationship is modelled by Inheritance?
a) Is-A relationship
b) Has-A relationship
c) Part-Of relationship
d) Belongs-to relationship
View Answer

Answer: a
Explanation: Inheritance models Is-A type of relationship between classes. This is because in this case derived class inherits all property of the base class and Is-A type of B class.

4. How the relationship is made in Association?
a) Through the objects of classes
b) Through constructor
c) Through destructor
d) Through class Names
View Answer

Answer: a
Explanation: Association relationship between classes is made using the objects of classes like we have a bank object denoting ABC bank and some objects of Employee class XYZ1, XYZ2 and so on. So, in this case, XYZ1 is an employee in ABC bank so there is a relationship between these two objects.

5. How many types of Association can be there between classes?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: d
Explanation: There can be four types of an association relationship between classes namely one-to-one, one-to-many, many-to-one and many-to-many.
advertisement

6. Why do we need relationships between classes?
a) To use the functionality of one class into other
b) To enhance the communication between classes
c) To increase code re-usability
d) All of the mentioned
View Answer

Answer: d
Explanation: Relationships are needed to increase the use of features of one class into the other classes i.e. increasing the re-usability of codes and increasing communication between classes.

7. Composition is also a type of _______________ relationship.
a) Aggregation
b) Association
c) Inheritance
d) Both Aggregation an Association
View Answer

Answer: b
Explanation: Composition is also a type of Both Aggregation and Association relationship. Composition relationships are string relationships whereas others are a superset of this relationship.

8. Which type of relationship is modelled by Composition?
a) Is-A relationship
b) Has-A relationship
c) Part-Of relationship
d) Have-A relationship
View Answer

Answer: c
Explanation: Composition models the part-of relationship between classes. In this children cannot exits without a parent, therefore, they are part of each other.

9. Which type of relationship is modelled by Aggregation?
a) Is-A relationship
b) Has-A relationship
c) Part-Of relationship
d) Have-A relationship
View Answer

Answer: b
Explanation: Aggregation models the has-a relationship between classes. In this children can exist without a parent, therefore, they have a relationship.
advertisement

10. Which of the following relationships is uni-directional?
a) Aggregation
b) Association
c) Composition
d) Both Aggregation and Composition
View Answer

Answer: d
Explanation: Both Association and composition are uni-directional relationships. For example, departments can have students but another way around is not possible.

11. In which of the following relationship objects of related classes can occur independently?
a) Aggregation
b) Association
c) Composition
d) Both Aggregation an Association
View Answer

Answer: d
Explanation: In both Aggregations and Association of objects of related classes can occur independently. For example, an employee can have a bank and a bank can have an employee. Also, Association is a superset of Aggregation, therefore, it also follows the same.

12. In which of the following relationship objects of related classes are strongly dependent?
a) Aggregation
b) Association
c) Composition
d) Both Composition an Association
View Answer

Answer: d
Explanation: In both Composition and Association, objects of related classes are strongly dependent. For example, books will be destroyed if the library does not exist (talking about objects of classes).

13. Composition is a __________ type of Association relationship.
a) strong
b) weak
c) unnecessary
d) necessary
View Answer

Answer: a
Explanation: Composition is a strong type of Association relationship because in this case, objects are strongly dependent on each other. For example, human and heart cannot exist without each other.
advertisement

14. Aggregation is a __________ type of Association relationship.
a) strong
b) weak
c) unnecessary
d) necessary
View Answer

Answer: b
Explanation: Aggregation is a weak type of Association relationship because in this case objects of related classes can occur independently. For example, bank and employee can occur independently.

15. Which type of relationship is modelled by Association?
a) Is-A relationship
b) Has-A relationship
c) Part-Of relationship
d) Have-A relationship
View Answer

Answer: b
Explanation: Association models the has-a relationship between classes. Similar to aggregation, in this children can exist without a parent, therefore, they have a relationship.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn