C++ Programming Questions and Answers – Function Objects

This section on C++ programming questions and answers focuses 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<>
View Answer

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

Answer: c
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

Answer: b
Explanation: <functional> header is need to be used with function objects.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <algorithm>
  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
View Answer

Answer: c
Explanation: In this program, We are dividing the first with the second by using function objects.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ funo.cpp
$ a.out
10 20 30

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

advertisement
  1.     #include <iostream> 
  2.     #include <functional>
  3.     #include <algorithm>
  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
View Answer

Answer: d
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:

advertisement
$ g++ funo1.cpp
$ a.out
-3 4 5

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

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <vector>
  4.     #include <algorithm>
  5.     #include <string>
  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
View Answer

Answer: a
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?

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <algorithm>
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         int numbers[] = {1, 2, 3};
  8.         int remainders[5];
  9.         transform ( numbers, numbers + 5, remainders, 
  10.         bind2nd(modulus<int>(), 2) );
  11.         for (int i = 0; i < 5; i++)
  12.             cout << (remainders[i] == 1 ? "odd" : "even") << "\n";
  13.         return 0;
  14.     }

a)

odd
even

b) even
c)

odd 
odd

d)

odd
even
odd
odd
even
View Answer
Answer: d
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?

  1.     #include <iostream>
  2.     #include <functional>
  3.     #include <algorithm>
  4.     using namespace std;
  5.     int main ()
  6.     {
  7.         int numbers[] = {10, -20, -30, 40, -50};
  8.         int cx;
  9.         cx = count_if ( numbers, numbers + 5, bind2nd(less<int>(), 0) );
  10.         cout << cx;
  11.         return 0;
  12.     }

a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
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

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

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

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.