Free CPP Exam Braindumps (page: 29)

Page 28 of 58

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

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Compare {
bool operator ()(int a) {
if (a >5) return true;
return false;
}
};
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(), Compare());
cout<< number<<endl;
return 0;
}

Program outputs:

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

Answer(s): E



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

#include <deque>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
class B { int val;
public:
B(int v):val(v){}
int getV() const {return val;} bool operator < (const B & v) const { return val<v.val;} };
ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;}
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
int main() {
int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
deque<B> d1(t, t+10);
sort(d1.begin(), d1.end());
set<B> s1(t,t+10);
cout<<binary_search(s1.begin(),s1.end(), 4)<<" "<<binary_search(d1.begin(),d1.end(), 4)<<endl;
return 0;
}

Program outputs:

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

Answer(s): E



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

#include <vector>
#include <iostream>
using namespace std;
class A
{
int a,b;
public:
A(const A & c) { a = c.a; }
A():a(0),b(0){}
void setA(int a) {this?>a = a;} void setB(int b) {this?>b = b;}
int getA() {return a;} int getB() {return b;}
};

int main ()
{
vector<A> v;
A a;
a.setA(10); a.setB(11);
v.push_back(a);
cout<<v[0].getB()<<" "<<v[0].getA()<<endl;
return 0;
}

  1. program outputs 10 11
  2. the result is unpredictable
  3. program outputs 10 0
  4. program outputs 11 0
  5. compilation error

Answer(s): B



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(Add(),1));
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






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