Labour Day Sale - Limited Time 60% Discount Offer - Ends in 0d 00h 00m 00s - Coupon code: 575363r9

Welcome To DumpsPedia

CPP Sample Questions Answers

Questions 4

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

#include

#include

using namespace std;

template

void print(T start, T end) {

while (start != end) {

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

}

}

int main()

{

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

list l1(t1, t1 + 5);

l1.remove(2);

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

return 0;

}

Options:

A.

program outputs: 1 2 4 5

B.

program outputs: 3 4 5

C.

program outputs: 1 3 4 5

D.

program outputs: 4 5

Buy Now
Questions 5

Which are NOT valid instantiations of priority_queue object:

#include

#include

#include

#include

#include

using namespace std;

int main()

{

deque mydeck;list mylist; vector myvector;

priority_queue first;//line I

priority_queue > second;//line II

priority_queue third(first);//line III

priority_queue > fourth(third);//line IV

priority_queue > fifth(myvector.begin(), myvector.end());//line V

return 0;

}

Options:

A.

line I

B.

line II

C.

line III

D.

line IV

E.

line V

Buy Now
Questions 6

Which changes introduced independently will allow code to compile and display 0 1 8 9 (choose all that apply)

#include

#include

#include

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

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

vectorv(t, t+10);

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

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

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

for(set::iterator i=s1.begin();i!= s1.end(); i++) {

cout<getA()<<" ";

}

cout<

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

D.

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

Buy Now
Questions 7

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):val(v){}

int getV() const {return val;} bool operator < (const B & v) const { return val

ostream & operator <<(ostream & out, const B & v) { out<

templatestruct Out {

ostream & out;

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

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

int main() {

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

deque d1(t, t+10);

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

set s1(t,t+10);

cout<

return 0;

}

Program outputs:

Options:

A.

1 0

B.

1 1

C.

0 0

D.

0 1

E.

compilation error

Buy Now
Questions 8

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

#include

#include

#include

#include

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

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

};

struct display { void operator() (const A & a) {cout << " " << a.getA();} };

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

set s1(t, t + 10);

for_each(v1.begin(), v1.end(), add10()); for_each(v1.begin(), v1.end(), display());

for_each(s1.begin(), s1.end(), add10()); for_each(s1.begin(), s1.end(), display());

return 0;

}

Options:

A.

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

B.

program outputs: 20 15 19 16 12 14 17 18 13 11 1 2 3 4 5 6 7 8 9 10

C.

program outputs: 20 15 19 16 12 14 17 18 13 11 11 12 13 14 15 16 17 18 19 20

D.

compilation error

Questions 9

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

#include

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

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

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

}

for (map::iterator i = m.begin(); i != m.end(); i++) {

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

}

return 0;

}

Options:

A.

program outputs: 1 2 3 4 5

B.

compilation error

C.

program outputs: 1 1 2 2 3 3 4 4 5 5

D.

program outputs: one two three four five

E.

program outputs: one one two two three three four four five five

Buy Now
Questions 10

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

#include

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

int main() {

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

deque d1(t, t+10);

set s1(t,t+10);

cout<

return 0;

}

Choose all possible outputs (all that apply):

Options:

A.

1 0

B.

1 1

C.

true true

D.

false false

E.

compilation error

Buy Now
Questions 11

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):val(v){}

