New Year Special 60% Discount Offer - Ends in 0d 00h 00m 00s - Coupon code: bestdeal

Free C++ Institute CPP Practice Exam with Questions & Answers | Set: 4

Questions 31

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

#include

#include <iostream>

#include <algorithm>

#include <functional>

using namespace std;

templatestruct Out {

ostream & out;

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

void operator() (const T & val ) { out<<val<<" "; } };

int Add(int a, int 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(), bind2nd(ptr_fun (Add),1));

vector::iterator it = find_if(v2.begin(), v2.end(),bind2nd(equal_to(),10));

cout<<*it<<endl;

return 0;

}

Program outputs:

Options:
A.

false

B.

true

C.

10

D.

0

E.

compilation error

C++ Institute CPP Premium Access
Questions 32

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

#include <iostream>

#include <algorithm>

#include

using namespace std;

struct Compare {

bool operator ()(int a) {

if (a >5) return true;

return false;

}

};

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

cout<< number<<endl;

return 0;

}

Program outputs:

Options:
A.

4

B.

3

C.

2

D.

0

E.

compilation error

Questions 33

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

#include <iostream>

#include <algorithm>

#include

#include

using namespace std;

void myfunction(int i) {

cout << " " << i;

}

bool classifier(int v) {

return v%2==0;

}

int main() {

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

vector v1(t, t+10);

set s1(t, t+10);

replace(v1.begin(), v1.end(),classifier, 10);

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

return 0;

}

Program outputs:

Options:
A.

1 5 10 5 10 10 10 3 3 1

B.

1 5 2 5 2 4 4 3 3 1

C.

compilation error

D.

10 10 2 10 2 4 4 10 10 10

Questions 34

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

#include <iostream>

#include

#include

using namespace std;

template void print(T start, T end) {

while (start != end) {

std::cout << *start << " "; start++;

}

}

int main(){

vectorv;

multiset s;

for(int i=10; i>0; i??) {

v.push_back(i); s.push_back(i);

}

print(v.begin(), v.end()); print(s.begin(), s.end());cout<<endl;

return 0;

}

Options:
A.

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

B.

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

C.

program outputs: 10 9 8 7 6 5 4 3 2 1 and unpredictable sequence of numbers range 1 to 10

D.

compilation error

Questions 35

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

#include

#include <iostream>

int main ()

{

std::vectorv1;

for(int i = 10; i>0; i??)

{

v1.push_back(i);

}

std::vector::iterator it = v1.begin();

int sum = 0;

while(it != v1.end())

{

sum+=it++;

}

std::cout<<*v1.erase(v1.begin(),v1.end()?3)<<" "<

return 0;

}

Options:
A.

program outputs 3 55

B.

compilation error

C.

program outputs 3 45

D.

program outputs 7 55

Questions 36

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

#include

#include

#include <iostream>

#include <string>

using namespace std;

template<typename T>

void print(T start, T end)

{

while (start != end)

cout<<*start++;

}

int main ()

{

string t[] = {"one", "two" ,"three" ,"four", "five"};

vector<string>v1(t, t+5);

deque<string>d1(v1.rbegin(), v1.rend());

d1.push_back("zero");

print(d1[0].rbegin(),d1[0].rend());

return 0;

}

Options:
A.

program outputs: orez

B.

program outputs: evif

C.

compilation error

D.

program outputs: five

Questions 37

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

#include <iostream>

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

return 0;

}

The output will be:

Options:
A.

6 6

B.

5 7

C.

6 7

D.

1 5

E.

6 5

Questions 38

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

#include <iostream>

#include <algorithm>

#include

using namespace std;

struct Even {

bool operator ()(int a) {

return (a % 2)==0?true:false;

}

};

int main () {

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

set s(t,t+15);

int number = count_if(s.begin(), s.end(), Even());

cout<< number<<endl;

return 0;

}

Program outputs:

Options:
A.

4

B.

3

C.

7

D.

8

E.

compilation error

Questions 39

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

#include

#include

#include <iostream>

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<<v1.size()<<" "<

return 0;

}

Options:
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

Questions 40

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

#include <iostream>

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

}

Options:
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