Pass the C++ Institute C++ Certified Professional Programmer CPP Questions and answers with CertsForce

Viewing page 3 out of 7 pages
Viewing questions 21-30 out of questions
Questions # 21:

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

#include

#include

#include <algorithm>

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

vector v1(t, t+10);

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

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


Expert Solution
Questions # 22:

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

#include

#include

#include <algorithm>

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


Expert Solution
Questions # 23:

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

vectorv1(t, t+5);

listl1;

l1.assign(v1.end(), v1.begin());

for(int i=0; i

{

cout<

}

cout<

return 0;

}

Options:

A.

program displays 5 4 3 2 1


B.

program displays 1 2 3 4 5


C.

compilation error


D.

segmentation fault runtime exception


Expert Solution
Questions # 24:

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

#include

#include

#include <algorithm>

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


Expert Solution
Questions # 25:

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


Expert Solution
Questions # 26:

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

#include

#include

#include <algorithm>

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

int main() {

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

vector v1(t, t+10);

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

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

return 0;

}

Program outputs:

Options:

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


Expert Solution
Questions # 27:

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

#include

#include

#include <algorithm>

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

struct Add : public binary_function {

int operator() (const int & a, const int & 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(Add(), 1));

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


Expert Solution
Questions # 28:

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

friend ostream & operator<<(ostream & c, const A & v);

};

template

ostream & operator<<(ostream & c, const A & v) {

c<

int main()

{

Aa(10);

cout<<a<

return 0;

}

Options:

A.

program will display:10


B.

program will not compile


C.

program will compile


D.

program will run without output


Expert Solution
Questions # 29:

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

#include

#include

#include <algorithm>

using namespace std;

templatestruct Out {

ostream & out;

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

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

int main() {

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

vector v1(t,t+10);

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

cout<

return 0;

}

Program outputs:

Options:

A.

3


B.

1


C.

6


D.

10


E.

compilation error


Expert Solution
Questions # 30:

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

#include

#include

#include <algorithm>

using namespace std;

templatestruct Out {

ostream & out;

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

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

int main() {

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

vector v1(t,t+10);

cout<<*max_element(v1.begin(), v1.end());

return 0;

}

Program outputs:

Options:

A.

3


B.

1


C.

6


D.

10


E.

compilation error


Expert Solution
Viewing page 3 out of 7 pages
Viewing questions 21-30 out of questions