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

Viewing page 7 out of 7 pages
Viewing questions 61-70 out of questions
Questions # 61:

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

map m;

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

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

}

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

cout<<"eleven ";

}

for(map::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.

runtime exception


Expert Solution
Questions # 62:

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

#include

using namespace std;

template

class A {

T_v;

public:

A() {}

A(T v): _v(v){}

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

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

string t[]={"aaa","Aaa", "aAa","aaA","bbb","Bbb", "bBb", "bbB"};

vector v1(t, t+8);

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

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

return 0;

}

Program outputs:

Options:

A.

Aaa Bbb aAa aaA aaa bBb bbB bbb


B.

Aaa aAa Bbb aaA aaa bBb bbB bbb


C.

bBb bbB bbb Aaa aAa Bbb aaA aaa


D.

Aaa aAa bBb bbB bbb Bbb aaA aaa


E.

compilation error


Expert Solution
Questions # 64:

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;

}

int main() {

vector v1(10,1);

fill(v1.begin()+2, v1.end()?2,2);

fill_n(v1.begin()+4,2,3);

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

return 0;

}

Program outputs:

Options:

A.

1 1 2 2 3 3 2 2 1 1


B.

1 1 2 2 2 2 2 2 1 1


C.

compilation error


D.

none of these


Expert Solution
Questions # 65:

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

s1.insert(v.begin(),v.end());

s1.erase(s1.lower_bound(2),s1.upper_bound(7));

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

cout<<*i<<" ";

}

return 0;

}

Options:

A.

program outputs: 0 1 8 9


B.

program outputs: 2 3 4 5 6 7


C.

program outputs: 1 6 5 7


D.

program outputs: 3 4 9 8 0


Expert Solution
Questions # 66:

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

#include

#include <algorithm>

#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

};

class F {

A val;

public:

F(A & v):val(v){}

bool operator() (A & v) {

if (v.getA() == val.getA()) return true;

return false;

}

};

int main() {

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

vector v1(t, t + 10);

set s1(t, t + 10);

A a(6); F f(a);

find_if(s1.begin(), s1.end(), f);

if (find_if(v1.begin(), v1.end(), f) !=v1.end()) {

cout<<"Found!\n";

} else {

cout<<"Not found!\n";

}

return 0;

}

Options:

A.

it will compile successfully


B.

it will display Found!


C.

it will display Not found!


D.

it will not compile successfully

Questions # 67:

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

#include

#include

class A {

public:

virtual int f() { return 10; }

virtual ~A(){}

};

class B: public A {

int f() {return 11; }

virtual ~B(){}

};

int main (){

std::vectorv1;

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

{

i%2>0?v1.push_back(new A()):v1.push_back(new B());

}

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

while(it != v1.end())

{

std::cout<f()<<" ";

v1.pop_back();++it;

}

return 0;

}

Options:

A.

destructor of class A will be called


B.

destructor of class B will be called


C.

code will not compile


D.

program outputs 10 11 10 11 10


E.

program outputs 10 11 10 11 10 11 10 11 10 11


Expert Solution
Questions # 68:

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

#include

#include

using namespace std;

class A

{

int a,b;

public:

A & operator =(const A & c) { a = c.a; return *this;}

A():a(0),b(0){}

void setA(int a) {this?>a = a;} void setB(int b) {this?>b = b;}

int getA() {return a;} int getB() {return b;}

};

int main ()

{

vectorv;

A a;

a.setA(10); a.setB(11);

v.push_back(a);

A b = v.front(); v.pop_back();

cout<

return 0;

}

Options:

A.

program outputs 11 10


B.

compilation error


C.

program outputs 0 10


D.

program outputs 10 0


E.

program outputs 11 0

Viewing page 7 out of 7 pages
Viewing questions 61-70 out of questions