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

Viewing page 5 out of 7 pages
Viewing questions 41-50 out of questions
Questions # 41:

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): _v(v){}

T getV() { return _v; }

};

int main()

{

A a(1);

cout << a.getV() <

return 0;

}

Options:

A.

program will display:1


B.

program will not compile


C.

program will compile


D.

program will cause runtime exception


Expert Solution
Questions # 42:

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;

}

Options:

A.

program will display: Hello world!


B.

compilation error


C.

program will display: world!Hello


D.

program will run without any output


Expert Solution
Questions # 43:

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

#include

#include

#include <algorithm>

#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:

Options:

A.

4 3 2 1


B.

3 2 1


C.

5 4 3 2 1


D.

compilation error


E.

1 2 3 4


Expert Solution
Questions # 44:

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

multiset s1(t,t+10);

s1.insert(s1.find(7), 3);

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

cout<<*i<<" ";

}

return 0;

}

Options:

A.

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


B.

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


C.

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


D.

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


E.

runtime exception


Expert Solution
Questions # 45:

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

#include

#include <algorithm>

#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(t, t+10, v1.end());

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

return 0;

}

Program outputs:

Options:

A.

10 5 9 6 2 4 7 8 3 1


B.

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


C.

compilation error


D.

runtime exception/segmentation fault


Expert Solution
Questions # 46:

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

deque d1(t, t+10);

deque d2(10);

transform(d1.begin(), d1.end(), d2.begin(), bind2nd(Add(), 1));

for_each(d2.rbegin(), d2.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 # 47:

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

#include

using namespace std;

template

void f(A &a)

{

cout<<1<

}

void f(int &a)

{

cout<<2<

}

int main()

{

int a = 1;

f(a);

return 0;

}

Options:

A.

program displays: 1


B.

program displays: 2


C.

compilation error


D.

runtime exception


Expert Solution
Questions # 48:

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

#include

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

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


Expert Solution
Questions # 49:

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

#include

#include <algorithm>

#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


Expert Solution
Questions # 50:

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

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

} };

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

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

Aa (5);

a.add(C());

cout << a.getV() <

return 0;

}

Options:

A.

program will display:5


B.

program will not compile


C.

program will compile


D.

program will cause runtime exception


Expert Solution
Viewing page 5 out of 7 pages
Viewing questions 41-50 out of questions