Free CPP Exam Braindumps
C++ Certified Professional Programmer Questions 46–50 (page: 11)

Updated On: 9-Feb-2026

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};
int t2[]={5,6,8,2,1};
vector<B> v1(10,0);
sort(t1, t1+5);
sort(t2, t2+5);
set_union(t1,t1+5,t2,t2+5,v1.begin());
for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
return 0;
}

Program outputs:

  1. 3 2 4 1 5 6 8 2 1 0
  2. 1 2 3 4 5 6 8 2 1 0
  3. 1 1 2 2 3 4 5 5 6 8
  4. 1 2 3 4 5 6 8 0 0 0
  5. compilation error

Answer(s): E



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, 6, 5, 7, 9, 8, 0 };
vector<int> v(t, t+10);
map<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(map<int, string>::iterator i=m.begin();i!= m.end(); i++) {
cout<<*i<<" ";
}
return 0;
}

  1. program outputs: 3 4 2 1 6 5 7 9 8 0
  2. program outputs: 00 11 22 33 44 55 66 77 88 99
  3. program outputs: 0 1 2 3 4 5 6 7 8 9
  4. program outputs: 0 00 1 11 2 22 3 33 4 44 5 55 6 66 7 77 8 88 9 99
  5. compilation error

Answer(s): E



What will happen when you attempt to compile and run the code below, assuming that file test.in contains the following sequence: 1 2 3?

#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 () {
ifstream f("test.in");
list<int> l;
for( ; !f.fail() ; ) {
int i;
f>>i;
l.push_back(i);
}
f.close();
for_each(l.begin(), l.end(), Out<int>(cout));
return 0;
}

Programwill output:

  1. 1 2 3
  2. 1 2 3 3
  3. no output
  4. compilation error
  5. program runs forever without output

Answer(s): B



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[]={6,10,8,7,9};
vector<int> v1(10);
sort(t1, t1+5); sort(t2, t2+5);
copy(t1,t1+5,v1.begin());
copy(t2,t2+5,v1.begin()+5);
merge(v1.begin(), v1.begin()+5,v1.end());
for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
return 0;
}

Program outputs:

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

Answer(s): E



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

#include <vector>
#include <set>
#include <iostream>
#include <algorithm>

using namespace std;

void print(int v) { cout<<v<<" "; }
struct Sequence {
int start;
Sequence(int start):start(start){}
int operator()() { return start++; }
};
bool predicate(int v) { return v%2==0; }
int main() {
vector<int> v1(10);
generate_n(v1.begin(), 10, Sequence(1));
set<int> s1(v1.begin(), v1.end());
remove_if(s1.begin(), s1.end(), predicate);
for_each(s1.begin(), s1.end(), print);cout<<endl;
return 0;
}

Program outputs:

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

Answer(s): D



Viewing page 11 of 47
Viewing questions 51 - 55 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!