Labour Day Sale - Limited Time 70% Discount Offer - Ends in 0d 00h 00m 00s - Coupon code: mxmas70

Home > C++ Institute > C++ Certified Professional Programmer > CPP

CPP C++ Certified Professional Programmer Question and Answers

Question # 4

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

#include

#include

#include

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==(A & b) { return a == b.a; }

};

struct Compare{

bool operator()(const A & a, const A & b) {return a.getA()==b.getA();};

};

int main () {

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

vector v (t,t+10);

vector::iterator it;

A m1[] = {A(1), A(2), A(3)};

it = find_end (v.begin(), v.end(), m1, m1+3, Compare());

cout << "Found at position: " << it?v.begin() << endl;

return 0;

}

A.

program outputs: Found at position: 5

B.

program outputs: Found at position: 0

C.

program outputs: Found at position: 7

D.

compilation error

E.

program outputs: Found at position: 10

***/

Question # 5

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>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[]={20, 30, 10, 20, 30, 10, 20, 30, 10, 20};

deque d1(t, t+10);

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

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

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

return 0;

}

Program outputs:

A.

30 30 30 20 20 20 20 10 10 10

B.

20 20 20 20

C.

30 20 20 20 10

D.

20 20 20 20 10

E.

30 20 20 20 20 10

Full Access
Question # 6

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;

}

A.

program will display: Hello world!

B.

compilation error

C.

program will display: world!Hello

D.

program will run without any output

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

B operator +(const B &b )const { return B(val + b.val);} };

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

templatestruct Out {

ostream & out;

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

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

template struct Add : public binary_function {

A operator() (const A & a, const A & b) const { 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(ptr_fun (Add()), 1));

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

return 0;

}

Program outputs:

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

Full Access
Question # 8

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;

}

int main() {

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

set s1(t, t+10);

vector v1(s1.rbegin(), s1.rend());

swap(s1, v1);

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

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

return 0;

}

Program outputs:

A.

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

B.

compilation error

C.

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

D.

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

Full Access
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;

}

A.

program will display:0

B.

program will not compile

C.

program will compile

D.

program will cause runtime exception

Full Access
Question # 10

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

#include

#include

#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(5);

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

set s1(v1.rbegin(), v1.rend());

deque d1(s1.rbegin(), s1.rend());

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

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

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

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

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

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

return 0;

}

Program outputs:

A.

5 4 3 2 1 1 2 3 4 5 1 2 3 4 5

B.

1 2 3 4 5 1 2 3 4 5 5 4 3 2 1

C.

no output

D.

1 2 3 4 5 5 4 3 2 1 1 2 3 4 5

E.

compilation error

Full Access
Question # 11

What will happen when you attempt to compile and run the code below, assuming that file test.out do not exist before the program execution?

#include

#include

#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[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

fstream f("test.out");

list l(t, t+10);

for_each(l.begin(), l.end(), Out(f));

f.close();

return 0;

}

A.

file test.out will be created and opened for writing

B.

file test.out will be created and opened for reading

C.

no file will be created nor opened

D.

file test.out will contain sequence 1 2 3 4 5 6 7 8 9 10

E.

compilation error

Full Access
Question # 12

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:

A.

3 2 4 1 5 6 10 8 7 9

B.

4 3 5 2 6 7 11 9 8 10

C.

9 7 8 10 6 5 1 4 2 3

D.

10 8 9 11 7 6 2 5 3 4

E.

compilation error

Full Access
Question # 13

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

multimap m;

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

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

}

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

m.erase(3);

}

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

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

}

return 0;

}

A.

program outputs: 1 2 3 4 5

B.

program outputs: 1 2 4 5

C.

program outputs: 1 1 2 2 3 4 4 5 5

D.

program outputs: 1 1 2 2 4 4 5 5

E.

program outputs: one two three four five

Full Access
Question # 14

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

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<<*i<<" ";

}

return 0;

}

A.

program outputs: 0 1 8 9

B.

program outputs: 2 3 4 5 6 7

C.

program outputs: 1 6 5 7

D.

program outputs: 3 4 9 8 0

Full Access
Question # 15

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

#include

#include

using namespace std;

int main ()

{

float f = 10.126;

cout.unsetf(ios::floatfield);

cout<

return 0;

}

What will be a mantissa part of the numbers displayed:

A.

1.0126 1.013

B.

1.012600 10.013

C.

10.01260 10.013

D.

1.012600 1.013

E.

1.0126 1.01

Full Access
Question # 16

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

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

B t2[]={6,10,8,7,9};

vector v1(10,0);

sort(t1, t1+5); sort(t2, t2+5);

copy(t1,t1+5,v1.begin());

copy(t2,t2+5,v1.begin()+5);

inplace_merge(v1.begin(), v1.begin()+5,v1.end());

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

return 0;

}

Program outputs:

A.

1 2 3 4 5 6 10 8 7 9

B.

3 2 4 1 5 6 7 8 9 10

C.

3 2 4 1 5 6 10 8 7 9

D.

1 2 3 4 5 6 7 8 9 10

E.

compilation error

Full Access
Question # 17

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:

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

Full Access
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 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(), bind2nd(Add(),1));

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

return 0;

}

Program outputs:

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

Full Access
Question # 19

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:

A.

123

B.

1 2 3

C.

321

D.

compilation error

E.

the result is unspecified

Full Access
Question # 20

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

#include

#include

#include

#include

using namespace std;

bool identical(int a, int b) {

return b == 2*a?true:false;

}

