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

Viewing page 2 out of 7 pages
Viewing questions 11-20 out of questions
Questions # 11:

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

map m;

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

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

}

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

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

}

return 0;

}

Options:

A.

program outputs: 1 2 3 4 5


B.

compilation error


C.

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


D.

program outputs: one two three four five


E.

program outputs: one one two two three three four four five five


Expert Solution
Questions # 12:

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

#include

#include <algorithm>

#include

using namespace std;

int main() {

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

map m;

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

m[i]=t[i];

}

pair p(5,5);

map::iterator it = find(m.begin(), m.end(), p);

if (it != m.end())

{

cout<first<

}

else

{

cout<<"Not found!\n";

}

return 0;

}

Program outputs:

Options:

A.

5


B.

Not found!


C.

10


D.

compilation error


Expert Solution
Questions # 13:

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

#include

#include

#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[] = {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;

}

Options:

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


Expert Solution
Questions # 14:

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

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

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

vector v1(10,0);

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


Expert Solution
Questions # 15:

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

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

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

vector v1(10);

sort(t1, t1+5);

sort(t2, t2+5);

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

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

return 0;

}

Program outputs:

Options:

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


Expert Solution
Questions # 16:

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

#include

#include <algorithm>

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

operator int() const {return a;}

};

int main () {

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

set s (t,t+15);

cout<

return 0;

}

Program outputs:

Options:

A.

true


B.

false


C.

1


D.

0


E.

compilation error

Questions # 17:

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

#include

#include <algorithm>

#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


Expert Solution
Questions # 18:

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

#include

#include

#include

using namespace std;

template void print(T start, T end) {

while (start != end) {

std::cout << *start << " "; start++;

}

}

int main() {

string t1[] ={ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};

list l1(t1, t1 + 10);

list l2(l1);

l2.reverse(); l1.splice(l1.end(),l2);

l1.unique();

print(l1.begin(), l1.end()); cout<

return 0;

}

Options:

A.

compilation error


B.

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


C.

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


D.

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


Expert Solution
Questions # 19:

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

#include

#include <algorithm>

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

Options:

A.

Identical


B.

Not identical


C.

compilation error at line marked I


D.

compilation error at line marked II


Expert Solution
Questions # 20:

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


Expert Solution
Viewing page 2 out of 7 pages
Viewing questions 11-20 out of questions