Free CPP Exam Braindumps
C++ Certified Professional Programmer Questions 61–65 (page: 14)

Updated On: 9-Feb-2026

What happens 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; }

void add(string & a) {
_v.insert(0, 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. compilation error
  3. program will display: world!Hello
  4. program will run without any output

Answer(s): B



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

#include <vector>
#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(10);
vector<int> v2(10);
generate(v1.begin(), v1.end(), Sequence(1));
reverse_copy(v1.begin(),v1.end(), v2.rbegin());
sort(v2.begin(), v2.end(), less_equal<int>());
for_each(v2.begin(), v2.end(), Out<int>(cout) );cout<<endl;
return 0;
}

Program outputs:

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

Answer(s): A



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

#include <iostream>
#include <algorithm>
#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;}
};
struct Compare {
bool operator ()(A & a) {
if (a.getA() < 5) return true;
return false;
}
};
int main () {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
set<A> d (t,t+15);
int number = count_if(d.begin(), d.end(), Compare());
cout<< number<<endl;
return 0;
}

Program outputs:

  1. 12
  2. 4
  3. 2
  4. 0
  5. compilation error

Answer(s): E



Given three files: class.h, class.cpp and main.cpp containing small C++ project, which sentences are TRUE if you attempt to compile and run the program? Assume that the whole compiling environment is properly set.

// File: main.cpp
#include <iostream>
#include "class.h"
using namespace std;

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

//File: class.h
#ifndef _CLASS_
#define _CLASS_
template <class T>
class A {
T _v;
public:
A() {}
A(T v);
T getV();
};
#endif

//File: class.cpp
#include "class.h"

template<typename T>
A<T>::A(T v):_v(v) {}

template<class T>
T A<T>::getV() { return _v; }

  1. program will display: 0
  2. program will not compile
  3. program will display unpredictable number
  4. program willl be not linked

Answer(s): D



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

#include <iostream>

using namespace std;

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

void f(int a)
{
cout<<2<<endl;
}

int main()
{
int a = 1;
f<float>(a);
return 0;
}

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

Answer(s): A



Viewing page 14 of 47
Viewing questions 66 - 70 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!