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;

void myfunction(int i) {

cout << " " << i;

}

void multiply (int a) {

a*2;

}

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

iter_swap(v1.begin(),t+9);

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

return 0;

}

Program outputs:

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

B. compilation error

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

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

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

Buy Now
Questions 5

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

#include

#include

#include

using namespace std;

int main() {

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

map m;

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

m[i]=t[i];

}

pair p(5,5);

map::iterator it = find(m.begin(), m.end(), p);

if (it != m.end())

{

cout<first<

}

else

{

cout<<"Not found!\n";

}

return 0;

}

Program outputs:

A. 5

B. Not found!

C. 10

D. compilation error

Buy Now
Questions 6

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

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 7

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

#include

#include

#include

#include

using namespace std;

bool identical(int a, int b) {

return b == 2*a?true:false;

}

int main() {

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

int u[] = {2,4,6,4,6,10,2,4,14,6,4,2,20,8,8,5};

vector v1(t, t + 15);

deque d1(u, u + 15);

pair::iterator, vector::iterator > result;

result = mismatch(d1.begin(), d1.end(), v1.begin(), identical); //Line I

if (result.first == d1.end() andand result.second == v1.end()) {//Line II

cout<<"Identical\n";

} else {

cout<<"Not identical\n";

}

return 0;

}

Program outputs:

A. Identical

B. Not identical

C. compilation error at line marked I

D. compilation error at line marked II

Buy Now
Questions 8

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

#include

#include

#include

using namespace std;

int main(){

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

listv(t, t+10);

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

if (s1.count(3) == 2) {

s1.erase(3);

}

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

cout<<*i<<" ";

}

return 0;

}

A. program outputs: 1 2 3 4 5

B. program outputs: 1 2 4 5

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

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

E. compilation error

Buy Now
Questions 9

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,3,5,1,2,4,4,5};

vector v (t,t+10);

vector::iterator it = v.begin();

while ( (it = adjacent_find (it, v.end())) != v.end()) {

cout<

}

cout<< endl;

return 0;

}

A. program outputs: 2 3

B. program outputs: 2 7

C. program outputs: 3 8

D. compilation error

E. program will run forever

Buy Now
Questions 10

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

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

vector v1(t, t + 10);

copy(t, t+10, v1.end());

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

return 0;

}

Program outputs:

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

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

C. compilation error

D. runtime exception/segmentation fault

Buy Now
Questions 11

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

multimap m;

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

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

}

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

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

}

return 0; }

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 12

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;

}

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 13

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
Exam Code: CPP
Exam Name: C++ Certified Professional Programmer
Last Update: Feb 15, 2025
Questions: 228
10%OFF Coupon Code: SAVE10

PDF (Q&A)

$49.99

VCE

$55.99

PDF + VCE

$65.99