New Year Sale - Special 70% Discount Offer - Ends in 0d 00h 00m 00s - Coupon code: 70dumps

CPP Questions and Answers

Question # 6

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

#include

#include <vector>

#include <iostream>

using namespace std;

int main ()

{

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

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

deque<int>d1;

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

for(int i=0; i

{

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

}

cout<<endl;

return 0;

}

A.

program outputs 5 4 3 2 1

B.

program outputs 1 2 3 4 5

C.

compilation error in line 8

D.

compilation error in line 10

E.

segmentation fault runtime exception

Full Access
Question # 7

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

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

bool compare(int a, int b) { return a == b; }

int main () {

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

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

vector<int>::iterator it = v.begin();

int m1[] = {1, 2, 3};

while ( (it = find_first_of (it, v.end(), m1, m1+3)) != v.end()) {

cout<<it?v.begin()<<" ";

}

cout<< endl;

return 0;

}

A.

program outputs: 0 1 2 5 6 7

B.

program outputs: 0 5

C.

program outputs: 0 0

D.

compilation error

E.

program will run forever

Full Access
Question # 8

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

#include <iostream>

#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<int, string> m;

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

m.insert(pair<int, string>(t[i], s[i]));

}

if (m.count(3) == 2) {

m.erase(3);

}

for (map<int, string>::iterator i = m.begin(); i != m.end(); i++) {

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

}

return 0;

}

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 3 3 4 4 5 5

E.

program outputs: one two three four five

Full Access
Question # 9

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

#include

#include <iostream>

#include <algorithm>

#include <functional>

using namespace std;

template<class T>struct Out {

ostream & out;

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

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

struct Add : public binary_function<int, int, int> {

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

return a+b;

}

};

int main() {

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

deque<int> d1(t, t+10);

deque<int> d2(10);

transform(d1.begin(), d1.end(), d2.begin(), bind2nd(Add(), 1));

for_each(d2.rbegin(), d2.rend(), Out<int>(cout));cout<<endl;

return 0;

}

Program outputs:

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

Full Access
Question # 10

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

#include <vector>

#include

#include <iostream>

#include <algorithm>

using namespace std;

template<class T>struct Out {

ostream & out;

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

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

template <typename T> struct Sequence {

T start; T step;

Sequence(T start, T step):start(start), step(step){}

T operator()() { T v = start; start+=step; return v; } };

bool Less(float a, float b) { return int(a)<int(b);}

int main() {

float t[]={2.28, 1.66, 1.32, 3.94, 3.64, 2.3, 2.98, 1.96, 2.62, 1.13};

vector v1; v1.assign(t, t+10);

stable_sort(v1.begin(), v1.end(), Less);

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

return 0;

}

Program outputs:

A.

1.66 1.32 1.96 1.13 2.28 2.3 2.98 2.62 3.94 3.64

B.

1.13 1.32 1.66 1.96 2.28 2.3 2.62 2.98 3.64 3.94

C.

compilation error

D.

3.94 3.64 2.98 2.62 2.3 2.28 1.96 1.66 1.32 1.13

E.

the exact output is impossible to determine

Full Access
Question # 11

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

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

void print(int v) {

cout<<v<<" ";

}

struct Sequence {

int start;

Sequence(int start):start(start){}

int operator()() {

return start++;

}

};

int main() {

vector<int> v1(10);

generate_n(v1.begin(), 10, Sequence(1));

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

cout<<endl;

return 0;

}

Program outputs:

A.

1 2 3 4 5 6 7 8 9 10

B.

0 0 0 0 0 0 0 0 0 0

C.

compilation error

D.

no output

Full Access
Question # 12

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

#include

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

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

deque d1(t, t+10);

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

deque::iterator it = upper_bound(d1.begin(), d1.end(), B(4), greater());

for_each(it, d1.end(), Out(cout)); cout<<endl;

return 0;

}

Program outputs:

A.

5 6 7 8 9 10

B.

4 5 6 7 8 9 10

C.

compilation error

D.

1 2 3 4 5

E.

1 2 3 4

Full Access
Question # 13

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

#include <iostream>

using namespace std;

void g(int a)

{

cout<<a?1<

}

template<class A>

void g(A a)

{

cout<<a+1<

}

int main()

{

int a = 1;

g(a);

return 0;

}

A.

program displays: 0

B.

program displays: 2

C.

compilation error

D.

runtime exception

Full Access
Question # 14

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

#include <iostream>

#include <algorithm>

#include <vector>

#include

#include

using namespace std;

void myfunction(int i) {

cout << " " << i;

}