int main() {

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

int u[] = {2,4,6,4,6,10,2,4,14,6,4,2,20,8,8,5};

vector v1(t, t + 15);

deque d1(u, u + 15);

pair::iterator, vector::iterator > result;

result = mismatch(d1.begin(), d1.end(), v1.begin(), identical); //Line I

if (result.first == d1.end() && result.second == v1.end()) {//Line II

cout<<"Identical\n";

} else {

cout<<"Not identical\n";

}

return 0;

}

Program outputs:

A.

Identical

B.

Not identical

C.

compilation error at line marked I

D.

compilation error at line marked II

Full Access
Question # 21

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

#include

#include

#include

using namespace std;

void myfunction(int i) {

cout << " " << i;

}

void multiply (int a) {

a*2;

}

int main() {

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

vector v1(t, t+10);

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

iter_swap(v1.begin(),t+9);

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

return 0;

}

Program outputs:

A.

1 5 9 6 2 4 7 8 3 1

B.

compilation error

C.

1 2 3 4 5 6 7 8 9 10

D.

10 9 8 7 6 5 4 3 2 1

E.

10 5 9 6 2 4 7 8 3 1

Full Access
Question # 22

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

#include

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

int main ()

{

list l;

for( ; !cin.bad() ; )

{

int i;

cin>>i;

l.push_back(i);

}

for_each(l.begin(), l.end(), Out(cout));

return 0;

}

Program will output:

A.

1 2 3

B.

1 2 3 end

C.

1

D.

compilation error

E.

program runs forever without output

Full Access
Question # 23

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;

}

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

Full Access
Question # 24

Which sentence is correct about the code below? Choose all that apply.

#include

#include

#include

using namespace std;

class F {

int val;

public:

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

bool operator() (int v) {

if (v == val) return true;

return false;

}

};

int main() {

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

vector v1(t, t + 10);

if (find(v1.begin(), v1.end(), 6) == find(v1.begin(), v1.end(), F(6))) {

cout<<"Found!\n";

} else {

cout<<"Not found!\n";

}

return 0;

}

A.

it will compile successfully

B.

it will display Found!

C.

it will display Not found!

D.

it will not compile successfully

Full Access
Question # 25

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,4,5,1,2,3,4,5};

vector v (t,t+10);

vector::iterator it;

int m1[] = {1, 2, 3};

it = search (v.begin(), v.end(), m1, m1+3);

cout << "found at position: " << it?v.begin() << endl;

return 0;

}

Program outputs:

A.

found at position: 5

B.

found at position: 0

C.

found at position: 6

D.

found at position: 1

E.

found at position: 10

Full Access
Question # 26

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

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

for_each(v1.begin(), v1.end(), bind1st(plus(), 1));

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

return 0;

}

Program outputs:

A.

3 2 4 1 5 6 10 8 7 9

B.

4 3 5 2 6 7 11 9 8 10

C.

9 7 8 10 6 5 1 4 2 3

D.

10 8 9 11 7 6 2 5 3 4

E.

compilation error

Full Access
Question # 27

What happens when you attempt to compile and run the following code? Choose all that apply.

#include

#include

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

templatestruct Out {

ostream & out;

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

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

int main () {

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

fstream f("test.out", ios::trunc|ios::out);

list l(t, t+10);

for_each(l.begin(), l.end(), Out(f));

f.close();

f.open("test.out");

for( ; f.good() ; ) {

int i;

f>>i;

cout<

}

f.close();

return 0;

}

A.

file test.out will be opened writing

B.

file test.out will be truncated

C.

file test.out will be opened for reading

D.

no file will be created nor opened

E.

program will display sequence 1 2 3 4 5 6 7 8 9 10

Full Access
Question # 28

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[]={6,10,8,7,9};

vector v1(10);

sort(t1, t1+5); sort(t2, t2+5);

copy(t1,t1+5,v1.begin());

copy(t2,t2+5,v1.begin()+5);

merge(v1.begin(), v1.begin()+5,v1.end());

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

return 0;

}

Program outputs:

A.

1 2 3 4 5 6 10 8 7 9

B.

3 2 4 1 5 6 7 8 9 10

C.

3 2 4 1 5 6 10 8 7 9

D.

1 2 3 4 5 6 7 8 9 10

E.

compilation error

Full Access
Question # 29

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;

}

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

Full Access
Question # 30

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

#include

#include

int main ()

{

std::vectorv1;

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

{

v1.push_back(i);

}

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

int sum = 0;

while(it != v1.end())

{

sum+=it++;

}

std::cout<<*v1.erase(v1.begin(),v1.end()?3)<<" "<

return 0;

}

A.

program outputs 3 55

B.

compilation error

C.

program outputs 3 45

D.

program outputs 7 55

Full Access
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<

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:

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

Full Access
Question # 33

What will happen when you attempt to compile and run the code below, assuming that file test.in contains the following sequence: 1 2 3?

#include

#include

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

int main () {

ifstream f("test.in");

list l;

for( ; !f.fail() ; ) {

int i;

f>>i;

l.push_back(i);

}

f.close();

for_each(l.begin(), l.end(), Out(cout));

return 0;

}

Programwill output:

A.

1 2 3

B.

1 2 3 3

C.

no output

D.

compilation error

E.

program runs forever without output

Full Access
Question # 34

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

#include

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

int Add(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(), bind2nd(ptr_fun (Add),1));

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

return 0;

}

Program outputs:

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

Full Access