Free CPP Exam Braindumps (page: 26)

Page 25 of 58

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

#include <iostream>

using namespace std;

template <class T>
class A {
T _v;
public:
A() {}
A(T v): _v(v){}
friend ostream & operator<<(ostream & c, const A<T> & v);
};

template <class T>
ostream & operator<<(ostream & c, const A<T> & v) {
c<<v._v; return c; }

int main()
{
A<int> a(10);
cout<<a<<endl;
return 0;
}

  1. program will display:10
  2. program will not compile
  3. program will compile
  4. program will run without output

Answer(s): B



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

#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
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<<" "; } };

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

Answer(s): C



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

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class A {
int a;
public:
A(int a) : a(a) {}
int getA() const { return a; } void setA(int a) { this?>a = a; }
bool operator==(A & b) { return a == b.a; }
};
struct Compare{
bool operator()(const A & a, const A & b) {return a.getA()==b.getA();};
};
int main () {
int t[] = {1,2,3,4,5,1,2,3,4,5};
vector<A> v (t,t+10);
vector<A>::iterator it;
A m1[] = {A(1), A(2), A(3)};
it = search (v.begin(), v.end(), m1, m1+3, Compare());
cout << "First found at position: " << it?v.begin() << endl;
return 0;
}

Program outputs:

  1. First found at position: 5
  2. First found at position: 0
  3. First found at position: 7
  4. compilation error
  5. First found at position: 10

Answer(s): B



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

#include <deque>
#include <iostream>
#include <algorithm>
using namespace std;
class B { int val;
public:
B(int v):val(v){} B(){}
int getV() const {return 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);
deque<B>::iterator it = lower_bound(d1.begin(), d1.end(), 4);
for_each(it, d1.end(), Out<B>(cout));cout<<endl;
return 0;
}

Program outputs:

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

Answer(s): D






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