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

Free C++ Institute CPP Practice Exam with Questions & Answers

Questions 1

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

#include <iostream>

#include <algorithm>

#include

#include

using namespace std;

void myfunction(int i) {

cout << " " << i;

}

int main() {

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

vector v1(t, t+10);

set s1(t, t+10);

replace(v1.begin(), v1.end(), 1, 10);

replace(s1.begin(), s1.end(), 1, 10);

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

return 0;

}

Program outputs:

Options:
A.

10 5 2 5 2 4 4 3 3 1

B.

1 10 2 5 2 4 4 3 3 10

C.

compilation error

D.

10 5 2 5 2 4 4 3 3 10

C++ Institute CPP Premium Access
Questions 2

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

#include

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

B operator +(const B &b )const { return B(val + b.val);} };

ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;}

templatestruct Out {

ostream & out;

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

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

template<typename A>

struct Add : public binary_function {

A operator() (const A & a, const A & b) const { return a+b; } };

int main() {

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

deque d1(t, t+10);

deque d2(10);

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

for_each(d2.rbegin(), d2.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

Questions 3

What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: true false<enter>?

#include <iostream>

#include <string>

using namespace std;

int main ()

{

bool a,b;

cin>>boolalpha>>a>>b;

cout<<a<

return 0;

}

Program will output:

Options:
A.

truefalse

B.

true0;

C.

1false

D.

10

E.

none of these

Questions 4

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

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

vector v2(10);

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

reverse_copy(v1.begin(),v1.end(), v2.rbegin());

sort(v2.begin(), v2.end(), less_equal());

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

return 0;

}

Program outputs:

Options:
A.

1 2 3 4 5 6 7 8 9 10

B.

10 9 8 7 6 5 4 3 2 1

C.

no output

D.

compilation error

Questions 5

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

ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;}

templatestruct Out {

ostream & out;

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

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

int main() {

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

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

vector v1(10,0);

sort(t1, t1+5);

sort(t2, t2+5);

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

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

return 0;

}

Program outputs:

Options:
A.

3 2 4 1 5 6 8 2 1 0

B.

1 2 3 4 5 6 8 2 1 0

C.

1 1 2 2 3 4 5 5 6 8

D.

1 2 3 4 5 6 8 0 0 0

E.

compilation error

Questions 6

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

#include

#include <iostream>

#include <algorithm>

using namespace std;

templatestruct 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[]={6,10,8,7,9};

vector v1(10);

sort(t1, t1+5); sort(t2, t2+5);

copy(t1,t1+5,v1.begin());

copy(t2,t2+5,v1.begin()+5);

merge(v1.begin(), v1.begin()+5,v1.end());

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

return 0;

}

Program outputs:

Options:
A.

1 2 3 4 5 6 10 8 7 9

B.

3 2 4 1 5 6 7 8 9 10

C.

3 2 4 1 5 6 10 8 7 9

D.

1 2 3 4 5 6 7 8 9 10

E.

compilation error

Questions 7

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 <string>

#include

#include <algorithm>

using namespace std;

templatestruct Out {

ostream & out;

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

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

int main () {

ifstream f("test.in");

list l;

for( ; f.good() ; ) {

int i;

f>>i;

l.push_back(i);

}

f.close();

for_each(l.begin(), l.end(), Out(cout));

return 0;

}

Program will output:

Options:
A.

1 2 3

B.

1 2 3 3

C.

no output

D.

compilation error

E.

program runs forever without output

Questions 8

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

ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;}

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

vector v1(t, t+10);

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

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 9

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

#include <iostream>

using namespace std;

template <typename T>

class A {

T_v;

public:

A() {}

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

T getV() { return _v; }

void add(T a) { _v+=a; }

template

U get(U a) {

return (U)(_v);

}

};

int main()

{

A a(1);

a.add(10);

cout.setf( ios::showpoint);

cout << a.getV() << " " << a.get(1.0)<<endl;

return 0;

}

Options:
A.

program will display: 11 11

B.

program will not compile

C.

program will display: 11.0000 11

D.

program will display: 11 11.000

Questions 10

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

#include

#include

#include <iostream>

using namespace std;

int main ()

{

vectorv1;

dequed1;

for(int i=0; i<5; i++)

{

v1.push_back(i);v1.push_front(i);

d1.push_back(i);d1.push_front(i);

}

for(int i=0; i

{

cout<<d1[i]<<" "<

}

cout<<endl;

return 0;

}

What will be its output:

Options:
A.

4 4 3 3 2 2 1 1 0 0 0 0 1 1 2 2 3 3 4 4

B.

runtime exception

C.

compilation error due to line 11

D.

compilation error due to line 12