This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer
Explanation: 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) >>
View Answer
Explanation: () 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);
View Answer
Explanation: 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
View Answer
Explanation: 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<iostream> 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
View Answer
Explanation: 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<iostream> 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
View Answer
Explanation: 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 8. What will be the output of the following C++ code? a) b) c) d)
b) Add
c) Add add_10(10);
d) Add add_10(5);
View Answer
Explanation: 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.#include<iostream>
#include <string>
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 Sanfoundry";
str(s);
integer(a);
return 0;
}
100
100
100
Hello Sanfoundry
Hello Sanfoundry
Hello Sanfoundry
Hello Sanfoundry
100
View Answer
Explanation: 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<T> f(a1, a2);
b) add<T> f(a1, a2);
c) subtract<T> f(a1, a2);
d) plus<T> f(a1, a2);
View Answer
Explanation: plus<T> 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) <any>
b) <fucntional>
c) <functor>
d) <function>
View Answer
Explanation: <functional> 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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<T> f;
b) logical_and<T> f;
c) logical_not<T> f;
d) negate<T> f;
View Answer
Explanation: 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<T> f; is an only logical functor.
15. Which of the following is an arithmetic unary functor?
a) logical_not<T> f;
b) logical_and<T> f;
c) logical_or<T> f;
d) negate<T> f;
View Answer
Explanation: 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,
x = f(arg1, arg2);
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)();
View Answer
Explanation: 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);
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Apply for C++ Internship
- Practice Computer Science MCQs
- Check Computer Science Books
- Check Programming Books
- Check C++ Books