Free CPP Exam Braindumps (page: 27)

Page 26 of 58

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[]={20, 30, 10, 20, 30, 10, 20, 30, 10, 20};
deque<B> d1(t, t+10);
sort(d1.begin(), d1.end());
pair<deque<B> ::iterator, deque<B>::iterator > result = equal_range(d1.begin(), d1.end(), B(20));
for_each(result.first, result.second, Out<B>(cout));cout<<endl;
return 0;
}

Program outputs:

  1. 10 10 10 20 20 20 20 30 30 30
  2. 20 20 20 20
  3. 10 20 20 20 20
  4. 20 20 20 20 30
  5. 10 20 20 20 20 30

Answer(s): B



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

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

int main()
{
int t[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
deque<int> mydeck(t, t+10); list<int> mylist(t,t+10);
queue<int> first;
queue<int> second(mydeck);
queue<int> third(second);
queue<int, list<int> > fourth(mylist);
mylist.clear(); third.clear();
cout<<third.size()<< " "<<mydeck.size()<< endl;
cout<<fourth.size()<< " "<<mylist.size()<<endl;
return 0;
}

  1. program outputs: 10 0
    10 0
  2. program outputs: 0 0
    0 0
  3. program outputs: 10 10
    10 10
  4. program outputs: 10 0
    0 10
  5. compilation error

Answer(s): E



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

#include <iostream>
#include <deque>
#include <list>
#include <queue>
#include <vector>
using namespace std;
class compare {
bool reverse;
public:
compare(bool revparam = false) { reverse = revparam; }
bool operator()(int lhs, int rhs) const {
if (reverse) return (lhs > rhs);
else return (lhs < rhs);
}
};
int main(){
int myints[] = { 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
priority_queue<int, deque<int> > first(myints, myints + 10);
priority_queue<int, vector<int>, compare> second(myints, myints + 10,
compare(false));
while (first.size() > 0){
cout << first.top() << " "; first.pop();
}
while (second.size() > 0) {
cout << second.top() << " "; second.pop();
}
return 0;
}

  1. compilation error
  2. program outputs: 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  3. program outputs: 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9
  4. program outputs: 3 4 2 1 6 5 7 9 8 0 3 4 2 1 6 5 7 9 8 0

Answer(s): B



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<<" "; } };
int main() {
int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
deque<int> d1(t, t+10);
set<int> s1(t,t+10);
cout<<binary_search(s1.begin(),s1.end(), 4)<<" "<<binary_search(d1.begin(),d1.end(), 4)<<endl;
return 0;
}

Choose all possible outputs (all that apply):

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

Answer(s): A,B






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