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

Viewing page 4 out of 7 pages
Viewing questions 31-40 out of questions
Questions # 31:

What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 1.1 2.2 3.3?

#include

#include

using namespace std;

int main ()

{

int a,b,c;

cin>>a>>b>>c;

cout<<a<

return 0;

}

Program will output:

Options:

A.

123


B.

1 2 3


C.

1.12.23.3


D.

1.1 2.2 3.3


E.

none of these


Expert Solution
Questions # 32:

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

vector v1(10);

vector v2(10);

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

sort(v1.rbegin(), v1.rend());

unique_copy(v1.begin(),v1.end(), v2.begin());

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

return 0;

}

Program outputs:

Options:

A.

20 30 10 20 30 10 20 30 10 20


B.

30 20 10 0 0 0 0 0 0 0


C.

30 0 0 0 0 0 0 0 20 10


D.

compilation error


Expert Solution
Questions # 33:

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

include

#include <algorithm>

#include

#include

#include

using namespace std;

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

cout<

return 0;

}

Options:

A.

program outputs: 6 6 6


B.

program outputs: 3 3 5


C.

program outputs: 3 6 5


D.

compilation error


E.

none of these


Expert Solution
Questions # 34:

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

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

};

struct Compare {

bool operator ()(A & a) {

if (a.getA() < 5) return true;

return false;

}

};

int main () {

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

set d (t,t+15);

int number = count_if(d.begin(), d.end(), Compare());

cout<< number<

return 0;

}

Program outputs:

Options:

A.

12


B.

4


C.

2


D.

0


E.

compilation error

Questions # 35:

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

#include

#include

#include

using namespace std;

template

int calculate(T start, T end)

{

int s = 0;

while (start != end)

s+= *start; start++;return s;

}

int main ()

{

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

vectorv1(t, t+5);

dequed1(t+5, t+10);

cout<

cout<

cout<

cout<

cout<

return 0;

}

Options:

A.

compilation error


B.

runtime exception


C.

program outputs 55 5 17 55


D.

program outputs 55 5 17 0


Expert Solution
Questions # 36:

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

#include

#include <algorithm>

#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 < ( const A & b) const { return a

};

struct display { void operator() (const A & a) {cout << " " << a.getA();} };

struct add10

{

void operator() (A & a) { a.setA(a.getA()+10) ;}

};

int main() {

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

vector v1(t, t + 10);

set s1(t, t + 10);

for_each(v1.begin(), v1.end(), add10()); for_each(v1.begin(), v1.end(), display());

for_each(s1.begin(), s1.end(), add10()); for_each(s1.begin(), s1.end(), display());

return 0;

}

Options:

A.

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


B.

program outputs: 20 15 19 16 12 14 17 18 13 11 1 2 3 4 5 6 7 8 9 10


C.

program outputs: 20 15 19 16 12 14 17 18 13 11 11 12 13 14 15 16 17 18 19 20


D.

compilation error

Questions # 37:

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

#include

#include

#include

using namespace std;

int main(){

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

string first[] = {"three", "four", "two", "one", "six","five", "seven", "nine","eight"," ten"};

multimap m;

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

m.insert(pair(second[i],first[i]));

}

if (m[11] == "eleven") {

cout<<"eleven ";

}

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

cout<second<<" ";

}

cout<

return 0;

}

Options:

A.

program outputs: one two three four five six seven eight nine ten 11


B.

program outputs: one two three four five six seven eight nine ten 10


C.

program outputs: one two three four five six seven eight nine ten 10


D.

program outputs: eleven one two three four five six seven eight nine ten 10


E.

compilation error


Expert Solution
Questions # 38:

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

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=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 : public binary_function {

B operator() (const B & a, const B & b) const {

return a+b; } };

int main() {

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

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 <algorithm>

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;

}

Options:

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


Expert Solution
Viewing page 4 out of 7 pages
Viewing questions 31-40 out of questions