Here is a listing of C++ language programming questions on “Function Objects” along with answers, explanations and/or solutions:
1. What does the function objects implement?
a) operator
b) operator()
c) operand
d) operand<>
View Answer
Explanation: Function objects are objects specifically designed to be used with a syntax similar to that of functions.
2. What are the two advantage of function objects than the function call?
a) It contains a state
b) It is a type
c) It contains a state & It is a type
d) It contains a prototype
View Answer
Explanation: A function object can contain state. The second is that a function object is a type and therefore can be used as a template parameter.
3. Which header is need to be used with function objects?
a) <function>
b) <functional>
c) <funct>
d) <functionstream>
View Answer
Explanation: <functional> header is need to be used with function objects.
4. What will be the output of the following C++ code?
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main ()
{
int first[] = {10, 40, 90};
int second[] = {1, 2, 3};
int results[5];
transform ( first, first + 5, second, results, divides<int>());
for (int i = 0; i < 3; i++)
cout << results[i] << " ";
return 0;
}
a) 10 20
b) 20 30
c) 10 20 30
d) 20 40
View Answer
Explanation: In this program, We are dividing the first with the second by using function objects.
Output:
$ g++ funo.cpp $ a.out 10 20 30
5. What will be the output of the following C++ code?
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main ()
{
int numbers[] = {3, -4, -5};
transform ( numbers, numbers + 3, numbers, negate<int>() );
for (int i = 0; i < 3; i++)
cout << numbers[i] << " ";
}
a) -3
b) 3 4 5
c) 3 -4 5
d) -3 4 5
View Answer
Explanation: In this program, we have passed “numbers + 3” in transform function. The results of the transform function are stored in numbers(3rd parameter in func) array whose size is 3.
Output:
$ g++ funo1.cpp $ a.out -3 4 5
6. What will be the output of the following C++ code?
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main ()
{
vector <string*> numbers;
numbers.push_back ( new string ("one") );
numbers.push_back ( new string ("two") );
numbers.push_back ( new string ("three") );
vector <int> lengths ( numbers.size() );
transform (numbers.begin(), numbers.end(), lengths.begin(),
mem_fun(&string :: length));
for (int i = 0; i < 3; i++)
{
cout << lengths[i];
}
return 0;
}
a) 335
b) 225
c) 334
d) 224
View Answer
Explanation: In this program, We calculated the number of letters in every string by using function objects.
Output:
$ g++ funo2.cpp $ a.out 335
7. What will be the output of the following C++ code?
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main ()
{
int numbers[] = {1, 2, 3};
int remainders[5];
transform ( numbers, numbers + 5, remainders,
bind2nd(modulus<int>(), 2) );
for (int i = 0; i < 5; i++)
cout << (remainders[i] == 1 ? "odd" : "even") << "\n";
return 0;
}
a)
odd even
b) even
c)
odd odd
d)
odd even odd odd evenView Answer
Explanation: Running the program will show above behaviour because we have given the value in for loop as 5 instead of 3.
8. What will be the output of the following C++ code?
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main ()
{
int numbers[] = {10, -20, -30, 40, -50};
int cx;
cx = count_if ( numbers, numbers + 5, bind2nd(less<int>(), 0) );
cout << cx;
return 0;
}
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: In this program, We are calculating the number of negative elements present in the program by using function objects.
Output:
$ g++ funo3.cpp $ a.out 3
9. Which are instances of a class with member function operator() when it is defined?
a) function objects
b) member
c) methods
d) iterators
View Answer
Explanation: Function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a regular function call.
10. How many parameters does a operator() in a function object shoud take?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: In the case of binary function objects, this operator() member function will take two parameters.
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.
- Practice Computer Science MCQs
- Check C++ Books
- Apply for Computer Science Internship
- Apply for C++ Internship
- Practice Programming MCQs