Free CPP Exam Braindumps
C++ Certified Professional Programmer Questions 66–70 (page: 15)

Updated On: 9-Feb-2026

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

#include <list>
#include <iostream>
using namespace std;
template<class T>
void print(T start, T end) {
while (start != end) {
std::cout << *start << " "; start++;
}
}
int main()
{
int t1[] = { 1, 7, 8, 4, 5 };
list<int> l1(t1, t1 + 5);
int t2[] = { 3, 2, 6, 9, 0 };
list<int> l2(t2, t2 + 5);
l1.sort();
list<int>::iterator it = l2.begin();
it++; it++;
l1.splice(l1.end(),l2, it, l2.end());
print(l1.begin(), l1.end()); cout<<"Size:"<<l1.size()<<" ";
print(l2.begin(), l2.end()); cout<<"Size:"<<l2.size()<<endl;
return 0;
}

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

Answer(s): A



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<<" ";
}
};
struct Sequence {
int start;
Sequence(int start):start(start){}
int operator()() { return 10*(1+(start++ %3)); } };
int main() {
vector<int> v1(10);
vector<int> v2(10);
generate(v1.begin(), v1.end(), Sequence(1));
sort(v1.rbegin(), v1.rend());
unique_copy(v1.begin(),v1.end(), v2.begin());
for_each(v2.begin(), v2.end(), Out<int>(cout) );cout<<endl;
return 0;
}

Program outputs:

  1. 20 30 10 20 30 10 20 30 10 20
  2. 30 20 10 0 0 0 0 0 0 0
  3. 30 0 0 0 0 0 0 0 20 10
  4. compilation error

Answer(s): B



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

#include <iostream>
#include <algorithm>
#include <set>
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; }
operator int() const {return a;}
};

int main () {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
set<A> s (t,t+15);
cout<<equal(s.begin(), s.end(), t)<<endl;

return 0;
}

Program outputs:

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

Answer(s): D



Which method added to class B at the marked spot will allow the code below to compile? Choose all possible solutions.

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class B { int val;
public:
B(int v):val(v){}
int getV() const {return val;}
/* Insert Code Here */
};
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};
vector<B> v1(t, t+10);
sort(v1.begin(), v1.end(), greater<B>());
for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
return 0;
}

  1. bool operator < (const B & v) const { return val<v.val;}
  2. bool operator > (const B & v) const { return val<v.val;}
  3. bool operator > (const B & v) const { return val>v.val;}
  4. bool operator == (const B & v) const { return val==v.val;}
  5. operator int () const { return val; }

Answer(s): B,C,D



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

#include <iostream>
#include <map>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
int main(){
int t[] = { 3, 4, 2, 1, 0, 1, 2, 3, 4, 0 };
vector<int> v(t, t+10);
multimap<int,string> m;
for(vector<int>::iterator i=v.begin(); i!=v.end(); i++) {
stringstream s; s<<*i<<*i; m.insert(pair<int,string>(*i,s.str()));
}
for(multimap<int, string>::iterator i=m.begin();i!= m.end(); i++) {
cout<<*i<<" ";
}
return 0;
}

  1. program outputs: 3 4 2 1 0 1 2 3 4 0
  2. program outputs: 00 11 22 33 44
  3. program outputs: 0 0 1 1 2 2 3 3 4 4
  4. program outputs: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4
  5. compilation error

Answer(s): E



Viewing page 15 of 47
Viewing questions 71 - 75 out of 228 questions


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

AI Tutor AI Tutor 👋 I’m here to help!