250+ TOP MCQs on Function Objects and Answers

This section on C++ programming questions and answers on “Function Objects”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ programming interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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<>
Answer: b
Clarification: 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
Answer: c
Clarification: 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)
b)
c)
d)
Answer: b
Clarification: header is need to be used with function objects.

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main ()
  6.     {
  7.         int first[] = {10, 40, 90};
  8.         int second[] = {1, 2, 3};
  9.         int results[5];
  10.         transform ( first, first + 5, second, results, divides<int>());
  11.         for (int i = 0; i < 3; i++)
  12.             cout << results[i] << " ";
  13.         return 0;
  14.     }

a) 10 20
b) 20 30
c) 10 20 30
d) 20 40
Answer: c
Clarification: 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?

  1.     #include  
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         int numbers[] = {3, -4, -5};
  8.         transform ( numbers, numbers + 3, numbers, negate<int>() );
  9.         for (int i = 0; i < 3; i++)
  10.             cout << numbers[i] << " ";
  11.     }

a) -3
b) 3 4 5
c) 3 -4 5
d) -3 4 5
Answer: d
Clarification: 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?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     #include 
  5.     #include 
  6.     using namespace std;
  7.     int main () 
  8.     {
  9.         vector <string*> numbers;
  10.         numbers.push_back ( new string ("one") );
  11.         numbers.push_back ( new string ("two") );
  12.         numbers.push_back ( new string ("three") );
  13.         vector <int> lengths ( numbers.size() );
  14.         transform (numbers.begin(), numbers.end(), lengths.begin(), 
  15.         mem_fun(&string :: length));
  16.         for (int i = 0; i < 3; i++) 
  17.         {
  18.             cout << lengths[i];
  19.         }
  20.         return 0;
  21.     }

a) 335
b) 225
c) 334
d) 224
Answer: a
Clarification: In this program, We calculated the number of letters in every string by using function objects.
Output:

$ g++ funo2.cpp
$ a.out
335