int add (int a, int b) { return a+b; }

int main() {

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

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

set<int> s1(t, t+10);

deque<int> d1;

d1.resize(s1.size());

transform(s1.begin(), s1.end(), v1.begin(), d1.begin(), add);

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

return 0;

}

Program outputs:

A.

0 0 0 0 0 0 0 0 0 0

B.

11 7 12 10 7 10 14 16 12 11

C.

compilation error

D.

runtime exception

E.

20 10 18 12 4 8 14 16 6 2

Full Access
Question # 15

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

#include

#include <iostream>

using namespace std;

bool mycomparison (int first, int second){return first>second;}

template<class T>

void print(T start, T end) {

while (start != end) {

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

}

}

int main()

{

int t1[] ={ 1, 7, 8, 4, 5 };

list<int> l1(t1, t1 + 5);

int t2[] ={ 3, 2, 6, 9, 0 };

list<int> l2(t2, t2 + 5);

l1.sort(mycomparison);

l2.sort(mycomparison);

l1.merge(l2,mycomparison);

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

print(l2.begin(), l2.end()); cout<<endl;

return 0;

}

A.

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

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: 9 8 7 6 5 4 3 2 1 0 9 6 3 2 0

D.

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

E.

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

Full Access
Question # 16

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

#include <vector>

#include <iostream>

using namespace std;

class A

{

int a,b;

public:

A(const A & c) { a = c.a; }

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

{

vector<A>v;

A a;

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

v.push_back(a);

cout<<v[0].getB()<<" "<<v[0].getA()<

return 0;

}

A.

program outputs 10 11

B.

the result is unpredictable

C.

program outputs 10 0

D.

program outputs 11 0

E.

compilation error

Full Access
Question # 17

What will happen when you attempt to compile and run the code below, assuming that file test.in contains the following sequence: 1 2 3?

#include <iostream>

#include

#include

#include

#include <algorithm>

using namespace std;

template<class T>struct Out {

ostream & out;

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

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

int main () {

ifstream f("test.in");

list<int> l;

for( ; !f.fail() ; ) {

int i;

f>>i;

l.push_back(i);

}

f.close();

for_each(l.begin(), l.end(), Out<int>(cout));

return 0;

}

Programwill output:

A.

1 2 3

B.

1 2 3 3

C.

no output

D.

compilation error

E.

program runs forever without output

Full Access
Question # 18

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

#include

#include <iostream>

#include <algorithm>

#include

using namespace std;

template<class T>struct Out {

ostream & out;

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

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

int main() {

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

deque<int> d1(t, t+10);

set<int> s1(t,t+10);

cout<<binary_search(s1.begin(),s1.end(), 4)<<" "<

return 0;

}

Choose all possible outputs (all that apply):

A.

1 0

B.

1 1

C.

true true

D.

false false

E.

compilation error

Full Access
Question # 19

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

#include

#include

#include <iostream>

using namespace std;

template<class T> 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<<endl;

return 0;

}

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

Full Access
Question # 20

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

#include <iostream>

#include <algorithm>

#include <vector>

#include

#include

using namespace std;

struct display {

void operator() (int i) {cout << " " << i;}

};

int main() {

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

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

deque<int> d1(t, t + 10);

set<int> s1(t, t + 10);

for_each(v1.begin(), v1.end(), display); //Line I

for_each(d1.begin(), d1.end(), *(new display())); // Line II

for_each(s1.begin(), s1.end(), display()); // Line III

return 0;

}

A.

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

B.

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

C.

compilation error in line I

D.

compilation error in line II

E.

compilation error in line III

Full Access
Question # 21

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

#include <iostream>

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

};

int main () {

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

deque<int> d (t,t+15);

int number = count(d.begin(), d.end(), 2);

cout<< number<

return 0;

}

Program outputs:

A.

4

B.

3

C.

2

D.

0

E.

compilation error

Full Access
Question # 22

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

#include

#include <iostream>

#include <algorithm>

using namespace std;

template<class T>struct Out {

ostream & out;

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

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

int main() {

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

deque<int> d1(t, t+10);

deque<int>::iterator it = lower_bound(d1.begin(), d1.end(), 4);

for_each(it, d1.end(), Out<int>(cout));cout<<endl;

return 0;

}

Program outputs:

A.

8 10 5 1 4 6 2 7 9 3

B.

4 5 6 7 8 9 10

C.

1 2 3 4 5 6 7 8 9 10

D.

compilation error

E.

1 2 3 4

Full Access
Question # 23

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

#include <vector>

#include <iostream>

#include <algorithm>

#include <functional>

using namespace std;

template<class T>struct Out {

ostream & out;

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

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

struct Add : public binary_function<int, int, int> {

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

return a+b;

}

};

int main() {

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

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

vector<int> v2(10);

transform(v1.begin(), v1.end(), v2.begin(), bind1st(Add(), 1));

for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;

return 0;

}

Program outputs:

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

Full Access
Question # 24

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

#include <iostream>

#include

#include

#include

using namespace std;

int main ()

{

string s;

getline(cin, s);

stringstream input(s);

stringstream output;

for( ; !input.fail() ; )

{

int i;

input>>hex>>i;

output<

}

cout<<output.str();

return 0;

}

What will be the result assuming that user will enter following sequence: 64 100:

A.

64 100

B.

100 256

C.

100 256 256

D.

0x64 0x100

E.

0x100 0x256 0x256

Full Access
Question # 25

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

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

template<class T>struct Out {

ostream & out;

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

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

int main() {

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

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

vector<int> v1(10);

sort(t1, t1+5);

sort(t2, t2+5);

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

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

return 0;

}

Program outputs:

A.

1 2 3 4 5 6 8 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.

1 2 5 0 0 0 0 0 0 0

Full Access
Question # 26

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

#include <iostream>

#include <algorithm>

#include

using namespace std;

int main() {

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

map<int, int> m;

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

m[i]=t[i];

}

map<int, int>::iterator it = find(m.begin(), m.end(), 5);

cout<<it?>first;

return 0;

}

Program outputs:

A.

5

B.

4

C.

10

D.

compilation error

Full Access
Question # 27

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;

}

A.

it will compile and print 7

B.

it will not compile

C.

it will compile but the program result is unpredictable

D.

adding code:

bool operator !=(const A & b) const {

if (this?>a != b.a) { return true; } return false; }

at Place 1 will allow the program to compile

Full Access
Question # 28

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

#include <iostream>

using namespace std;

template<class A>

void f(A a)

{

cout<<1<

}

void f(int a)

{

cout<<2<

}

int main()

{

int a = 1;

f(a);

return 0;

}

A.

program displays: 1

B.

program displays: 2

C.

compilation error

D.

runtime exception

Full Access
Question # 29

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

#include

#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

{

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

}

cout<<endl;

return 0;

}

