Function Objects Cpp MCQ

Errorlogger
0
1. What does the function objects implement?
a) operator
b) operator()
c) operand
d) none of the mentioned



2. What are the two advantage of function objects than the function call?
a) It contains a state.
b) It is a type
c) Both a & b
d) None of the mentioned



3. What is the output of this program?
  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) All of the mentioned



4. What is the output of this program?
  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 + 5, 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



5. What is the output of this program?
  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



6. What is the output of this program?
  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) Runtime error



7. What is the output of this program?
  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



8. Which are instances of a class with member function operator() when it is
defined?
a) function objects
b) member
c) methods
d) none of the mentioned



9. How many parameters does a operator() in a function object shoud take?
a) 1
b) 2
c) 3
d) 4

Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(30)

Our website uses cookies to enhance your experience. Check Now
Accept !