Object Oriented Programming MCQs with Answers
Practice important Object Oriented Programming MCQs with answers and explanations.
Chapter: 31
Q501:
What is the result if a derived class object is used to call a function that exists in multiple base classes with the same signature, without explicitly specifying the base class?
A. It will compile successfully
B. It will throw a runtime error
C. It will cause a compilation error
D. It will override the function
Correct Answer:
C
Chapter: 31
Q502:
Fill in the blank: In a situation where a derived class inherits from a base class through multiple paths, using _______ inheritance avoids multiple copies of the base class object.
A. Virtual
B. Public
C. Private
D. Protected
Correct Answer:
A
Chapter: 32
Q503:
What is the main advantage of generic programming?
A. Reusability
B. Performance
C. Complexity
D. Memory usage
Correct Answer:
A
Chapter: 32
Q504:
What keyword is used to define a function template in C++?
A. template
B. function
C. class
D. struct
Correct Answer:
A
Chapter: 32
Q505:
In the function template `template< typename T > void printArray( T* array, int size )`, what does `T` represent?
A. The array size
B. The data type of the array elements
C. The function name
D. The template keyword
Correct Answer:
B
Chapter: 32
Q506:
Which of the following is a valid declaration of a function template in C++?
A. template< class T > void funName( T x );
B. template< void T > funName( T x );
C. template< T class > void funName( T x );
D. template< typename > void funName( x );
Correct Answer:
A
Chapter: 32
Q507:
What is the purpose of the `isEqual` function template?
A. To compare two values of any type
B. To print an array of elements
C. To sort an array of elements
D. To find the maximum value in an array
Correct Answer:
A
Chapter: 32
Q508:
In the function `template< typename T > bool isEqual( T x, T y )`, what happens when it is instantiated with `const char*`?
A. It will compare the addresses of the strings
B. It will compare the lengths of the strings
C. It will use `strcmp` to compare the strings
D. It will return false for any input
Correct Answer:
C
Chapter: 32
Q509:
What modification is needed for `isEqual` to correctly handle `const char*` types?
A. Use `strcmp` for comparison
B. Write a separate function for `const char*`
C. Add `const char*` to the template parameters list
D. Remove the template keyword
Correct Answer:
C
Chapter: 32
Q510:
In generic programming, what is the main drawback of function overloading?
A. It requires writing multiple functions for different types
B. It reduces code reusability
C. It is harder to maintain
D. It increases compile time
Correct Answer:
A
Chapter: 32
Q511:
What does the `getInput< int >()` function do in the provided example?
A. It returns an integer input from the user
B. It initializes an integer variable
C. It returns a string input from the user
D. It prints an integer value
Correct Answer:
A
Chapter: 32
Q512:
The `template< class T > void printArray( T* array, int size )` can be instantiated for which data types?
A. Only int and char
B. Only int, char, and double
C. Any data type
D. Only built-in types
Correct Answer:
C
Chapter: 32
Q513:
Fill in the blank: The `printArray` function template can be used to print arrays of any type by using the generic type ______.
A. T
B. typename
C. class
D. type
Correct Answer:
B
Chapter: 32
Q514:
Fill in the blank: If a function template `getInput()` does not specify the data type explicitly, it will generate a compile-time error when used with ______ data types.
A. int and double
B. all
C. custom types
D. none
Correct Answer:
B
Chapter: 33
Q515:
What is the primary purpose of using templates in C++?
A. Code specialization
B. Code reusability
C. Code obfuscation
D. Code duplication
Correct Answer:
B
Chapter: 33
Q516:
Which type of template allows you to define a general class for different data types?
A. Function Template
B. Class Template
C. Function Overloading
D. Operator Overloading
Correct Answer:
B
Chapter: 33
Q517:
What does the following template function do? template< typename T > T my_cast( U u ) { return (T)u; }
A. Converts T to U
B. Converts U to T
C. Prints the value of U
D. Does nothing
Correct Answer:
B
Chapter: 33
Q518:
Which function template declaration is correct?
A. template< class T > void func( T x );
B. template< typename T > void func( T x );
C. Both A and B
D. None of the above
Correct Answer:
C
Chapter: 33
Q519:
To compare two strings ignoring case sensitivity, which class can be used with the template function compare?
A. CaseSenCmp
B. NonCaseSenCmp
C. Both CaseSenCmp and NonCaseSenCmp
D. None of the above
Correct Answer:
B
Chapter: 33
Q520:
The printArray function template works with which of the following data types?
A. int only
B. char only
C. double only
D. All of the above
Correct Answer:
D