int getV() const {return val;} bool operator < (const B & v) const { return val

ostream & operator <<(ostream & out, const B & v) { out<

templatestruct Out {

ostream & out;

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

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

int main() {

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

deque d1(t, t+10);

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

set s1(t,t+10);

cout<

return 0;

}

Program outputs:

Options:

A.

1 0

B.

1 1

C.

true true

D.

false false

E.

compilation error

Buy Now
Questions 12

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

#include

#include

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

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

cout<< number<

return 0;

}

Program outputs:

Options:

A.

4

B.

3

C.

2

D.

0

E.

compilation error

Buy Now
Questions 13

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

#include

#include

using namespace std;

int main ()

{

int a,b,c;

cin>>a>>b>>c;

cout<

return 0;

}

Program will output:

Options:

A.

123

B.

1 2 3

C.

1.12.23.3

D.

1.1 2.2 3.3

E.

none of these

Buy Now
Questions 14

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

#include

#include

#include

#include

#include

using namespace std;

int main() {

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

vector v(t, t + 10);

multimap m;

for (vector::iterator i = v.begin(); i != v.end(); i++) {

stringstream s;s << *i << *i;

m.insert(pair(*i, s.str()));

}

pair::iterator, multimap::iterator> range;

range = m.equal_range(2);

for (multimap::iterator i = range.first; i != range.second; i++) {

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

}

return 0;

}

The output will be:

Options:

A.

2 2

B.

1 2

C.

1 3

D.

2

E.

0 2

Buy Now
Questions 15

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

templatestruct Out {

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

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

int main() {

int t[]={20, 30, 10, 20, 30, 10, 20, 30, 10, 20};

deque d1(t, t+10);

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

pair ::iterator, deque::iterator > result = equal_range(d1.begin(), d1.end(), B(20), greater());

for_each(result.first, result.second, Out(cout));cout<

return 0;

}

Program outputs:

Options:

A.

30 30 30 20 20 20 20 10 10 10

B.

20 20 20 20

C.

30 20 20 20 10

D.

20 20 20 20 10

E.

30 20 20 20 20 10

Buy Now
Questions 16

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

#include

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

bool Greater(int v1, int v2) { return v1

int main() {

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

vector v1(t, t+10);

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

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

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

Buy Now
Questions 17

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):val(v){}

operator int() { return val;}

};

templatestruct Out {

ostream & out;

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

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

struct Sequence { int start;

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

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

bool predicate(int v) { return v%2==0; }

int main() {

vector v1(10);

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

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

return 0;}

Program outputs:

Options:

A.

1 3 5 7 9 6 7 8 9 10

B.

1 3 5 7 9

C.

2 4 6 8 10

D.

compilation error

E.

no output

Buy Now
Questions 18

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

#include

#include

#include

using namespace std;

template

void print(T start, T end) {

while (start != end) {

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

}

}

int main()

{

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

list l1(t1, t1 + 5);

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

deque d1(t2, t2 + 5);

l1.sort();

d1.sort();

l1.merge(d1);

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

print(d1.begin(), d2.end()); cout<

return 0;

}

Options:

A.

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

B.

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

C.

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

D.

compilation error

Buy Now
Questions 19

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

#include

#include

#include

using namespace std;

void print(int v) { cout<

struct Sequence {

int start;

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

int operator()() {

return 10*(1+(start++ %3));

}

};

int main() {

vector v1(10);

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

remove(v1.begin(), v1.end(), 10);

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

return 0;

}

Program outputs:

Options:

A.

20 30 10 20 30 10 20 30 10 20

B.

20 30 20 30 20 30 20

C.

20 30 20 30 20 30 20 30 10 20

D.

compilation error

Buy Now
Questions 20

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

templatestruct Out {

ostream & out;

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

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

int main() {

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

vector v1(t,t+10);

cout<<*max_element(v1.begin(), v1.end(), greater());

cout<

return 0;

}

Program outputs:

Options:

A.

3

B.

1

C.

6

D.

10

E.

compilation error

Buy Now
Questions 21

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

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

void operator()(const T & val ) {

out<

}

};

struct Sequence {

int start;

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

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

};

struct Odd { bool operator()(int v) { return v%2==0; }};

int main() {

vector v1(10);

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

partition(v1.begin(),v1.end(), Odd());

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

return 0;

}

Choose all possible outputs:

Options:

A.

1 2 3 4 5 6 7 8 9 10

B.

5 7 3 9 1 10 2 8 4 6

C.

10 2 8 4 6 5 7 3 9 1

D.

4 6 8 10 2 7 5 3 1 9

E.

2 4 6 8 10 1 3 5 7 9

Buy Now
Questions 22

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

#include

#include

class A {

public:

virtual int f() { return 10; }

virtual ~A(){}

};

class B: public A {

int f() {return 11; }

virtual ~B(){}

};

int main (){

std::vectorv1;

for(int i = 10; i>0; i??)

{

i%2>0?v1.push_back(new A()):v1.push_back(new B());

}

std::vector::iterator it = v1.begin();

while(it != v1.end())

{

std::cout<f()<<" ";

v1.pop_back();++it;

}

return 0;

}

Options:

A.

destructor of class A will be called

B.

destructor of class B will be called

C.

code will not compile

D.

program outputs 10 11 10 11 10

E.

program outputs 10 11 10 11 10 11 10 11 10 11

Buy Now
Questions 23

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

#include

using namespace std;

int main()

{

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

cout<<100<<" ";

cout.setf(ios::showbase);

cout<<100<<" ";

return 0;

}

Program outputs:

Options:

A.

144 0144

B.

144 0x64

C.

0x144 0144

D.

0144 100

E.

compilation error

Buy Now
Questions 24

Which sentence is correct about the code below?

#include

#include

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

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

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

vector::iterator it = find(v1.begin(), v1.end(), A(7));

cout << it?>getA() << endl;

return 0;

}

Options:

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

Questions 25

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

#include

#include

#include

using namespace std;

int main () {

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

vector v (t,t+10);

vector::iterator it;

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

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

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

return 0;

}

Program outputs:

Options:

A.

found at position: 5

B.

found at position: 0

C.

found at position: 6

D.

found at position: 1

E.

found at position: 10

Buy Now
Questions 26

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

#include

#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

Buy Now
Questions 27

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

#include

#include

#include

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<

templatestruct Out {

ostream & out;

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

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

int main() {

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

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

vector 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(cout));cout<

return 0;

}

Program outputs:

Options:

A.

compilation error

B.

1 2 3 4 5 6 8 0 0 0

C.

1 2 3 4 5 6 8 2 1 0

D.

5 2 1 0 0 0 0 0 0 0

E.

1 2 5 0 0 0 0 0 0 0

Buy Now
Questions 28

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

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

int main() {

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

vector v1(t,t+10);

cout<<*max_element(v1.begin(), v1.end());

return 0;

}

Program outputs:

Options:

A.

3

B.

1

C.

6

D.

10

E.

compilation error

Buy Now
Questions 29

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

#include

#include

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

operator int() const {return a;}

};

int main () {

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

set s (t,t+15);

cout<

return 0;

}

Program outputs:

Options:

A.

true

B.

false

C.

1

D.

0

E.

compilation error

Questions 30

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

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

int main() {

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

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

vector 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(cout));cout<

return 0;

}

Program outputs:

Options:

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

Buy Now
Questions 31

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

#include

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

int Add(int a, int b) {

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(), bind2nd(ptr_fun (Add),1));

vector::iterator it = find_if(v2.begin(), v2.end(),bind2nd(equal_to(),10));

cout<<*it<

return 0;

}

Program outputs:

Options:

A.

false

B.

true

C.

10

D.

0

E.

compilation error

Buy Now
Questions 32

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

#include

#include

#include

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<second<<" ";

}

cout<

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

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

Buy Now
Questions 33

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

#include

#include

#include

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<

templatestruct Out {

ostream & out;

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

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

int main() {

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

vector v1(t,t+10);

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

cout<<*min_element(v1.begin(), v1.end());

return 0;

}

Program outputs:

Options:

A.

3

B.

1

C.

6

D.

10

E.

compilation error

Buy Now
Questions 34

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

#include

#include

using namespace std;

int main ()

{

bool a,b;

cin>>boolalpha>>a>>b;

cout<

return 0;

}

Program will output:

Options:

A.

truefalse

B.

true0;

C.

1false

D.

10

E.

none of these

Buy Now
Exam Code: CPP
Exam Name: C++ Certified Professional Programmer
Last Update: May 1, 2024
Questions: 228
$64  $159.99
$48  $119.99
$40  $99.99
buy now CPP