Object Oriented Programming MCQs with Answers
Practice important Object Oriented Programming MCQs with answers and explanations.
Multiple Choice Questions
Q541: In the Complex class example, why does Complex< float > fc(0, 0); Complex< double > dc = fc; result in an error?
- A: The constructor is not defined for double type
- B: There is no copy constructor defined
- C: The copy constructor only works for the same template type
- D: The class is not properly templated
View Answer
C
Q542: What does the template<typename T> class Complex represent?
- A: A class template for different data types
- B: A single class for one data type
- C: A function template for complex operations
- D: A type of class specialization
View Answer
A
Q543: How can you allow a Complex class to copy from a different data type?
- A: By using a template constructor
- B: By defining a non-templated constructor
- C: By using explicit specialization
- D: By using a copy constructor with two template parameters
View Answer
D
Q544: What is the result of the following statement: Complex< float > fc(0, 0); Complex< double > dc = fc; when using a template constructor?
- A: It will compile successfully
- B: It will result in a compilation error
- C: It will cause runtime error
- D: It will not compile but give a warning
View Answer
B
Q545: Fill in the blank: A template function that is used to handle different types of containers must support the _______ and _______ operators.
- A: Increment, Comparison
- B: Addition, Subtraction
- C: Multiplication, Division
- D: Increment, Dereference
View Answer
D
Q546: What type of copy constructor is used to handle different data types in the Complex class?
- A: A non-templated copy constructor
- B: A templated copy constructor with two parameters
- C: A default copy constructor
- D: A copy constructor with static parameters
View Answer
B
Q547: What is the main advantage of using explicit template functions in class templates?
- A: They reduce code bloat by generating only required instances
- B: They make the code more readable
- C: They simplify memory management
- D: They increase the execution speed
View Answer
A
Q548: How does the Vector<char*> class handle memory management differently from Vector<int>?
- A: It uses shallow copy and needs explicit memory management
- B: It uses deep copy automatically
- C: It does not handle memory management
- D: It uses static memory allocation
View Answer
A
Q549: What does the insert function do in the specialized Vector<char*> class?
- A: It replaces existing values at a specific index with new values
- B: It adds new values at the end of the vector
- C: It removes values from the vector
- D: It sorts the values in the vector
View Answer
A
Q550: In the template Vector<char*>, what is the effect of shallow copy?
- A: It copies pointers but not the actual data, leading to potential issues
- B: It copies both pointers and data correctly
- C: It removes pointers and retains only data
- D: It changes the data type of pointers
View Answer
A