Summer Special 60% Discount Offer - Ends in 0d 00h 00m 00s - Coupon code: bestdeal

Free C++ Institute CPP Practice Exam with Questions & Answers | Set: 2

Questions 11

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

#include <iostream>

#include <algorithm>

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

vector::iterator it = search_n(v.begin(), v.end(), 4, 2);

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

return 0;

}

Program outputs:

Options:
A.

10

B.

3

C.

1

D.

15

E.

compilation error

C++ Institute CPP Premium Access
Questions 12

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

#include <iostream>

using namespace std;

void g(int a)

{

cout<<a?1<<endl;

}

template

void g(A a)

{

cout<<a+1<<endl;

}

int main()

{

int a = 1;

g(a);

return 0;

}

Options:
A.

program displays: 0

B.

program displays: 2

C.

compilation error

D.

runtime exception

Questions 13

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

#include <iostream>

using namespace std;

int main()

{

cout.setf(ios::hex, ios::basefield);

cout<<100<<" ";

cout.flags(ios::showbase);

cout<<100<<" ";

return 0;

}

Program outputs:

Options:
A.

64 64

B.

64 0x64

C.

0x64 0x64

D.

64 100

E.

compilation error

Questions 14

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

#include <iostream>

#include <algorithm>

#include

using namespace std;

void myfunction(pair i) {

cout << " " << i.first;

}

int main() {

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

map m;

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

m[i]=t[i];

}

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

return 0;

}

Program outputs:

Options:
A.

10 5 9 6 2 4 7 8 3 1

B.

0 1 2 3 4 5 6 7 8 9

C.

9 8 7 6 5 4 3 2 1 0

D.

1 3 8 7 4 2 6 9 5 10

E.

compilation error

Questions 15

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

#include <iostream>

using namespace std;

int main()

{

cout<<100<<" ";

cout.setf(ios::hex);

cout<<100<<" ";

return 0;

}

Program outputs:

Options:
A.

100 64

B.

100 0x64

C.

0x64 0x64

D.

64 0x64

E.

100 100

Questions 16

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

#include

#include

#include <iostream>

#include <algorithm>

using namespace std;

templatestruct Out {

ostream & out;

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

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

struct Sequence {

int start;

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

int operator()() { return start++; } };

int main() {

vector v1(10);

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

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

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

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

return 0;

}

Program outputs:

Options:
A.

8 10 5 1 4 6 2 7 9 3

B.

1 2 3 4 5 6 7 8 9 10

C.

compilation error

D.

10 9 8 7 6 5 4 3 2 1

Questions 17

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

#include <iostream>

#include

#include

using namespace std;

int main(){

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

vectorv(t, t+10);

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

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

pair::iterator,multiset::iterator> range;

range = s1.equal_range(6);

while (range.first != range.second) {

cout<<*range.first<<" "; range.first++;

}

return 0;

}

Options:
A.

program outputs: 6 6

B.

program outputs: 5 7

C.

program outputs: 5 5 6 6 7 7

D.

program outputs: 5 5 7 7

E.

program outputs: 1 1 6 6 5 5

Questions 18

Which changes, introduced independently, will allow the code to compile and display “one” “eight” “nine” “ten”? Choose all that apply

#include <iostream>

#include

#include <string>

using namespace std;

class A {

int a;

public:

A(int a):a(a){}

int getA() const { return a;}

/* Insert Code Here 1 */

};

/* Insert Code Here 2 */

int main(){

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

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

map m;/* Replace Code Here 3 */

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

m.insert(pair(A(t[i]),s[i]));

}

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

map::iterator i=m.begin(); /* Replace Code Here 4 */

for( ;i!= m.end(); i++) {

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

}

cout<<endl;

return 0;

}

Options:
A.

operator int() const { return a;} inserted at Place 1

B.

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

C.

bool operator < (const A & b) const { return b.a<a;} inserted at Place 1

D.

struct R { bool operator ()(const A & a, const A & b) { return a.getA()

replacing line marked 3 with map m;

replacing line marked 4 with map::iterator i=m.begin();

Questions 19

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

#include <iostream>

#include <algorithm>

#include

using namespace std;

int main () {

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

vector v (t,t+10);

vector::iterator it;

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

it = find_end (v.begin(), v.end(), m1, m1+3);

if (it != v.end())

cout << "Found at position: " << it?v.begin() << endl;

return 0;

}

Options:
A.

program outputs: Found at position: 5

B.

program outputs: Found at position: 0

C.

no output

D.

program outputs: Found at position: 10

Questions 20

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

#include

#include <iostream>

#include <algorithm>

#include <functional>

using namespace std;

templatestruct Out {

ostream & out;

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

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

struct Add : public binary_function {

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

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