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

#include

using namespace std;

void myfunction(int i) {

cout << " " << i;

}

int main() {

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

set s1(t, t+10);

vector v1(s1.rbegin(), s1.rend());

swap_ranges(s1.begin(), s1.end(), v1.begin());

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

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

return 0;

}

Program outputs:

A. 10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10

B. compilation error

C. 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

D. 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1

E. 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1

Buy Now
Questions 5

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

#include

#include

#include

#include

using namespace std;

void print(int v) { cout<

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

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

remove_if(s1.begin(), s1.end(), predicate);

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

return 0;

}

Program outputs:

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

Buy Now
Questions 6

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, 7, 8, 4, 5 };

list l1(t1, t1 + 5);

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

list l2(t2, t2 + 5);

l1.sort();

list::iterator it = l2.begin();

it++; it++;

l1.splice(l1.end(),l2, it, l2.end());

print(l1.begin(), l1.end()); cout<<"Size:"<

print(l2.begin(), l2.end()); cout<<"Size:"<

return 0;

}

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

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

C. compilation error

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

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

Buy Now
Questions 7

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

#include

#include

#include

#include

#include

using namespace std;

struct display {

void operator() (int i) {cout << " " << i;}

};

int main() {

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

vector v1(t, t + 10);

deque d1(t, t + 10);

set s1(t, t + 10);

for_each(v1.begin(), v1.end(), display); //Line I

for_each(d1.begin(), d1.end(), *(new display())); // Line II

for_each(s1.begin(), s1.end(), display()); // Line III

return 0;

}

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

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

C. compilation error in line I

D. compilation error in line II

E. compilation error in line III

Buy Now
Questions 8

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

vector v1(t, t+10);

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

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

}

Program outputs:

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 9

Which keywords can be used to define template type parameters? Choose all possible answers:

A. class

B. typedef

C. typename

D. static

E. volatile

Buy Now
Questions 10

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

#include

#include

#include

using namespace std;

int main(){

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

vectorv(myints, myints+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<<*i<<" ";

}

return 0;

}

A. program outputs: 0 1 8 9

B. program outputs: 2 3 4 5 6 7

C. program outputs: 1 6 5 7

D. program outputs: 3 4 9 8 0

Buy Now
Questions 11

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<

struct Add {

int operator()(int and a, int and 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(), bind1st(Add(),1));

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

return 0;

}

Program outputs:

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

Buy Now
Questions 12

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<

struct Add : public binary_function {

int operator() (const int and a, const int and 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<

return 0;

}

Program outputs:

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

Buy Now
Questions 13

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

#include

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

set s1(t, t + 15);

pair::iterator, vector::iterator > resultSet = equal(s1.begin(), s1.end(), v1.begin());

cout<<*resultSet.first<<" "<<*resultSet.second<

}

Program outputs:

A. 2 4

B. 4 2

C. 0 5

D. compilation error

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

PDF (Q&A)

$45.99

VCE

$49.99

PDF + VCE

$59.99