250+ TOP MCQs on Functors and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “Functors”.

1. What are functors in C++?
a) Objects of a class which are treated as functions
b) Objects that are used to call the function of other classes
c) Functions that are called using pointer objects
d) Functions that are called only once in a program
Answer: a
Clarification: Functors are objects of a class that are treated like function i.e. they can also be used as function calls.

2. Which of the following operators are overloaded for functors?
a) []
b) ()
c) <<
d) >>
Answer: b
Clarification: () operator is overloaded to use functor property in a C++ program because this is the only operator used for a function call.

3. What is the correct function prototype of () operator overloading?
a) return_type operator(arguments)();
b) return_type operator(arguments);
c) return_type operator()(arguments);
d) return_type operator(Class_name)(arguments);
Answer: c
Clarification: The correct syntax of overloading a () operator in a class is as follows:
return_type operator()(arguments){}

4. Which of the following is correct about Functors?
a) Functors should not be declared outside the main function
b) Overloaded operator () function is not a member of the class
c) Functors should be declared global
d) Functors have a state
Answer: d
Clarification: Functors are objects of a class which also have other members and member functions which can be used to save states of that functors hence functors have a state. Functors can be declared anywhere in a program.

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

#include
using namespace std;
class Print
{
   public:
	void operator()(int a){
		cout<<a<<endl;
	}
};
int main()
{
	Print print;
	print(5);
	return 0;
}

a) 5
b) Compile-time error
c) Run-time error
d) Nothing is printed
Answer: a
Clarification: As we have overloaded the () operator inside the class Print, therefore, the print object is a functor, therefore, can be used as a function which is just printing the value passed as a parameter.

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

#include
using namespace std;
class Add
{
	int x;
   public:
	Add(int x){
		this->x = x;
	}
	int operator()(int a){
		return x+a;
	}
};
int main()
{
	Add add_5(5);
	int a = 5;
	cout<<add_5(a);
	return 0;
}

a) 5
b) 10
c) Error
d) Segmentation fault
Answer: b
Clarification: As add_5 is a functor whose x value is initialized as 5 therefore it adds 5 to the value passed as parameter and returns the sum.

7. Given the below class, what is the correct syntax of declaring a functor that adds 10 to each of the passed argument?

class Add
{
	int x;
   public:
	Add(int x){
		this->x = x;
	}
	int operator()(int a){
		return x+a;
	}
};

a) Add add_10(10);
b) Add add_10(10);
c) Add add_10(10);
d) Add add_10(5);
View Answer

Answer: c
Clarification: Given the above class, we can declare an object of this class, which will be a functor that adds 10 to every value passed to it, like this Add add_10(10); where 10 in bracket shows that the variable 10 is intialized to class member x which will be used to add 10 to every argument.

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

#include
#include 
using namespace std;
template <class T>
class Print
{
   public:
	int operator()(T a){
		cout<<a<<endl;
	}
};
int main()
{
	Print<string> str;
	Print<int> integer;
	int a = 100;
	string s = "Hello ";
	str(s);
	integer(a);
	return 0;
}

a)

100
100

b)

100
Hello 

c)

Hello 
Hello 

d)

Hello 
100

View Answer

Answer: d
Clarification: The program is correct and we have initialized two functors one for printing the int values to command line and other for printing string on console. Therefore the program runs fine.

 
 

9. Which of te following is a built-in example of functors in C++?
a) mltiplication f(a1, a2);
b) add f(a1, a2);
c) subtract f(a1, a2);
d) plus f(a1, a2);
Answer: d
Clarification: plus f(a1, a2); is one of the correct in-built functor available.

10. Which of the following header file is required to use in-bulit functors of C++?
a)
b)
c)
d)
Answer: b
Clarification: header file is required to use the fuctionality of in-built functors provided by C++.

11. What are unary functors?
a) Functors that accepts only one parameter
b) Functors that accepts two parameters
c) Functors that accepts more than one parameters
d) Functors that accepts other than a specific type of parameter
Answer: a
Clarification: Unary functors are those which accepts only one argument as a parameter in a functor.

12. What are binary functors?
a) Functors that accepts only one parameter
b) Functors that accepts more than one parameters
c) Functors that accepts two parameters
d) Functors that accepts other than a specific type of parameter
Answer: c
Clarification: Binary functors are those which accepts two arguments as a parameter in a functor.

13. How many ways are there to use functors?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: There are two ways of using functors one like function calls and other like normal class objects.

14. Which of the following is a logical unary functor?
a) logical_or f;
b) logical_and f;
c) logical_not f;
d) negate f;
Answer: c
Clarification: logical_and and logical_or requires two arguments to act upon whereas negate and logical_not requires only one argument but negate also produces non-logical results like negate of a number, therefore, it is not logical hence logical_not f; is an only logical functor.

15. Which of the following is an arithmetic unary functor?
a) logical_not f;
b) logical_and f;
c) logical_or f;
d) negate f;
Answer: d
Clarification: logical_and and logical_or requires two arguments to act upon whereas negate and logical_not requires only one argument but logical_not produces only logical results, therefore, will not work on arithmetic values whereas negate works with all types of values.

16. What of the following is the equivalent statement for the functor call,

where f is a functor and arg1 and arg2 are the arguments required by the functors?
a) f.call(arg1, arg2);
b) f.operator()(arg1, arg2);
c) f.operator(arg1, arg2);
d) f.operator(arg1, arg2)();
Answer: b
Clarification: f(arg1, arg2) means we are calling the overlaoded () operator method which can be called using object f as follows f.operator()(arg1,arg2); In general, object.operator()(argument_lists);

Leave a Reply

Your email address will not be published. Required fields are marked *