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

Updated On: 1-Feb-2026

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

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
template<typename T>class B { T val;
public:
B(T v):val(v){}
T getV() const {return val;} bool operator < (const B & v) const { return val<v.val;} };
template<class T>ostream & operator <<(ostream & out, const B<T> & 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<<" "; } };
bool Less(const B<float> &a, const B<float> &b) { return int(a.getV())<int(b.getV());}
int main() {
float t[]={2.28, 1.66, 1.32, 3.94, 3.64, 2.3, 2.98, 1.96, 2.62, 1.13};
vector<B<float> > v1; v1.assign(t, t+10);
stable_sort(v1.begin(), v1.end(), Less);
for_each(v1.begin(), v1.end(), Out<B<float> >(cout));cout<<endl;

return 0;
}

Program outputs:

  1. 1.66 1.32 1.96 1.13 2.28 2.3 2.98 2.62 3.94 3.64
  2. 1.13 1.32 1.66 1.96 2.28 2.3 2.62 2.98 3.64 3.94
  3. compilation error
  4. 3.94 3.64 2.98 2.62 2.3 2.28 1.96 1.66 1.32 1.13
  5. the exact output is impossible to determine

Answer(s): A



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

#include <iostream>

using namespace std;

template <class T>
class A {
T _v;
public:
A() {}
A(T v): _v(v){}
friend ostream & operator<<(ostream & c, const A<T> & v) {
c<<v._v; return c;
}
};


int main()
{
A<int> a(10);
cout<<a<<endl;
return 0;
}

  1. program will display:10
  2. program will not compile
  3. program will compile
  4. program will run without output

Answer(s): A,C



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

#include <iostream>
#include <algorithm>
#include <vector>
#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; }
bool operator < (const A & b) const { return a<b.a;}
};
class F {
A val;
public:
F(A & v):val(v){}
bool operator() (A & v) {
if (v.getA() == val.getA()) return true;
return false;
}
};
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<A> v1(t, t + 10);
set<A> s1(t, t + 10);
A a(6); F f(a);
find_if(s1.begin(), s1.end(), f);
if (find_if(v1.begin(), v1.end(), f) !=v1.end()) {
cout<<"Found!\n";
} else {
cout<<"Not found!\n";
}
return 0;
}

  1. it will compile successfully
  2. it will display Found!
  3. it will display Not found!
  4. it will not compile successfully

Answer(s): D



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<<" "; } };
template <typename T> struct Sequence {
T start; T step;
Sequence(T start, T step):start(start), step(step){}
T operator()() { T v = start; start+=step; return v; } };
bool Less(float a, float b) { return int(a)<int(b);}
int main() {
float t[]={2.28, 1.66, 1.32, 3.94, 3.64, 2.3, 2.98, 1.96, 2.62, 1.13};
vector<float> v1; v1.assign(t, t+10);
stable_sort(v1.begin(), v1.end(), Less);
for_each(v1.begin(), v1.end(), Out<float>(cout));cout<<endl;

return 0;
}

Program outputs:

  1. 1.66 1.32 1.96 1.13 2.28 2.3 2.98 2.62 3.94 3.64
  2. 1.13 1.32 1.66 1.96 2.28 2.3 2.62 2.98 3.64 3.94
  3. compilation error
  4. 3.94 3.64 2.98 2.62 2.3 2.28 1.96 1.66 1.32 1.13
  5. the exact output is impossible to determine

Answer(s): A



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

#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
class B {
int val;
public:
B(int v):val(v){}
operator int() { return val;}
};
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++; } };
bool predicate(int v) { return v%2==0; }
int main() {
vector<int> v1(10);
generate_n(v1.begin(), 10, Sequence(1));
for_each(v1.begin(), remove_if(v1.begin(), v1.end(), predicate), Out<int>(cout));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
  5. no output

Answer(s): B



Viewing page 6 of 47
Viewing questions 26 - 30 out of 228 questions



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

Join the CPP Discussion