Object Oriented Programming MCQs with Answers
Practice important Object Oriented Programming MCQs with answers and explanations.
Chapter: 36
Q561:
What is the result of using the `template< class T, int I >` in C++?
A. Partial specialization with a fixed integer value
B. Complete specialization with a fixed integer value
C. General template with two parameters
D. Partial specialization with any integer value
Correct Answer:
A
Chapter: 36
Q562:
How is a complete specialization different from a partial specialization?
A. Complete specialization fixes all template parameters, while partial specialization only fixes some.
B. Complete specialization allows for multiple parameter types, while partial specialization does not.
C. Partial specialization fixes all template parameters, while complete specialization only fixes some.
D. Complete specialization is not allowed in C++, while partial specialization is.
Correct Answer:
A
Chapter: 36
Q563:
When creating an `Array` class with a non-type parameter for size, which declaration is correct?
A. template< class T, int SIZE > class Array {};
B. template< class T > class Array< SIZE > {};
C. template< class T, int SIZE = 10 > class Array {};
D. template< int SIZE > class Array {};
Correct Answer:
C
Chapter: 36
Q564:
What is the purpose of using `template< class T = int > class Vector { };`?
A. To set a default type for the `Vector` class
B. if no type is specified, it defaults to `int`
C. To specify that `Vector` should only handle integers
D. To create a vector of size 10 by default
Correct Answer:
B
Chapter: 36
Q565:
The `Vector< char* >` class is an example of ____.
A. Partial specialization
B. Complete specialization
C. Function template specialization
D. Non-type template parameters
Correct Answer:
B
Chapter: 36
Q566:
Which method is used to compare two pointers to objects of type `T`?
A. `template< typename T > bool isEqual( T x, T y )`
B. `template< typename T > bool isEqual( T* x, T* y )`
C. `template< > bool isEqual< const char* >( const char* x, const char* y )`
D. `template< class T > bool isEqual( T* x, T* y )`
Correct Answer:
B
Chapter: 36
Q567:
When using a default non-type parameter, what happens if no value is provided?
A. The default value specified in the template definition is used.
B. The compiler throws an error for missing parameter.
C. The program uses the size provided at runtime.
D. The template cannot be instantiated without specifying a non-type parameter.
Correct Answer:
A
Chapter: 36
Q568:
Which of the following shows correct default non-type parameter usage?
A. template< class T, int SIZE = 10 > class Array {};
B. template< class T > class Array< SIZE = 10 > {};
C. template< int SIZE > class Array {};
D. template< class T > class Array { int SIZE; };
Correct Answer:
A
Chapter: 37
Q569:
In which order does the compiler search for template specializations?
A. Complete specialization, Partial specialization, General template
B. Partial specialization, Complete specialization, General template
C. General template, Complete specialization, Partial specialization
D. Partial specialization, General template, Complete specialization
Correct Answer:
A
Chapter: 37
Q570:
What does the compiler use first when resolving a template specialization?
A. Partial specialization
B. Complete specialization
C. General template
D. Ordinary function
Correct Answer:
B
Chapter: 37
Q571:
In the given code snippet, which template specialization is used for `Vector<char*>`?
A. Partial specialization
B. General template
C. Complete specialization
D. None of the above
Correct Answer:
C
Chapter: 37
Q572:
Which function template specialization is used for `sort(str)` in the example?
A. Complete specialization for `char*`
B. Partial specialization for `Vector<char*>`
C. General template for `T`
D. Ordinary function sort(char*)
Correct Answer:
A
Chapter: 37
Q573:
Which resolution order does the compiler follow for function templates?
A. Complete specialization, Partial specialization, General template, Ordinary function
B. Ordinary function, Complete specialization, Partial specialization, General template
C. General template, Partial specialization, Complete specialization, Ordinary function
D. Partial specialization, Ordinary function, Complete specialization, General template
Correct Answer:
B
Chapter: 37
Q574:
In the example provided, which class can inherit from a partial specialization?
A. A class template
B. Complete specialization
C. Ordinary class
D. None of the above
Correct Answer:
A
Chapter: 37
Q575:
In the code `template<class T> class A {}; template<class T> class B : public A<T> { ... };`, which class is derived from the class template `A`?
A. A<T>
B. B<T>
C. A class template that is not specified
D. A derived class with less parameters
Correct Answer:
B
Chapter: 37
Q576:
The resolution order for function templates includes which of the following?
A. Partial specialization, General template, Complete specialization, Ordinary function
B. Ordinary function, Complete specialization, General template, Partial specialization
C. General template, Partial specialization, Complete specialization, Ordinary function
D. Complete specialization, Ordinary function, Partial specialization, General template
Correct Answer:
B
Chapter: 37
Q577:
In the example where `B< T* > : public A< T* >`, which class is used for inheritance?
A. A< T* >
B. B< T* >
C. A< int* >
D. B< int* >
Correct Answer:
A
Chapter: 37
Q578:
A function template can be overloaded with which of the following?
A. Partial specialization
B. Complete specialization
C. Ordinary function
D. All of the above
Correct Answer:
D
Chapter: 37
Q579:
When using a template class, the derived class must match the base class in what aspect?
A. Number of template parameters
B. Function signatures
C. Class names
D. Template arguments
Correct Answer:
A
Chapter: 38
Q580:
In C++, when a function or class is declared as a friend of a class template, it becomes a friend of ______.
A. all instantiations of that template class
B. some instantiations of that template class
C. only the specific instantiation that is used
D. none of the instantiations of that template class
Correct Answer:
A