Free CPP Exam Braindumps
C++ Certified Professional Programmer Questions 41–45 (page: 10)

Updated On: 9-Feb-2026

Which pieces of code inserted independently into places marked 1 and 2 will cause the program to compile and display: 0 1 2 3 4 5 6 7 8 9? Choose all that apply.

#include <list>
#include <iostream>
using namespace std;
class A { int a; public:
A(int a){ this?>a=a;}
//insert code here 1
};
//insert code here 2
template<class T> void print(T start, T end) {
while (start != end) {
std::cout << *start << " "; start++;
}
}
int main() {
A t1[] = { 1, 7, 8, 4, 5 }; list<A> l1(t1, t1 + 5);
A t2[] = { 3, 2, 6, 9, 0 }; list<A> l2(t2, t2 + 5);
l1.sort(); l2.sort(); l1.merge(l2);
print(l1.begin(), l1.end());
print(l2.begin(), l2.end()); cout<<endl;
return 0;
}

  1. place 1: operator int() { return a; }
  2. place 1: operator int() { return a; }
    bool operator < (const A & b) { return this?>a< b.a;}
  3. place 1: bool operator < (const A & b) { return this?>a< b.a;}
  4. place 1: bool operator < (const A & b) { return this?>a< b.a;}
    friend ostream & operator <<(ostream & c, const A & a);
    place 2: ostream & operator <<(ostream & c, const A & a) { c<<a.a; return c;}
  5. place 1: bool operator < (const A & b) { return this?>a< b.a;}
    place 2: ostream & operator <<(ostream & c, const A & a) { c<<a.a; return c;}

Answer(s): A,B,D



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

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
int t[] = {1,2,3,4,5,1,2,3,4,5};
vector<int> v (t,t+10);
vector<int>::iterator it;
int m1[] = {1, 2, 3};
it = search (v.begin(), v.end(), m1, m1+3);
cout << "found at position: " << it?v.begin() << endl;
return 0;
}

Program outputs:

  1. found at position: 5
  2. found at position: 0
  3. found at position: 6
  4. found at position: 1
  5. found at position: 10

Answer(s): B



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

#include <deque>
#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() {
deque<int> d1(10);
generate(d1.begin(), d1.end(), Sequence(1));
sort(d1.begin(), d1.end());
pair<deque<int>::iterator, deque<int>::iterator > result = equal_range(d1.begin(), d1.end(), 20);
for_each(result.first, result.second, Out<int>(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



Which changes, introduced independently, will allow the code to compile and display “one” “eight” “nine” “ten”? Choose all that apply

#include <iostream>
#include <map>
#include <string>
using namespace std;
class A {
int a;
public:
A(int a):a(a){}
int getA() const { return a;}
/* Insert Code Here 1 */
};
/* Insert Code Here 2 */

int main(){
int t[] = { 3, 4, 2, 1, 6, 5, 7, 9, 8, 10 };
string s[] = {"three", "four", "two", "one", "six","five", "seven", "nine","eight","ten"};
map<A, string> m; /* Replace Code Here 3 */
for(int i=0; i<10; i++) {
m.insert(pair<A,string>(A(t[i]),s[i]));
}
m.erase(m.lower_bound(2),m.upper_bound(7));
map<A, string>::iterator i=m.begin(); /* Replace Code Here 4 */
for( ;i!= m.end(); i++) {
cout<<i?>second<<" ";
}
cout<<endl;
return 0;
}

  1. operator int() const { return a;} inserted at Place 1
  2. bool operator < (const A & b) const { return a<b.a;} inserted at Place 1
  3. bool operator < (const A & b) const { return b.a<a;} inserted at Place 1
  4. struct R { bool operator ()(const A & a, const A & b) { return a.getA()<b.getA();} }; inserted at Place 2
    replacing line marked 3 with map<A, string, R> m;
    replacing line marked 4 with map<A, string,R>::iterator i=m.begin();

Answer(s): A,B,D



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 t1[]={3,2,4,1,5};
int t2[]={6,10,8,7,9};
vector<int> v1(5);
transform(t1,t1+5,t2,v1.rbegin(), plus<int>());
for_each(v1.rbegin(), v1.rend(), Out<int>(cout));cout<<endl;
return 0;
}

Program outputs:

  1. 9 12 12 8 14
  2. 14 8 12 12 9
  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): A



Viewing page 10 of 47
Viewing questions 46 - 50 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!