CPP

CPP

Function Objects has 8 Questions

What does the function objects implement?

operator
operator()
operand
none of the mentioned
What are the two advantage of function objects than the function call?

It contains a state.
It is a type
Both a & b
None of the mentioned
What is the output of this program?
  1.     #include  
  2.     #include 
  3.     #include 
  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.     }

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

335
225
334
224
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     #include 
  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.     }

odd even
even
odd odd
odd even odd odd even
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     #include 
  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.     }

1
2
3
4
Which are instances of a class with member function operator() when it is defined?

function objects
member
methods
none of the mentioned
How many parameters does a operator() in a function object shoud take?

1
2
3
4