Free C++ Institute CPP Exam Actual Questions

The questions for CPP were last updated On Nov 3, 2024

Question No. 1

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

#include

#include

#include

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

templatestruct Out {

ostream & out;

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

void operator() (const T & val ) { out<

struct Add : public binary_function {

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

return a+b; } };

int main() {

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

return 0;

}

Program outputs:

Show Answer Hide Answer
Correct Answer: D

Question No. 2

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

#include

using namespace std;

class C {

public:

int _c;

C():_c(0){}

C(int c) { _c = c;}

C operator+=(C & b) {

C tmp;

tmp._c = _c+b._c;

return tmp;

}

};

template

class A {

T_v;

public:

A() {}

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

T getV() { return _v; }

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

};

int main()

{

A b(2);

Aa (5);

Cc;

a.add(c);

cout << a.getV() <

return 0;

}

Show Answer Hide Answer
Correct Answer: B

Question No. 3

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

#include

using namespace std;

class B {};

template

class A {

T_v;

public:

A() {}

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

T getV() { return _v; }

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

};

int main()

{

A a(1);

Ab;

a.add(10);

cout << a.getV() <

return 0;

}

Show Answer Hide Answer
Correct Answer: A, C

Question No. 4

Which sentence is correct about the code below? Choose all that apply.

#include

#include

#include

using namespace std;

class F {

int val;

public:

F(int v):val(v){}

bool operator() (int v) {

if (v == val) return true;

return false;

}

};

int main() {

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

vector v1(t, t + 10);

if (find(v1.begin(), v1.end(), 6) == find(v1.begin(), v1.end(), F(6))) {

cout<<"Found!\n";

} else {

cout<<"Not found!\n";

}

return 0;

}

Show Answer Hide Answer
Correct Answer: D

Question No. 5

Which lines of the code below contain proper instantiation of queue objects?

#include

#include

#include

#include

#include

using namespace std;

int main()

{

deque mydeck;

list mylist;

vector myvector;

queue first; // line I

queue second(mydeck);// line II

queue third(second);// line III

queue fourth(mylist);// line IV

queue fifth(myvector);// line V

return 0;

}

Show Answer Hide Answer
Correct Answer: A, B, C