Object Oriented Programming MCQs with Answers
Practice important Object Oriented Programming MCQs with answers and explanations.
Multiple Choice Questions
Q221: Which of the following is true about the composition relationship in C++?
- A: The composed object's constructor is called after the composing object's constructor
- B: The composed object's destructor is called before the composing object's destructor
- C: The composing object's destructor is called before the composed object's destructor
- D: The composed object is destroyed first and then the composing object
View Answer
C
Q222: What is the primary purpose of the SetString() method in the String class?
- A: To return a pointer to the string data
- B: To allocate new memory for the string and copy data into it
- C: To delete the old memory and allocate new memory
- D: To get the length of the string
View Answer
B
Q223: In the given code, what does the Student::Student(const Student & s) constructor do?
- A: Copies the name, GPA, and roll number from another Student object
- B: Creates a new Student object without initializing it
- C: Copies only the roll number from another Student object
- D: Creates a new Student object with default values
View Answer
A
Q224: Fill in the blank: In the Student class, the ________ function is responsible for freeing the dynamically allocated memory for the name attribute.
- A: Destructor
- B: Constructor
- C: GetName
- D: SetName
View Answer
A
Q225: Fill in the blank: The method ________ is used in the String class to allocate memory and copy the new string value.
- A: SetString
- B: GetString
- C: DeleteString
- D: AllocateString
View Answer
A
Q226: What is the correct method to get the string value from the String class?
- A: SetString()
- B: GetString()
- C: AllocateString()
- D: DeleteString()
View Answer
B
Q227: Which method in the Student class allows you to set a new name for the student?
- A: SetName()
- B: GetName()
- C: SetRollNo()
- D: GetRollNo()
View Answer
A
Q228: Fill in the blank: The composition relationship in C++ indicates a ________ relationship where one object is a part of another.
- A: has-a
- B: owns
- C: creates
- D: manages
View Answer
A
Q229: In composition, the lifetime of one object is ________ on the lifetime of another.
- A: dependent
- B: independent
- C: optional
- D: uncertain
View Answer
A
Q230: What does the "SetString" method of the `String` class do?
- A: Sets the value of a string
- B: Initializes a new string
- C: Deletes the string
- D: Returns the string
View Answer
A