Object Oriented Programming MCQs with Answers
Practice important Object Oriented Programming MCQs with answers and explanations.
Chapter: 17
Q261:
What does the `Complex operator + (const double& rhs)` function do?
A. It adds a double value to a complex number and returns the result
B. It adds two complex numbers
C. It subtracts a double value from a complex number
D. It multiplies a complex number by a double value
Correct Answer:
A
Chapter: 17
Q262:
What is the issue with the statement `Complex c5 = 450.120 + c1` when adding a double to a complex number?
A. The `+` operator is called with reference to the double value
B. The `+` operator is not overloaded for the double type
C. The `+` operator cannot handle complex number arguments
D. The `+` operator results in a compilation error
Correct Answer:
A
Chapter: 17
Q263:
How can adding a complex number to a real number (or vice versa) be handled in C++?
A. By overloading the `+` operator as a non-member function
B. By using a member function only
C. By using a static function
D. By using a global function
Correct Answer:
A
Chapter: 17
Q264:
What is the main benefit of overloading the assignment operator in classes with dynamic memory?
A. It prevents memory leaks and dangling pointers
B. It allows multiple assignments in a single statement
C. It optimizes performance
D. It changes the data type of the assigned values
Correct Answer:
A
Chapter: 17
Q265:
In the context of operator overloading, what does the term "deep copy" refer to?
A. Copying the value of members individually
B. Creating a new object for each assignment
C. Performing a shallow copy of the object
D. Assigning the same reference to multiple objects
Correct Answer:
A
Chapter: 17
Q266:
What problem occurs with the statement `str1 = str2 = str3` when the assignment operator is void?
A. The assignment is executed in reverse order, leading to errors
B. The void return type causes issues with chaining assignments
C. The assignment operator cannot handle multiple assignments
D. The void return type causes compilation errors for all assignments
Correct Answer:
B
Chapter: 17
Q267:
To resolve the issue with chaining assignments, what change should be made to the assignment operator?
A. It should return a reference to the current object
B. It should return a new object
C. It should handle multiple types
D. It should not use the `void` return type
Correct Answer:
A
Chapter: 17
Q268:
Fill in the blank: The `operator+` function for a complex number and a double can be implemented as a _______ function to handle operations involving basic data types.
A. friend
B. member
C. static
D. global
Correct Answer:
A
Chapter: 17
Q269:
What type of operator function is needed to add a complex number and a double value using a friend function?
A. Binary operator function
B. Unary operator function
C. Ternary operator function
D. Static operator function
Correct Answer:
A
Chapter: 17
Q270:
In the modified assignment operator code, what is the purpose of the `delete [] bufferPtr` statement?
A. It resolves inaccessible memory issues by deallocating old memory
B. It initializes the buffer pointer to NULL
C. It allocates new memory for the buffer
D. It copies the old buffer to the new buffer
Correct Answer:
A
Chapter: 17
Q271:
Fill in the blank: In the assignment operator, returning `*this` allows for _______ assignment operations.
A. multiple
B. chained
C. successive
D. none
Correct Answer:
C
Chapter: 18
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
Correct Answer:
B
Chapter: 18
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
Correct Answer:
A
Chapter: 18
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
Correct Answer:
A
Chapter: 18
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
Correct Answer:
A
Chapter: 18
Q276:
Which operator is overloaded in the `Complex` class to handle addition with another `Complex` object?
A. `+=`
B. `*=`
C. `-=`
D. `/=`
Correct Answer:
A
Chapter: 18
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
Correct Answer:
A
Chapter: 18
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
Correct Answer:
D
Chapter: 18
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
Correct Answer:
B
Chapter: 18
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
Correct Answer:
A