Object Oriented Programming MCQs with Answers
Practice important Object Oriented Programming MCQs with answers and explanations.
Chapter: 14
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
Correct Answer:
C
Chapter: 14
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
Correct Answer:
B
Chapter: 14
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
Correct Answer:
A
Chapter: 14
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
Correct Answer:
A
Chapter: 14
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
Correct Answer:
A
Chapter: 14
Q226:
What is the correct method to get the string value from the String class?
A. SetString()
B. GetString()
C. AllocateString()
D. DeleteString()
Correct Answer:
B
Chapter: 14
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()
Correct Answer:
A
Chapter: 14
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
Correct Answer:
A
Chapter: 15
Q229:
In composition, the lifetime of one object is ________ on the lifetime of another.
A. dependent
B. independent
C. optional
D. uncertain
Correct Answer:
A
Chapter: 15
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
Correct Answer:
A
Chapter: 15
Q231:
In the `Student` class example, which class is used as a data member to represent the student's name?
A. String
B. Char
C. Date
D. Integer
Correct Answer:
A
Chapter: 15
Q232:
The `Student` class uses composition to include which of the following classes?
A. String
B. Date
C. Chair
D. Both A and B
Correct Answer:
D
Chapter: 15
Q233:
What is the output of the constructor call `Student aStudent("Fakhir", 899, 3.1);` in terms of constructor messages?
A. Only "Constructor::Student"
B. Constructor::String followed by "Constructor::Student"
C. Destructor::Student followed by "Destructor::String"
D. None of the above
Correct Answer:
B
Chapter: 15
Q234:
What does the `Student` class destructor do?
A. Deletes the student object
B. Deletes the name object
C. Deletes the date object
D. Deletes both name and date objects
Correct Answer:
B
Chapter: 15
Q235:
In aggregation, objects can exist _______ each other.
A. dependently
B. independently
C. permanently
D. temporarily
Correct Answer:
B
Chapter: 15
Q236:
What is the main difference between composition and aggregation?
A. Lifetime dependency of sub-objects
B. Memory usage
C. Execution speed
D. Compiler support
Correct Answer:
A
Chapter: 15
Q237:
In the `Room` class example, what does the `AddChair` method do?
A. Adds a chair to the room's array of chairs
B. Removes a chair from the room
C. Checks if a chair can be folded
D. Returns a chair from the room
Correct Answer:
A
Chapter: 15
Q238:
Which of the following best describes a friend function?
A. A non-member function that can access private members of a class
B. A member function of a class
C. A constructor function
D. A destructor function
Correct Answer:
A
Chapter: 15
Q239:
The `DoSomething` function is made a friend of class `X`. What can this function now do?
A. Access private members of `X`
B. Access public members of `X`
C. Modify the class `X` definition
D. Instantiate objects of class `X`
Correct Answer:
A
Chapter: 15
Q240:
What keyword is used to declare a friend function in a class?
A. friend
B. public
C. private
D. protected
Correct Answer:
A