Free CPP Exam Braindumps (page: 8)

Page 7 of 58

Which stack initialization (line numbers) are correct? Choose all that apply.

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

int main()
{
deque<int> mydeck;
list<int> mylist;
vector<int> myvector;
stack<int> first; // Line I
stack<int> second(mydeck); // Line II
stack<int> third(second); // Line III
stack<int, list<int> > fourth(mylist); // Line IV
stack<int, vector<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,D,E



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){}
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());
deque<B>::iterator it = upper_bound(d1.begin(), d1.end(), B(4));
for_each(it, d1.end(), Out<B>(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

Answer(s): A



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):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() {
B t1[]={3,2,4,1,5};
B t2[]={5,6,8,2,1};
vector<B> 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<B>(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 happens when you attempt to compile and run the following code?

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






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

CPP Exam Discussions & Posts