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

Updated On: 26-Jan-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<<" "; } };
B Add(B a, B b) { return a+b; }
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
vector<B> v1(t, t+10);
vector<B> v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind2nd(ptr_fun(Add),1));
for_each(v2.rbegin(), v2.rend(), Out<B>(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



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

#include <vector>
#include <iostream>
using namespace std;
class A
{
int a,b;
public:
A & operator =(const A & c) { a = c.a; return *this;}
A():a(0),b(0){}
void setA(int a) {this?>a = a;} void setB(int b) {this?>b = b;}
int getA() {return a;} int getB() {return b;}
};

int main ()
{
vector<A> v;
A a;
a.setA(10); a.setB(11);
v.push_back(a);
A b = v.front(); v.pop_back();
cout<<b.getB()<<" "<<b.getA()<<endl;
return 0;
}

  1. program outputs 11 10
  2. compilation error
  3. program outputs 0 10
  4. program outputs 10 0
  5. program outputs 11 0

Answer(s): A



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

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
float f = 10.126;
cout<<f<<" "<<setprecision(2)<<f<<endl;
return 0;
}

Program outputs:

  1. 10.126 10
  2. 10.126 10.12
  3. compilation error
  4. 10.126 10.13

Answer(s): A



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

#include <iostream>

using namespace std;

template<int>
void g(int a)
{
cout<<a?1<<endl;
}

template<class A>
void g(A a)
{
cout<<a+1<<endl;
}

int main()
{
int a = 1;
g(a);
return 0;
}

  1. program displays: 1
  2. program displays: 2
  3. compilation error
  4. runtime exception

Answer(s): B



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

#include <iostream>
#include <string>

using namespace std;

template <class T>
class A {
T _v;
public:
A() {}
A(T v): _v(v){}
T getV() { return _v; }
void add(T & a) { _v+=a; }
};


int main()
{
A<string> a("Hello");
string s(" world!");
a.add(s);
cout << a.getV() <<endl;
return 0;
}

  1. program will display: Hello world!
  2. program will not compile
  3. program will display: Hello
  4. program will run without any output

Answer(s): A



Viewing page 9 of 47
Viewing questions 41 - 45 out of 228 questions



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

Join the CPP Discussion