C++ Programming Questions and Answers – Function Templates – 2

«
»

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Function Templates – 2”.

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
View Answer

Answer: a
Explanation: 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.
advertisement

2. In how many ways templates concept can be used?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: b
Explanation: Template concept can be used in two different ways. They are function templates used with functions and class templates used with classes.

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
View Answer

Answer: b
Explanation: 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.

4. Templates simulate which of the following feature?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Inheritance
View Answer

Answer: a
Explanation: 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.

5. Which keyword is used for the template?
a) Template
b) template
c) Temp
d) temp
View Answer

Answer: b
Explanation: C++ uses template reserved keyword for defining templates.

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;}
View Answer

Answer: a
Explanation: Starts with keyword template and then <class VAR>, then use VAR as type anywhere in the function below.

7. What does this template function indicates?

advertisement
==================
template<class T>
void func(T a)
{
	cout<<a;
}
==================

a) A function taking a single generic parameter and returning a generic type
b) A function taking a single generic parameter and returning nothing
c) A function taking single int parameter and returning a generic type
d) A function taking a single generic parameter and returning a specific non-void type
View Answer

Answer: b
Explanation: As the return type of function is void therefore function is not returning anything. Now as the function is taking a template T as its argument which is a general type, therefore, it is accepting a single general type argument.

8. What does this template function indicates?

==================
template<class T>
T func(T a)
{
	cout<<a;
}
==================

a) A function taking a single generic parameter and returning a generic type
b) A function taking a single generic parameter and returning nothing
c) A function taking single int parameter and returning a generic type
d) A function taking a single generic parameter and returning a specific non-void type
View Answer

Answer: a
Explanation: As the return type of function is template T, therefore, the function is returning a general type. Now as the function is taking a template T as its argument which is a general type, therefore, it is accepting a single general type argument.

9. What does this template function indicates?

==================
template<class T, class U>
U func(T a, U b)
{
	cout<<a<<"\t"<<b;
}
==================

a) A function taking a single generic parameter and returning a generic type which may be different from argument type
b) A function taking a single generic parameter and returning a generic type which must be different from argument type
c) A function taking a single generic parameter and returning a generic type which must have the same type as argument type
d) A function taking a single generic parameter and returning a specific non-void type
View Answer

Answer: a
Explanation: As the return type of function is template U, therefore, the function is returning a general type. Now as the function is taking a template T as its argument which is a general type, therefore, it is accepting a single general type argument. But as U and T are different therefore return type and argument type may be the same or different. Same if U and T both have the same type and different if both have different types.
advertisement

10. What will be the output of the following C++ code?

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
template<class T>
T func(T a)
{
	cout<<a;
	return a;
}
 
template<class U>
void func(U a)
{
	cout<<a;
}
 
int main(int argc, char const *argv[])
{
	int a = 5;
	int b = func(a);
	return 0;
}

a) 5
b) 55
c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: Template function cannot be overloaded as done in this program. They can be overloaded with normal functions or other templates.

11. What will be the output of the following C++ code?

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
template<class T>
T func(T a)
{
	cout<<a;
	return a;
}
 
int func(int a)
{
	cout<<a;
}
 
int main(int argc, char const *argv[])
{
	int a = 5;
	int b = func(a);
	float c = func(5.5);
	return 0;
}

a) 5
b) 55.5
c) Error
d) Segmentation fault
View Answer

Answer: b
Explanation: C++ allows to overload template functions with normal functions therefore the program does not gives any error. When program is executed then int b = func(a); calls the normal function whereas float c = func(5.5) calls the template function.

12. What will be the output of the following C++ code?

advertisement
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
template<class T>
T func(T a)
{
	return a;
}
 
template<class T, class U>
T func(U a)
{
	return (T)a;
}
 
int main(int argc, char const *argv[])
{
	int a = 5;
	int b = func(a);
	int c = func(5.5);
	cout<<b<<c<<endl;
	return 0;
}

a) 5
b) 55
c) Error
d) Segmentation fault
View Answer

Answer: b
Explanation: C++ allows such overlaoding of template functions therefore the program does not gives any error. The first template function returns the same parameter, whereas second template function returns type casted value.

13. Write a template function which takes two numbers, can be integers/real, and an operator as a character ‘+’/’-‘ or as boolean values true/false(representing ‘+’/’-‘ respectively) and returns the results accordingly.
a)

template<class T, class U>
T func(T a, T b, U c)
{
 
	if(c == '+' || c){
		return a+b;
	}
	else if(c == '-' || !c){
		return a-b;
	}
}

b)

template<class T>
T func(T a, T b, T c)
{
	if(c == '+' || c){
		return a+b;
	}
	else if(c == '-' || !c){
		return a-b;
	}
}

c)

template<class T, class U>
U func(T a, T b, U c)
{
	if(c == '+' || !c){
		return a+b;
	}
	else if(c == '-' || c){
		return a-b;
	}
}

d)

template<class T, class U>
U func(T a, T b, U c)
{
	if(c == '+' || c){
		return a+b;
	}
	else if(c == '-' || !c){
		return a-b;
	}
}
View Answer
Answer: a
Explanation: We need two templates one for handling numbers and other for operators. Here T handles numbers and U handles operators. T can take any number type, whereas U can take either char or bool type. The function takes numbers and operators and produces the result. The return type should be a number, therefore, the return type is T.
 
 

14. What type can be used to replace templates from this function?

template<class T, class U>
T func(T a, T b, U c)
{
	if(c == '+' || c){
		return a+b;
	}
	else if(c == '-' || !c){
		return a-b;
	}
}

a) replace templates T, U with auto keyword
b) replace templates T, U with generic keyword
c) replace templates T, U with temp keyword
d) replace templates T, U with GEN_TEMP keyword
View Answer

Answer: a
Explanation: C++ allows use of auto keyword which helps in binding types dynamically. The modified without template version of above program is given below:

========================================
auto func(auto a, auto b, auto c)
{
	if(c == '+' || c){
		return a+b;
	}
	else if(c == '-' || !c){
		return a-b;
	}
}
========================================

15. What is the syntax of an explicit call for a template? Assume the given template function.

template<class T, class U>
void func(T a, U b)
{
	cout<<a<<"\t"<<b<<endl;
}

a) func(3,’a’);
b) func(3,’a’);
c) func(3,’a’);
d) func(3,‘a’);
View Answer

Answer: a
Explanation: This is the correct method of explicit template function call.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn