Summer Special Flat 65% Limited Time Discount offer - Ends in 0d 00h 00m 00s - Coupon code: suredis

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

Demo: 34 questions
Total 228 questions

C++ Certified Professional Programmer Questions and Answers

Question 1

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

#include

#include

#include

using namespace std;

class B { int val;

public:

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

int getV() const {return val;} bool operator < (const B & v) 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() {

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

set s1(t, t+10);

sort(s1.begin(), s1.end());

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

return 0;

}

Program outputs:

Options:

A.

8 10 5 1 4 6 2 7 9 3

B.

1 2 3 4 5 6 7 8 9 10

C.

compilation error

D.

10 9 8 7 6 5 4 3 2 1

Question 2

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

#include

#include

#include

using namespace std;

int main(){

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

vectorv(myints, myints+10);

set s1(v.begin(),v.end());

set > s2(v.begin(), v.end());

for(set::iterator i=s1.begin();i!= s1.end(); i++) {

cout<<*i<<" ";

}

for(set >::iterator i=s2.begin();i!= s2.end(); i++) {

cout<<*i<<" ";

}

cout<

return 0;

}

Options:

A.

program outputs: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9

B.

program outputs: 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0

C.

program outputs: 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0

D.

program outputs: 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9

Question 3

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

#include

#include

#include

#include

using namespace std;

void myfunction(int i) { cout << " " << i;

}

struct sequence {

int val,inc;

sequence(int s, int i):val(s),inc(i){}

int operator()(){

int r = val; val += inc;

return r;

}

};

int main() {

vector v1(10);

fill(v1.begin(), v1.end(), sequence(1,1));

for_each(v1.begin(), v1.end(), myfunction);

return 0;

}

Program outputs:

Options:

A.

1 2 3 4 5 6 7 8 9 10

B.

10

C.

0 0 0 0 0 0 0 0 0 0

D.

compilation error

Question 4

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

#include

#include

#include

using namespace std;

void print(int v) {

cout<

}

struct Sequence {

int start;

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

int operator()() {

return start++;

}

};

int main() {

vector v1(10);

generate_n(v1.begin(), 10, Sequence(1));

for_each(v1.begin(), v1.end(), print);

cout<

return 0;

}

Program outputs:

Options:

A.

1 2 3 4 5 6 7 8 9 10

B.

0 0 0 0 0 0 0 0 0 0

C.

compilation error

D.

no output

Question 5

Which changes introduced independently will allow code to compile and display 0 1 8 9 (choose all that apply)

#include

#include

#include

using namespace std;

class A {

int a;

public:

A(int a):a(a){}

int getA() const { return a;}

/* Insert Code Here 1 */

};

/* Insert Code Here 2 */

int main(){

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

vectorv(t, t+10);

set s1(v.begin(),v.end());

s1.insert(v.begin(),v.end());

s1.erase(s1.lower_bound(2),s1.upper_bound(7));

for(set::iterator i=s1.begin();i!= s1.end(); i++) {

cout<getA()<<" ";

}

cout<

return 0;

}

Options:

A.

operator int() const { return a;} inserted at Place 1

B.

bool operator < (const A & b) const { return a

C.

bool operator < (const A & b) const { return b.a

D.

bool operator < (const A & a, const A & b) { return a.getA()

Question 6

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

#include

#include

#include

using namespace std;

void print(int v) { cout<

struct Sequence {

int start;

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

int operator()() {

return 10*(1+(start++ %3));

}

};

int main() {

vector v1(10);

generate_n(v1.begin(), 10, Sequence(1));

remove(v1.begin(), v1.end(), 10);

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

return 0;

}

Program outputs:

Options:

A.

20 30 10 20 30 10 20 30 10 20

B.

20 30 20 30 20 30 20

C.

20 30 20 30 20 30 20 30 10 20

D.

compilation error

Question 7

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

#include

#include

#include

using namespace std;

int main(){

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

sets(myints, myints+10);

multiset s1(s.begin(),s.end());

s1.insert(s.begin(),s.end());

s1.erase(s1.lower_bound(2),s1.upper_bound(7));

for(multiset::iterator i=s1.begin();i!= s1.end(); i++) {

cout<<*i<<" ";

}

return 0;

}

The output will be:

Options:

A.

0 0 1 1 8 8 9 9

B.

0 1 8 9

C.

2 3 4 5 6 7

D.

3 4 9 8 0

E.

3 3 4 4 9 9 8 8 0 0

Question 8

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

#include

using namespace std;

int main()

{

cout.setf(ios::hex, ios::basefield);

cout<<100.33<<" ";

cout.setf(ios::showbase);

cout<<100.33<<" ";

return 0;

}

Program outputs:

Options:

A.

64.21 64.21

B.

64.21 0x64.21

C.

0x64.21 0x64.21

D.

100.33 100.33

E.

compilation error

Question 9

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

#include

using namespace std;

class C {};

template

class A {

T_v;

public:

A() {}

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

T getV() { return _v; }

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

};

int main()

{

A b;

Aa;

a.add(C());

cout << b.getV() <

return 0;

}

Options:

A.

program will display:0

B.

program will not compile

C.

program will compile

D.

program will cause runtime exception

Question 10

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){}

operator int() { return val;}

};

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++; } };

bool predicate(int v) { return v%2==0; }

int main() {

vector v1(10);

generate_n(v1.begin(), 10, Sequence(1));

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

return 0;}

Program outputs:

Options:

A.

1 3 5 7 9 6 7 8 9 10

B.

1 3 5 7 9

C.

2 4 6 8 10

D.

compilation error

E.

no output

Question 11

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;

}

Options:

A.

compilation error

B.

program outputs 0 1 2 3 4 5 6 7 8 9

C.

program outputs 2 3 4 5 6 7

D.

program outputs 2 4 6

Question 13

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:

Options:

A.

1 2 3 4 5 6 7 8 9 10

B.

2 3 4 5 6 7 8 9 10 11

C.

10 9 8 7 6 5 4 3 2 1

D.

11 10 9 8 7 6 5 4 3 2

E.

compilation error

Question 14

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

#include

#include

#include

using namespace std;

void myfunction(int i) {

cout << " " << i;

}

int main() {

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

vector v1(t, t + 10);

copy_backward(t, t+10, v1.rend());

for_each(v1.begin(), v1.end(), myfunction);

return 0;

}

Program outputs:

Options:

A.

10 5 9 6 2 4 7 8 3 1

B.

1 3 8 7 4 2 6 9 5 10 10 5 9 6 2 4 7 8 3 1

C.

1 3 8 7 4 2 6 9 5 10

D.

runtime exception/segmentation fault

E.

compilation error

Question 15

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

#include

using namespace std;

class C {

public:

int _c;

C():_c(0){}

C(int c) { _c = c;}

C operator+=(C & b) {

C tmp;

tmp._c = _c+b._c;

return tmp;

}

};

template

class A {

T_v;

public:

A() {}

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

T getV() { return _v; }

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

};

int main()

{

A b(2);

Aa (5);

Cc;

a.add(c);

cout << a.getV() <

return 0;

}

Options:

A.

program will display:2

B.

program will not compile

C.

program will compile

D.

program will cause runtime exception

Question 16

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

#include

#include

#include

using namespace std;

int main(){

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

vectorv(t, t+10);

multiset s1(v.begin(),v.end());

multiset > s2(v.begin(), v.end());

for(multiset >::iterator i=s2.begin();i!= s2.end(); i++) {

cout<<*i<<" ";

}

for(multiset::iterator i=s1.begin();i!= s1.end(); i++) {

cout<<*i<<" ";

}

cout<

return 0;

}

The output will be:

Options:

A.

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9

B.

9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0

C.

0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0

D.

9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9

Question 17

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){}

int getV() const {return val;} bool operator < (const B & v) 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() {

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

deque d1(t, t+10);

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

set s1(t,t+10);

cout<

return 0;

}

Program outputs:

Options:

A.

1 0

B.

1 1

C.

0 0

D.

0 1

E.

compilation error

Question 18

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() {

deque d1(10);

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

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

pair::iterator, deque::iterator > result = equal_range(d1.begin(), d1.end(), 20);

for_each(result.first, result.second, Out(cout));cout<

return 0;

}

Program outputs:

Options:

A.

10 10 10 20 20 20 20 30 30 30

B.

20 20 20 20

C.

10 20 20 20 20

D.

20 20 20 20 30

E.

10 20 20 20 20 30

Question 19

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

#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;} };

templatestruct Out {

ostream & out;

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

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

struct Add {

B operator()(B & a, B & 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:

Options:

A.

1 2 3 4 5 6 7 8 9 10

B.

2 3 4 5 6 7 8 9 10 11

C.

10 9 8 7 6 5 4 3 2 1

D.

11 10 9 8 7 6 5 4 3 2

E.

compilation error

Question 20

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

#include

#include

#include

using namespace std;

int main () {

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

vector v (t,t+15);

vector::iterator it = search_n(v.begin(), v.end(), 4, 2);

cout<< it?v.begin()<

return 0;

}

Program outputs:

Options:

A.

10

B.

3

C.

1

D.

15

E.

compilation error

Question 21

Which stack initialization (line numbers) are correct? Choose all that apply.

#include

#include

#include

#include

#include

using namespace std;

int main()

{

deque mydeck;

list mylist;

vector myvector;

stack first;// Line I

stack second(mydeck);// Line II

stack third(second);// Line III

stack > fourth(mylist);// Line IV

stack > fifth(myvector);// Line V

return 0;

}

Options:

A.

line I

B.

line II

C.

line III

D.

line IV

E.

line V

Question 22

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

#include

#include

#include

using namespace std;

int main () {

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

vector v (t,t+15);

int number = count(v.begin(), v.end(), 2);

cout<< number<

return 0;

}

Program outputs:

Options:

A.

4

B.

3

C.

2

D.

0

E.

compilation error

Question 23

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

#include

using namespace std;

template

class A {

T_v;

public:

A() {}

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

T getV() { return _v; }

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

template

U get(U a) {

return (U)(_v);

}

};

int main()

{

A a(1);

a.add(10);

cout.setf( ios::showpoint);

cout << a.getV() << " " << a.get(1.0)<

return 0;

}

Options:

A.

program will display: 11 11

B.

program will not compile

C.

program will display: 11.0000 11

D.

program will display: 11 11.000

Question 24

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

#include

#include

#include

#include

#include

using namespace std;

int main()

{

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

deque mydeck(t, t+10);list mylist(t,t+10);

queue first;

queue second(mydeck);

queue third(second);

queue > fourth(mylist);

mylist.clear();third.clear();

cout<

cout<

return 0;

}

Options:

A.

program outputs: 10 0

10 0

B.

program outputs: 0 0

0 0

C.

program outputs: 10 10

10 10

D.

program outputs: 10 0

0 10

E.

compilation error

Question 26

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

#include

#include

#include

#include

#include

using namespace std;

void myfunction(int i) {

cout << " " << i;

}

int main() {

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

vector v1(t, t + 10);

deque d1(t, t + 10);

set s1(t, t + 10);

for_each(v1.begin(), v1.end(), myfunction); // Line I

for_each(d1.begin(), d1.end(), myfunction); // Line II

for_each(s1.begin(), s1.end(), myfunction); // Line III

return 0;

}

Options:

A.

program outputs: 10 5 9 6 2 4 7 8 3 1 10 5 9 6 2 4 7 8 3 1 1 2 3 4 5 6 7 8 9 10

B.

program outputs: 10 5 9 6 2 4 7 8 3 1 10 5 9 6 2 4 7 8 3 1 10 5 9 6 2 4 7 8 3 1

C.

program outputs: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

D.

compilation error in line I

E.

compilation error in line III

Question 27

Which method added to class B at the marked spot will allow the code below to compile? Choose all possible solutions.

#include

#include

#include

using namespace std;

class B { int val;

public:

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

int getV() const {return val;}

/* Insert Code Here */

};

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

vector v1(t, t+10);

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

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

return 0;

}

Options:

A.

bool operator < (const B & v) const { return val

B.

bool operator > (const B & v) const { return val

C.

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

D.

bool operator == (const B & v) const { return val==v.val;}

E.

operator int () const { return val; }

Question 28

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

#include

using namespace std;

int main()

{

cout<

return 0;

}

Program outputs:

Options:

A.

true false

B.

1 0

C.

1 false

D.

true 0

E.

compilation error

Question 29

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<

int main() {

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

int t2[]={5,6,8,2,1};

vector v1(10);

sort(t1, t1+5);

sort(t2, t2+5);

set_union(t1,t1+5,t2,t2+5,v1.begin());

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

return 0;

}

Program outputs:

Options:

A.

3 2 4 1 5 6 8 2 1 0

B.

1 2 3 4 5 6 8 2 1 0

C.

1 1 2 2 3 4 5 5 6 8

D.

1 2 3 4 5 6 8 0 0 0

E.

compilation error

Question 30

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<

int main() {

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

int t2[]={5,6,8,2,1};

vector v1(10);

sort(t1, t1+5);

sort(t2, t2+5);

set_intersection(t1,t1+5,t2,t2+5,v1.begin());

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

return 0;

}

Program outputs:

Options:

A.

compilation error

B.

1 2 3 4 5 6 8 0 0 0

C.

1 2 3 4 5 6 8 2 1 0

D.

1 1 2 2 3 4 5 5 6 8

E.

1 2 5 0 0 0 0 0 0 0

Question 31

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<

int main() {

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

deque d1(t, t+10);

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

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

return 0;

}

Program outputs:

Options:

A.

8 10 5 1 4 6 2 7 9 3

B.

4 5 6 7 8 9 10

C.

1 2 3 4 5 6 7 8 9 10

D.

compilation error

E.

1 2 3 4

Question 32

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

#include

using namespace std;

template

class A {

T_v;

public:

A(T v);

};

template

Options:

A.

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

int main()

{

A a(2);

cout<<1<

return 0;

}

B.

program will display: 1

C.

program will not compile

D.

program will compile

E.

program will cause runtime exception

Question 33

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

#include

#include

#include

#include

#include

using namespace std;

int main() {

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

vector v(t, t + 10);

multimap m;

for (vector::iterator i = v.begin(); i != v.end(); i++) {

stringstream s;s << *i << *i;

m.insert(pair(*i, s.str()));

}

pair::iterator, multimap::iterator> range;

range = m.equal_range(2);

for (multimap::iterator i = range.first; i != range.second; i++) {

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

}

return 0;

}

The output will be:

Options:

A.

2 2

B.

1 2

C.

1 3

D.

2

E.

0 2

Question 34

What will happen when you attempt to compile and run the code below, assuming you enter the following sequence: 1 2 3?

#include

using namespace std;

int main ()

{

int a,b,c;

cin>>a>>b>>c;

cout<

return 0;

}

Program will output:

Options:

A.

123

B.

1 2 3

C.

321

D.

compilation error

E.

the result is unspecified

Demo: 34 questions
Total 228 questions