C++ Institute CPP - C++ Certified Professional Programmer Exam Practice Test

Page: 1 / 14
Total 228 questions
Question 1

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

#include

#include

using namespace std;

template

class A {

T_v;

public:

A() {}

A(T v): _v(v){}

T getV() { return _v; }

void add(T & a);

void add(string & a);

};

template

void A::add(T & a) { _v+=a; }

void A::add(string & a) {

_v.insert(0, a);

}

int main()

{

Aa("Hello");

string s(" world!");

a.add(s);

cout << a.getV() <

return 0;

}



Answer : B


Question 2

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

#include

#include

#include

#include

#include

using namespace std;

int main()

{

deque mydeck;list mylist; vector myvector;

queue first; queue second(mydeck);

queue third(second); queue > fourth(mylist);

fourth.push(10);fourth.push(11);fourth.push(12);

queue > fifth(myvector);

fifth.push(10);fifth.push(11);fifth.push(12); // Line I

while(!fifth.empty())

{

cout<

fifth.pop(); // Line III

}

while (!fourth.empty())

{

cout << fourth.front() << " ";

fourth.pop(); // Line IV

}

return 0;

}



Answer : D


Question 3

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

#include

#include

using namespace std;

class A

{

int a;

public:

A():a(0){} A(int a){ this?>a = a;}

void setA(int a) {this?>a = a;}

int getA() {return a;}

};

ostream &operator<<(ostream & cout, A & a)

{

cout<< a.getA();

return cout;

}

int main ()

{

vectorv(5, new A());

v.push_back(new A(1));

vector::iterator it;

for(it = v.begin(); it != v.end(); it++)

{

cout<<*it<<" ";

}

cout<

return 0;

}



Answer : E


Question 4

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

#include

#include

#include

#include

using namespace std;

class B { int val;

public:

B(int v):val(v){} B(){}

int getV() const {return val;} bool operator > (const B & v) const { return val>v.val;} };

ostream & operator <<(ostream & out, const B & v) { out<

templatestruct Out {

ostream & out;

Out(ostream & o): out(o){}

void operator() (const T & val ) { out<

int main() {

int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};

deque d1(t, t+10);

sort(d1.begin(), d1.end(), greater());

deque::iterator it = lower_bound(d1.begin(), d1.end(), 4,greater());

for_each(it, d1.end(), Out(cout));cout<

return 0;

}

Program outputs:



Answer : A


Question 5

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

#include

#include

int main ()

{

std::vectorv1;

for(int i = 0; i<10; i++) {v1.push_back(i); }

std::vector v2(v1.begin()+2, v1.end()?2);

std::vector::iterator it = v2.begin();

for( ; it != v2.end(); it++) {std::cout<<*it++<<" "; }std::cout<

return 0;

}



Answer : D


Question 6

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

#include

#include

using namespace std;

int main() {

int t[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };

string s[] = { "one", "one", "two", "two", "three","three", "four", "four", "five", "five"};

map m;

for (int i = 0; i < 10; i++) {

m.insert(pair(t[i], s[i]));

}

if (m.count(3) == 2) {

m.erase(3);

}

for (map::iterator i = m.begin(); i != m.end(); i++) {

cout << i?>first << " ";

}

return 0;

}



Answer : A


Question 7

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

#include

#include

class A {

public:

virtual int f() { return 10; }

virtual ~A(){}

};

class B: public A {

int f() {return 11; }

virtual ~B(){}

};

int main (){

std::vectorv1;

for(int i = 10; i>0; i??)

{

i%2>0?v1.push_back(new A()):v1.push_back(new B());

}

std::vector::iterator it = v1.begin();

while(it != v1.end())

{

std::cout<f()<<" ";

v1.pop_back();++it;

}

return 0;

}



Answer : D


Page:    1 / 14   
Total 228 questions