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

Viewing page 6 out of 7 pages
Viewing questions 51-60 out of questions
Questions # 51:

What will happen 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(){

vectorv;

set s;

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

v.push_back(i);

s.push_back(i);

}

print(v.begin(), v.end()); print(s.begin(), s.end());cout<

return 0;

}

The output will be:

Options:

A.

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


B.

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


C.

10 9 8 7 6 5 4 3 2 1 and unpredictable sequence of number range 1 to 10


D.

compilation error


Expert Solution
Questions # 52:

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

#include

#include

int main ()

{

std::vectorv1;

for(int i = 0; i<10; i++) {v1.push_back(i); }

v1.resize(4);

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

v1.insert(v1.end()?1, 4);

for(int i=0 ; i<= v1.size(); i++) {std::cout<

return 0;

}

Options:

A.

compilation error


B.

program outputs 0 1 2 3 4


C.

program outputs 0 2 4 8 6 and exception


D.

program outputs 0 2 4 6 8


E.

program outputs 0 2 4 8 6


Expert Solution
Questions # 53:

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

#include

#include <algorithm>

#include

#include

using namespace std;

void myfunction(int i) { cout << " " << i;

}

struct sequence {

int val,inc;

sequence(int s, int i):val(s),inc(i){}

int operator()(){

int r = val; val += inc;

return r;

}

};

int main() {

vector v1(10);

fill(v1.begin(), v1.end(), sequence(1,1));

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

return 0;

}

Program outputs:

Options:

A.

1 2 3 4 5 6 7 8 9 10


B.

10


C.

0 0 0 0 0 0 0 0 0 0


D.

compilation error


Expert Solution
Questions # 54:

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(), bind1st(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 # 55:

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

set > s2(v.begin(), v.end());

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

cout<<*i<<" ";

}

for(set >::iterator i=s2.begin();i!= s2.end(); i++) {

cout<<*i<<" ";

}

cout<

return 0;

}

Options:

A.

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


B.

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


C.

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


D.

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


Expert Solution
Questions # 56:

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

};

struct Odd { bool operator()(int v) { return v%2==0; }};

int main() {

vector v1(10);

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

partition(v1.begin(),v1.end(), Odd());

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

return 0;

}

Choose all possible outputs:

Options:

A.

1 2 3 4 5 6 7 8 9 10


B.

5 7 3 9 1 10 2 8 4 6


C.

10 2 8 4 6 5 7 3 9 1


D.

4 6 8 10 2 7 5 3 1 9


E.

2 4 6 8 10 1 3 5 7 9


Expert Solution
Questions # 57:

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.push_back(pair(t[i], s[i]));

}

for (multimap::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 # 58:

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;

}

Options:

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


Expert Solution
Questions # 59:

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 t1[]={3,2,4,1,5};

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

vector v1(10);

sort(t1, t1+5);

sort(t2, t2+5);

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

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

return 0;

}

Program outputs:

Options:

A.

6 8 3 4 0 0 0 0 0 0


B.

3 4 0 0 0 0 0 0 0 0


C.

6 8 0 0 0 0 0 0 0 0


D.

compilation error


E.

3 4 6 8 0 0 0 0 0 0


Expert Solution
Questions # 60:

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

#include

#include <algorithm>

#include

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

set s1(t, t + 15);

pair::iterator, vector::iterator > resultSet = equal(s1.begin(), s1.end(), v1.begin());

cout<<*resultSet.first<<" "<<*resultSet.second<

return 0;

}

Program outputs:

Options:

A.

2 4


B.

4 2


C.

0 5


D.

compilation error


Expert Solution
Viewing page 6 out of 7 pages
Viewing questions 51-60 out of questions