Object Oriented Programming MCQs with Answers
Practice important Object Oriented Programming MCQs with answers and explanations.
Multiple Choice Questions
Q271: Fill in the blank: In the assignment operator, returning `*this` allows for _______ assignment operations.
- A: multiple
- B: chained
- C: successive
- D: none
View Answer
C
Q272: What issue arises when assigning a string to itself, as shown in `str1 = str1`?
- A: The program crashes due to a syntax error
- B: The program produces unexpected results due to self-assignment
- C: The memory is leaked due to improper handling
- D: The program generates a compilation error
View Answer
B
Q273: How can the self-assignment problem be resolved in the `operator=` function for a class?
- A: By checking if the left-hand side operand is the same as the right-hand side operand
- B: By changing the return type of the function
- C: By removing the copy constructor
- D: By using a static member function
View Answer
A
Q274: What is the purpose of the `if (this != &rhs)` condition in the `operator=` function?
- A: It ensures that the left and right operands are not the same object before performing the assignment
- B: It checks if the right operand is NULL
- C: It verifies the size of both operands
- D: It prevents memory leaks by deallocating old memory
View Answer
A
Q275: What modification is suggested to avoid assignment to sub-expressions like `(str1 = str2) = str3`?
- A: Change the return type to `const String &`
- B: Use a different operator for assignment
- C: Return a new object instead of the current one
- D: Use a non-member function for assignment
View Answer
A
Q276: Which operator is overloaded in the `Complex` class to handle addition with another `Complex` object?
- A: `+=`
- B: `*=`
- C: `-=`
- D: `/=`
View Answer
A
Q277: In the `Complex` class, what does the `operator += (const Complex & rhs)` function do?
- A: It adds the real and imaginary parts of two complex numbers and returns the result
- B: It subtracts the complex number from another
- C: It multiplies the complex number with another
- D: It divides the complex number by another
View Answer
A
Q278: Which of the following is a correct way to overload the `+` operator for a `Complex` class?
- A: Complex operator + (const Complex & a, const Complex & b)
- B: Complex operator + (const Complex & a, double b)
- C: Complex operator + (double a, const Complex & b)
- D: All of the above
View Answer
D
Q279: What is the result of using a friend function for operator overloading?
- A: It increases encapsulation and limits access to private data
- B: It decreases encapsulation and allows access to private data
- C: It prevents the use of private data
- D: It makes the operator functions private
View Answer
B
Q280: Why should the use of friend functions be limited in operator overloading?
- A: They minimize encapsulation, which can lead to data vulnerability and programming bugs
- B: They increase the performance of the program
- C: They make debugging easier
- D: They are necessary for all types of operator overloading
View Answer
A