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

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

return 0;

}

Program outputs:

A. 10

B. 3

C. 1

D. 15

E. compilation error

Buy Now
Questions 5

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

#include

#include

#include

using namespace std;

templatestruct Out {

ostream and out;

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

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

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<

return 0;

}

Program outputs:

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

Buy Now
Questions 6

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

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

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

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

cout<second<<" ";

}

cout<

return 0;

}

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

B. bool operator < (const A and b) const { return a

C. bool operator < (const A and b) const { return b.a

D. struct R { bool operator ()(const A and a, const A and b) { return a.getA() m; replacong line marked 4 with multimap::iterator i=m.begin();

Buy Now
Questions 7

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;

}

A. line I

B. line II

C. line III

D. line IV

E. line V

Buy Now
Questions 8

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

Buy Now
Questions 9

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

B operator ?(const B andb )const { return B(val ? b.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[]={1,2,3,4,5,6,7,8,9,10};

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

vector v1(t1, t1+10);

vector v2(t2, t2+10);

vector v3(10);

transform(v1.begin(), v1.end(), v2.rbegin(), v3.begin(), minus());

for_each(v3.rbegin(), v3.rend(), Out(cout));cout<

return 0;

}

Program outputs:

A. 9 7 5 3 1 ?1 ?3 ?5 ?7 ?9

B. ?1 ?3 ?5 ?7 ?9 9 7 5 3 1

C. 1 3 5 7 9 ?1 ?3 ?5 ?7 ?9

D. 1 3 5 7 9 ?1 ?3 ?5 ?7 ?9

E. ?9 ?7 ?5 ?3 ?1 1 3 5 7 9

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 and out;

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

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

};

int main() {

char s[]={"qwerty"};

char t1[]={"ert"};

char t2[]={"ERT"};

sort(s, s+6);

cout<

return 0;

} Program outputs:

A. 0 0

B. 0 1

C. 1 0

D. 1 1

Buy Now
Questions 11

What happens 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, 6, 5, 7, 9, 8, 0 };

vector v(t, t + 10);

map m;

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

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

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

}

pair::iterator, map::iterator> range;

range = m.equal_range(6);

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

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

}

return 0;

}

A. program outputs: 6

B. program outputs: 5 7

C. program outputs: 6 7

D. program outputs: 1 5

E. program outputs: 6 5

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[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };

multiset s1(t,t+10);

s1.insert(s1.find(7), 3);

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

cout<<*i<<" ";

}

return 0;

}

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

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

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

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

E. runtime exception

Buy Now
Questions 13

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

void add(string and a);

};

template

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

void A::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

Buy Now
Exam Code: CPP
Exam Name: C++ Certified Professional Programmer
Last Update: Nov 16, 2024
Questions: 228
10%OFF Coupon Code: SAVE10

PDF (Q&A)

$49.99

VCE

$55.99

PDF + VCE

$65.99