Free CPP Exam Braindumps (page: 30)

Page 29 of 58

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

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };

int main() {
int t1[]={3,2,4,1,5};
int t2[]={5,6,8,2,1};
vector<int> v1(10);
sort(t1, t1+5);
sort(t2, t2+5);
set_symmetric_difference(t1,t1+5,t2,t2+5,v1.begin());
for_each(v1.begin(), v1.end(), Out<int>(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

Answer(s): E



What will happen when you attempt to compile and run the code below, assuming that file test.out do not exist before the program execution?

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;

template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) {out<<val<<" "; } };

int main (){
int t[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
fstream f("test.out");
list<int> l(t, t+10);
for_each(l.begin(), l.end(), Out<int>(f));
f.close();
return 0;
}

  1. file test.out will be created and opened for writing
  2. file test.out will be created and opened for reading
  3. no file will be created nor opened
  4. file test.out will contain sequence 1 2 3 4 5 6 7 8 9 10
  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==(const A & b) const { return a == b.a; }
};
bool compare(const A & a, const A & b) { return a == b; }
int main () {
int t[] = {1,2,3,3,5,1,2,4,4,5};
vector<A> v (t,t+10);
vector<A>::iterator it = v.begin();

while ( (it = adjacent_find (it, v.end(), compare)) != v.end()) {
cout<<it?v.begin()<<" ";it++;
}
cout<< endl;
return 0;
}

  1. program outputs: 2 3
  2. program outputs: 2 7
  3. program outputs: 3 8
  4. compilation error
  5. program will run forever

Answer(s): B



Which lines of the code below contain proper instantiation of queue objects?

#include <iostream>
#include <deque>
#include <list>
#include <queue>
#include <vector>
using namespace std;

int main()
{
deque<int> mydeck;
list<int> mylist;
vector<int> myvector;
queue<int> first; // line I
queue<int> second(mydeck); // line II
queue<int> third(second); // line III
queue<int> fourth(mylist); // line IV
queue<int> fifth(myvector); // line V
return 0;
}

  1. line I
  2. line II
  3. line III
  4. line IV
  5. line V

Answer(s): A,B,C






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