Free CPP Exam Braindumps
C++ Certified Professional Programmer Questions 56–60 (page: 13)

Updated On: 9-Feb-2026

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

Program outputs:

  1. 9 7 5 3 1 ?1 ?3 ?5 ?7 ?9
  2. ?1 ?3 ?5 ?7 ?9 9 7 5 3 1
  3. 1 3 5 7 9 ?1 ?3 ?5 ?7 ?9
  4. 1 3 5 7 9 ?1 ?3 ?5 ?7 ?9
  5. ?9 ?7 ?5 ?3 ?1 1 3 5 7 9

Answer(s): A



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<<" "; } };

struct Add : public binary_function<int, int, int> {
int operator() (const int & a, const int & b) const {
return a+b;
}
};
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
vector<int> v1(t, t+10);
vector<int> v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind1st(Add(), 1));
for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
return 0;
}

Program outputs:

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

Answer(s): D



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"};
multimap<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));
multimap<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 multimap<A, string, R> m;
    replacong line marked 4 with multimap<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 <iostream>
#include <algorithm>
#include <map>
using namespace std;

int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
map<int, int> m;
for(int i=0; i < 10; i++) {
m[i]=t[i];
}
map<int, int>::iterator it = find(m.begin(), m.end(), 5);
cout<<it?>first;
return 0;
}

Program outputs:

  1. 5
  2. 4
  3. 10
  4. compilation error

Answer(s): D



What happens when you attempt to compile and run the following code? Choose all that apply.

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
#include <iomanip>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) {out<<setw(3)<<hex<<val; } };
int main () {
int t[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
fstream f("test.out", ios::trunc|ios::out);
list<int> l(t, t+10);
for_each(l.begin(), l.end(), Out<int>(f));
f.close(); f.open("test.out");
for( ; f.good() ; ) {
int i; f>>i;
cout<<i<<" ";
}
f.close();
return 0;
}

  1. file test.out will be opened writing
  2. file test.out will be truncated
  3. file test.out will be opened for reading
  4. no file will be created nor opened
  5. program will display sequence 1 2 3 4 5 6 7 8 9 10

Answer(s): A,B,C,E



Viewing page 13 of 47
Viewing questions 61 - 65 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!