Test Series - cpp

Test Number 46/102

Q: What is a function template?
A. creating a function without having to specify the exact type
B. creating a function with having an exact type
C. creating a function without having blank spaces
D. creating a function without class
Solution: Function template is used to create a function without having to specify the exact type.
Q: Which is used to describe the function using placeholder types?
A. template parameters
B. template type parameters
C. template type
D. type parameters
Solution: During runtime, We can choose the appropriate type for the function and it is called as template type parameters.
Q: Pick out the correct statement.
A. you only need to write one function, and it will work with many different types
B. it will take a long time to execute
C. duplicate code is increased
D. it will take a long time to execute & duplicate code is increased
Solution: Because of template type parameters, It will work with many types and saves a lot of time.
Q: What can be passed by non-type template parameters during compile time?
A. int
B. float
C. constant expression
D. string
Solution: Non-type template parameters provide the ability to pass a constant expression at compile time. The constant expression may also be an address of a function, object or static class member.
Q: From where does the template class derived?
A. regular non-templated C++ class
B. templated class
C. regular non-templated C++ class or templated class
D. main function
Solution: Class derived template is derived from regular non-templated C++ class or templated class.
Q: 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
Solution: 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: In how many ways templates concept can be used?
A. 1
B. 2
C. 3
D. 4
Solution: Template concept can be used in two different ways. They are function templates used with functions and class templates used with classes.
Q: 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
Solution: 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: Templates simulate which of the following feature?
A. Polymorphism
B. Abstraction
C. Encapsulation
D. Inheritance
Solution: 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: Which keyword is used for the template?
A. Template
B. template
C. Temp
D. temp
Solution: C++ uses template reserved keyword for defining templates.

You Have Score    /10