C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Function Templates – 2”.
Q 1. What are Templates in C++?
A. A feature that allows the programmer to write generic programs
B. A feature that allows the programmer to write specific codes for a problem
C. A feature that allows the programmer to make program modular
D. A feature that does not add any power to the language
Show Answer
Answer:-A. A feature that allows the programmer to write generic programsExplanation
Templates are features in C++ that allows the programmer to write generic programs. for example, making the same function to take different types of arguments and perform the same action on them without specifying the type in the argument list.
Q 2. In how many ways templates concept can be used?
A. 1
B. 2
C. 3
D. 4
Show Answer
Answer:-B. 2Explanation
Template concept can be used in two different ways. They are function templates used with functions and class templates used with classes.
Q 3. What is the difference between normal function and template function?
A. The normal function works with any data types whereas template function works with specific types only
B. Template function works with any data types whereas normal function works with specific types only
C. Unlike a normal function, the template function accepts a single parameter
D. Unlike the template function, the normal function accepts more than one parameters
Show Answer
Answer:-B. Template function works with any data types whereas normal function works with specific types onlyExplanation
As a template feature allows you to write generic programs. therefore a template function works with any type of data whereas normal function works with the specific types mentioned while writing a program. Both normal and template function accepts any number of parameters.Q 4. Templates simulate which of the following feature?
A. Polymorphism
B. Abstraction
C. Encapsulation
D. Inheritance
Show Answer
Answer:-A. PolymorphismExplanation
Template function helps in writing functions that work with different types of parameters which is what polymorphism means i.e. using same function prototype to perform the same operations on different types of parameters.
Q 5. Which keyword is used for the template?
A. Template
B. template
C. Temp
D. temp
Show Answer
Answer:-B. templateExplanation
C++ uses template reserved keyword for defining templates.Q 6. What is the correct syntax of defining function template/template functions?
A. template <class T> void(T a){cout<<a;}
B. Template <class T> void(T a){cout<<a;}
C. template <T> void(T a){cout<<a;}
D. Template <T> void(T a){cout<<a;}
Leave a Reply