Question 6 of 228

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

#include <vector>

#include <iostream>

#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>v.val;} }; ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;} template<class T>struct Out { ostream & out;

Out(ostream & o): out(o){}

void operator() (const T & val ) { out<<val<<" "; } };

int main() {

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

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

vector<B> v1(10,0);

sort(t1, t1+5);

sort(t2, t2+5);

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

for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl; return 0;

}

Program outputs:

Answer

Suggested Answer

The suggested answer is D.

Question 7 of 228

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

#include <list>

#include <vector>

#include <iostream>

using namespace std;

int main ()

{

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

vector<int>v1(t, t+5);

list<int>l1;

l1.assign(v1.end(), v1.begin());

for(int i=0; i<l1.size(); i++)

{

cout<<l1.at(i)<<" ";

}

cout<<endl;

return 0;

}

Answer

Suggested Answer

The suggested answer is C.

Question 8 of 228

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

#include <vector>

#include <iostream>

#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<v.val;} }; ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;}

template<class T>struct Out {

ostream & out;

Out(ostream & o): out(o){}

void operator() (const T & val ) { out<<val<<" "; } };

int main() {

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

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

vector<B> 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<B>(cout));cout<<endl; return 0;

}

Program outputs:

Answer

Suggested Answer

The suggested answer is E.

Question 9 of 228

Which sentence is correct about the code below?

#include <iostream>#include <algorithm>#include <vector>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; }/* Insert Code Here */};
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<A> v1(t, t + 10);for_each(v1.begin(), v1.end(), add10());vector<A>::iterator it = find(v1.begin(), v1.end(), A(7)); cout << it?>getA() << endl; return 0;}
Answer

Suggested Answer

The suggested answer is B.

Question 10 of 228

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

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

void myfunction(int i) {

cout << " " << i;

}

void multiply (int a) {

a*2;

}

int main() {

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

vector<int> v1(t, t+10);

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

iter_swap(v1.begin(),t+9);

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

return 0;

}

Program outputs:

Answer

Suggested Answer

The suggested answer is A.