Free CPP Exam Braindumps (page: 9)

Page 8 of 58

What happens when you attempt to compile and run the following code?

#include <vector>
#include <iostream>
#include <algorithm>
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<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };

struct Add {
B operator()(B & a, B & b) { return a+b; }};
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
vector<B> v1(t, t+10);
vector<B> v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind1st(1,Add()));
for_each(v2.rbegin(), v2.rend(), Out<B>(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

Answer(s): E



What happens when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
vector<int> 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

Answer(s): A



What happens when you attempt to compile and run the following code?

#include <deque>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; }
};
bool Compare(char a, char b) { return tolower(a) < tolower(b);}
int main() {
char s[]={"qwerty"};
char t1[]={"ert"};
char t2[]={"ERT"};
sort(s, s+6);
cout<<includes(s,s+6, t1,t1+3, Compare)<<" "<<includes(s,s+6, t2,t2+3, Compare)<<endl;
return 0;
}

Program outputs:

  1. 0 0
  2. 0 1
  3. 1 0
  4. 1 1

Answer(s): D



What happens when you attempt to compile and run the following code?

#include <vector>
using namespace std;
int main ()
{
std::vector<int> v1;
v1.push_back(10);
return 0;
}

  1. compilation fails due to error in line 2
  2. compilation fails due to error in line 5
  3. exception is thrown during run time
  4. code compiles and executes successfully

Answer(s): D






Post your Comments and Discuss C++ Institute CPP exam with other Community members:

CPP Exam Discussions & Posts