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

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

cout<< number<

return 0;

}

Program outputs:

A. 4

B. 3

C. 2

D. 0

E. compilation error

Browse 228 Q&As
Questions 5

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 6

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

#include

using namespace std;

int main()

{

cout<

return 0;

}

Program outputs:

A. true false

B. 1 0

C. 1 false

D. true 0

E. compilation error

Browse 228 Q&As
Questions 7

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[]={20, 30, 10, 20, 30, 10, 20, 30, 10, 20};

deque d1(t, t+10);

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

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

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

return 0;

}

Program outputs:

A. 10 10 10 20 20 20 20 30 30 30

B. 20 20 20 20

C. 10 20 20 20 20

D. 20 20 20 20 30

E. 10 20 20 20 20 30

Browse 228 Q&As
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=0):val(v){}

int getV() const {return val;}

operator int () const { return val;} };

templatestruct Out {

ostream and out;

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

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

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

Browse 228 Q&As
Questions 9

What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 1 2 3 4 quit?

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

{

list l;

while(cin.good())

{

string s;

cin>>s;

if (s == "quit") break;

l.push_back(s);

}

for_each(l.begin(), l.end(), Out(cout));

return 0;

}

Program will output:

A. 1 2 3 4

B. 1 2 3 4 quit

C. 1

D. program runs forever without output

Browse 228 Q&As
Questions 10

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 multiply (int a) {

return a*2;

}

int main() {

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

vector v1(t, t+10);

set s1(t, t+10);

transform(s1.begin(), s1.end(), v1.begin(), multiply);

transform(v1.begin(), v1.end(), s1.begin(), multiply);

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

return 0;

}

Program outputs:

A. 20 10 18 12 4 8 14 16 6 2

B. 2 4 6 8 10 12 14 16 18 20

C. 4 8 12 16 20 24 28 32 36 40

D. compilation error

Browse 228 Q&As
Questions 11

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

#include

using namespace std;

int main()

{

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

cout<<100.33<<" ";

cout.setf(ios::showbase);

cout<<100.33<<" ";

return 0;

}

Program outputs:

A. 64.21 64.21

B. 64.21 0x64.21

C. 0x64.21 0x64.21

D. 100.33 100.33

E. compilation error

Browse 228 Q&As
Questions 12

What will happen when you attempt to compile and run the following code? Choose all that apply.

#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 and b) const { return a

};

class F {

A val;

public:

F(A and v):val(v){}

bool operator() (A and v) {

if (v.getA() == val.getA()) return true;

return false;

}

};

int main() {

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

vector v1(t, t + 10);

set s1(t, t + 10);

A a(6); F f(a);

find_if(s1.begin(), s1.end(), f);

if (find_if(v1.begin(), v1.end(), f) !=v1.end()) {

cout<<"Found!\n";

} else {

cout<<"Not found!\n";

}

return 0;

}

A. it will compile successfully

B. it will display Found!

C. it will display Not found!

D. it will not compile successfully

Browse 228 Q&As
Questions 13

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

#include

using namespace std;

template

class A {

T_v;

public:

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

T getV() { return _v; }

};

int main()

{

A a(1);

cout << a.getV() <

return 0;

}

A. program will display:1

B. program will not compile

C. program will compile

D. program will cause runtime exception

Browse 228 Q&As
Questions 14

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

#include

#include

#include

#include

#include

using namespace std;

int main()

{

deque mydeck;list mylist; vector myvector;

queue first; queue second(mydeck);

queue third(second); queue > fourth(mylist);

fourth.push(10);fourth.push(11);fourth.push(12);

queue > fifth(myvector);

fifth.push(10);fifth.push(11);fifth.push(12); // Line I

while(!fifth.empty())

{

cout<

fifth.pop(); // Line III

} while (!fourth.empty()) { cout << fourth.front() << " "; fourth.pop(); // Line IV } return 0; }

A. program outputs: 10 11 12 10 11 12

B. compilation error in line I

C. compilation error in line II

D. compilation error in line III

E. compilation error in line IV

Browse 228 Q&As
Questions 15

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.insert(pair(t[i], s[i]));

}

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

m.erase(3);

}

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

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

}

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 4 4 5 5

E. program outputs: one two three four five

Browse 228 Q&As
Questions 16

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

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. 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1

Browse 228 Q&As
Questions 17

What happens when you attempt to compile and run the following code? Choose all possible answers.

#include

using namespace std;

class C {

public:

int _c;

C():_c(0){}

C(int c) { _c = c;}

C operator+=(C and b) {

C tmp; tmp._c = _c+b._c;

return tmp;

} };

ostream and operator<<(ostream and c, const C and v) {

c<

template

class A {

T_v;

public:

A() {}

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

T getV() { return _v; }

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

};

int main()

{

A b(2);

Aa (5);

a.add(C());

cout << a.getV() <

return 0;

}

A. program will display:5

B. program will not compile

C. program will compile

D. program will cause runtime exception

Browse 228 Q&As
Questions 18

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

pair::iterator,set::iterator> range;

range = s1.equal_range(6);

cout<<*range.first<<" "<<*range.second<

return 0;

}

The output will be:

A. 6 6

B. 5 7

C. 6 7

D. 1 5

E. 6 5

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

PDF

$45.99

VCE

$49.99

PDF + VCE

$59.99