Vcehome > C++ Institute > C++ Certified Associate Programmer > CPP > CPP Online Practice Questions and Answers

CPP Online Practice Questions and Answers

Questions 4

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 and v) const { return val>v.val;} };

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

templatestruct Out {

ostream and out;

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

void operator() (const T and 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:

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

Browse 228 Q&As
Questions 5

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 and v) const { return val

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

templatestruct Out {

ostream and out;

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

void operator() (const T and 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());

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

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

return 0;

}

Program outputs:

A. 5 6 7 8 9 10

B. 4 5 6 7 8 9 10

C. 6 7 8 9 10

D. 1 2 3 4 5

E. 1 2 3 4

Browse 228 Q&As
Questions 6

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 and v) const { return val

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

templatestruct Out {

ostream and out;

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

void operator() (const T and 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_symmetric_difference(t2,t2+5,t1,t1+5,v1.begin());

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

return 0;

}

Program outputs:

A. 6 8 3 4 0 0 0 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. 3 4 6 8 0 0 0 0 0 0

Browse 228 Q&As
Questions 7

What will happen 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);

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

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

bool found = s1.find(7);

if (found){

cout<<"Element found!\n";

}else {

cout<<"Element not found!\n";

}

return 0;

}

A. program will display "Element found!"

B. program will display "Element not found!\n"

C. code will not compile

D. changing type of variable found to int will make this code compile

Browse 228 Q&As
Questions 9

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

#include

#include

using namespace std;

template

class A {

T_v;

public:

A() {}

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

T getV() { return _v; }

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

void add(string and a) {

_v.insert(0, a);

}

};

int main()

{

Aa("Hello");

string s(" world!");

a.add(s);

cout << a.getV() <

return 0;

}

A. program will display: Hello world!

B. compilation error

C. program will display: world!Hello

D. program will run without any output

Browse 228 Q&As
Questions 10

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 start++;

}

};

int main() {

vector v1(10);

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

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

cout<

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

Browse 228 Q&As
Questions 11

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;

}

A. line I

B. line II

C. line III

D. line IV

E. line V

Browse 228 Q&As
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 ,4 ,5};

vectorv1(t, t+5);

dequed1;

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

for(int i=0; i

{

cout<

}

cout<

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

Browse 228 Q&As
Questions 13

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, 6 , 7, 8 , 9, 10};

vectorv1(t, t+10);

dequed1(t, t+10);

d1.empty();

v1.empty();

if (v1.isempty())

{

cout<<"I am empty ";

}

else

{

cout<<"I am not empty ";

}

cout<

return 0;

}

A. program outputs: I am empty 0 0

B. program outputs: I am not empty 0 0

C. compilation error

D. program outputs: I am not empty 10 10

Browse 228 Q&As
Exam Code: CPP
Exam Name: C++ Certified Professional Programmer
Last Update: May 14, 2024
Questions: 228 Q&As

PDF

$49.99

VCE

$59.99

PDF + VCE

$67.99