CPP

CPP practice questions, page 6 of 9, with answer review and explanations.

Question 26

What happens when you attempt to compile and run the following code? #include #include #include using namespace std; class B { int val; public: B(int v):val(v){} int getV() const {return val;} bool operator struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out d1(t, t+10); sort(d1.begin(), d1.end()); deque ::iterator it = upper_bound(d1.begin(), d1.end(), B(4)); for_each(it, d1.end(), Out (cout)); cout<<endl; return 0; } Program outputs:

  1. 5 6 7 8 9 10
  2. 4 5 6 7 8 9 10
  3. 6 7 8 9 10
  4. 1 2 3 4 5
  5. 1 2 3 4

Question 27

What happens when you attempt to compile and run the following code? #include #include #include using namespace std; class B { int val; public: B(int v):val(v){} int getV() const {return val;} bool operator struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out v1(10,0); sort(t1, t1+5); sort(t2, t2+5); set_symmetric_difference(t2,t2+5,t1,t1+5,v1.begin()); for_each(v1.begin(), v1.end(), Out (cout));cout<<endl; return 0; } Program outputs:

  1. 6 8 3 4 0 0 0 0 0 0
  2. 3 4 0 0 0 0 0 0 0 0
  3. 6 8 0 0 0 0 0 0 0 0
  4. compilation error
  5. 3 4 6 8 0 0 0 0 0 0

Question 28

What happens when you attempt to compile and run the following code? #include #include #include #include using namespace std; template struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out v1(t, t+10); for_each(v1.begin(), v1.end(), bind2nd(plus (), 1)); for_each(v1.rbegin(), v1.rend(), Out (cout));cout<<endl; return 0; } Program outputs:

  1. 3 2 4 1 5 6 10 8 7 9
  2. 4 3 5 2 6 7 11 9 8 10
  3. 9 7 8 10 6 5 1 4 2 3
  4. 10 8 9 11 7 6 2 5 3 4
  5. compilation error

Question 29

What happens when you attempt to compile and run the following code? #include #include #include using namespace std; class B { int val; public: B(int v=0):val(v){} int getV() const {return val;} operator int () const { return val;} }; template struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out v1(t, t+10); vector v2(10); transform(v1.begin(), v1.end(), v2.begin(), bind1st(1,Add())); for_each(v2.rbegin(), v2.rend(), Out (cout));cout<<endl; return 0; } Program outputs:

  1. 1 2 3 4 5 6 7 8 9 10
  2. 2 3 4 5 6 7 8 9 10 11
  3. 10 9 8 7 6 5 4 3 2 1
  4. 11 10 9 8 7 6 5 4 3 2
  5. compilation error

Question 30

What happens when you attempt to compile and run the following code? #include #include #include using namespace std; int main () { int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5}; vector v (t,t+15); int number = count(v.begin(), v.end(), 2); cout<< number<<endl; return 0; } Program outputs:

  1. 4
  2. 3
  3. 2
  4. 0
  5. compilation error