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

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

struct Add {

int operator()(int & a, int & b) {

return a+b;

}

};

int main() {

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

vector v1(t, t+10);

vector v2(10);

transform(v1.begin(), v1.end(), v2.begin(), bind1st(1,Add()));

for_each(v2.rbegin(), v2.rend(), Out(cout));cout<

return 0;

}

Program outputs:



Answer : E


Question 2

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

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

};

struct Sequence {

int start;

Sequence(int start):start(start){}

int operator()() { return start++; }

};

struct Odd { bool operator()(int v) { return v%2==0; } };

int main() {

vector v1(10);

vector v2(10);

generate(v1.begin(), v1.end(), Sequence(1));

stable_partition(v1.begin(),v1.end(), Odd());

for_each(v1.begin(), v1.end(), Out(cout) );cout<

return 0;

}

Program outputs:



Answer : E


Question 3

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

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

void operator()(const T & val ) {

out<

}

};

struct Sequence {

int start;

Sequence(int start):start(start){}

int operator()() { return start++; } };

int main() {

vector v1(10);

vector v2(10);

generate(v1.begin(), v1.end(), Sequence(1));

random(v1.begin(),v1.end());

for_each(v1.begin(), v1.end(), Out(cout) );cout<

return 0;

}

Program outputs:



Answer : D


Question 4

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

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

void operator()(const T & val ) {

out<

}

};

struct Sequence {

int start;

Sequence(int start):start(start){}

int operator()() { return 10*(1+(start++ %3)); }

};

int main() {

vector v1(10);

generate(v1.begin(), v1.end(), Sequence(1));

unique(v1.begin(),v1.end());

for_each(v1.begin(), v1.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

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

struct Add {

int operator()(int & a, int & b) {

return a+b;

}

};

int main() {

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

vector v1(t, t+10);

vector v2(10);

transform(v1.begin(), v1.end(), v2.begin(), bind1st(Add(),1));

for_each(v2.rbegin(), v2.rend(), Out(cout));cout<

return 0;

}

Program outputs:



Answer : E


Question 6

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


Question 7

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=0):val(v){}

int getV() const {return val;}

operator int () const { return 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() {

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

vector v1(t, t+10);

transform(v1.begin(), v1.end(), v1.begin(), bind2nd(plus(), 1));

for_each(v1.rbegin(), v1.rend(), Out(cout));cout<

return 0;

}

Program outputs:



Answer : D


Page:    1 / 14   
Total 228 questions