A.

program displays 5 4 3 2 1

B.

program displays 1 2 3 4 5

C.

compilation error

D.

segmentation fault runtime exception

Full Access
Question # 30

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

#include <vector>

#include <iostream>

#include <algorithm>

#include <functional>

using namespace std;

class B { int val;

public:

B(int v=0):val(v){}

int getV() const {return val;}

operator int () const { return val;} };

template<class T>struct Out {

ostream & out;

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

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

int main() {

B 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<<endl;

return 0;

}

Program outputs:

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

Full Access
Question # 31

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

#include <iostream>

#include

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

template<class T>struct 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<<i<<" ";

}

f.close();

return 0;

}

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

Full Access
Question # 32

What will be output of the program when you attempt to compile and run the following code?

#include <iostream>

#include

#include <vector>

#include

using namespace std;

int main(){

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

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

multimap<int,string> m;

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

m.insert(pair<int,string>(second[i],first[i]));

}

m[0]="ten";

m.insert(pair<int,string>(1,"eleven"));

for(multimap<int, string>::iterator i=m.begin();i!= m.end(); i++) {

cout<<i?>second<<" ";

}

return 0;

}

A.

zero one two three four five six seven eight nine

B.

ten one two three four five six seven eight nine

C.

zero eleven two three four five six seven eight nine

D.

ten eleven two three four five six seven eight nine

E.

compilation error

Full Access
Question # 33

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

#include

#include <vector>

#include <iostream>

using namespace std;

template<typename T>

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

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

deque<int>d1(t+5, t+10);

cout<<calculate(t,t+10)<<" ";

cout<<calculate(v1.begin()+1,v1.end()?2)<<" ";

cout<<calculate(d1.rbegin()+1,d1.rend()?2)<<" ";

cout<<calculate(t[0],t[10])<<" ";

cout<<endl;

return 0;

}

A.

compilation error

B.

runtime exception

C.

program outputs 55 5 17 55

D.

program outputs 55 5 17 0

Full Access
Question # 34

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

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

template<class T>struct Out {

ostream & out;

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

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

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<int> v1(t, t+10);

vector<int> v2(10);

transform(v1.begin(), v1.end(), v2.begin(), bind1st(Add(),1));

for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;

return 0;

}

Program outputs:

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

Full Access