Object Oriented Programming MCQs with Answers
Practice important Object Oriented Programming MCQs with answers and explanations.
Multiple Choice Questions
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
View Answer
A
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
View Answer
C
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
View Answer
B
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
View Answer
B
Q515: What is the primary purpose of using templates in C++?
- A: Code specialization
- B: Code reusability
- C: Code obfuscation
- D: Code duplication
View Answer
B
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
View Answer
B
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
View Answer
B
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
View Answer
C
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
View Answer
B
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
View Answer
D