C++ Institute CPP Exam
C++ Certified Professional Programmer (Page 3 )

Updated On: 1-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;
class B { int val;
public:
B(int v=0):val(v){}
int getV() const {return val;}
B operator ?(const B &b )const { return B(val ? b.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[]={1,2,3,4,5,6,7,8,9,10};
B t2[]={1,2,3,4,5,6,7,8,9,10};
vector<B> v1(t1, t1+10);
vector<B> v2(t2, t2+10);
vector<B> v3(10);
transform(v1.begin(), v1.end(), v2.rbegin(), v3.begin(), minus<B>());
for_each(v3.rbegin(), v3.rend(), Out<B>(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 <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;

void myfunction(int i) { cout << " " << i;
}
struct sequence {
int val,inc;
sequence(int s, int i):val(s),inc(i){}
int operator()(){
int r = val; val += inc;
return r;
}
};
int main() {
vector<int> v1(10);
fill(v1.begin(), v1.end(), sequence(1,1));
for_each(v1.begin(), v1.end(), myfunction);
return 0;
}

Program outputs:

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

Answer(s): D



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

#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
struct Even {
bool operator ()(int a) {
return (a % 2)==0?true:false;
}
};
int main () {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
set<int> s(t,t+15);

int number = count_if(s.begin(), s.end(), Even());
cout<< number<<endl;
return 0;
}

Program outputs:

  1. 4
  2. 3
  3. 7
  4. 8
  5. compilation error

Answer(s): B



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

#include <vector>
#include <set>
#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 start++ ;
}
};
int main() {
vector<int> v1(5);
generate(v1.begin(), v1.end(), Sequence(1));
set<int> s1(v1.rbegin(), v1.rend());
deque<int> d1(s1.rbegin(), s1.rend());
reverse(v1.begin(),v1.end());
reverse(s1.begin(), s1.end());
reverse(d1.begin(), d1.end());
for_each(v1.begin(), v1.end(), Out<int>(cout) );
for_each(s1.begin(), s1.end(), Out<int>(cout) );
for_each(d1.begin(), d1.end(), Out<int>(cout) );cout<<endl;
return 0;
}

Program outputs:

  1. 5 4 3 2 1 1 2 3 4 5 1 2 3 4 5
  2. 1 2 3 4 5 1 2 3 4 5 5 4 3 2 1
  3. no output
  4. 1 2 3 4 5 5 4 3 2 1 1 2 3 4 5
  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;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
bool Greater(int v1, int v2) { return v1<v2; }
int main() {
int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
vector<int> v1(t, t+10);
sort(v1.begin(), v1.end(), Greater);
for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
return 0;
}

Program outputs:

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

Answer(s): B



Viewing page 3 of 47
Viewing questions 11 - 15 out of 228 questions



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

Join the CPP Discussion