Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute Exam CPP Topic 2 Question 73 Discussion

Actual exam question for C++ Institute's CPP exam
Question #: 73
Topic #: 2
[All CPP Questions]

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;

}

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:

Show Suggested Answer Hide Answer
Suggested Answer: C

Contribute your Thoughts:

Nell
21 days ago
Whoa, hold on a second! If the replace function is using a predicate, then the answer should be D. The predicate function checks if the number is even, and the replace function replaces those even numbers with 10. So the output will be 10 5 2 5 2 4 4 3 3 10.
upvoted 0 times
Juan
2 days ago
I think the answer is A.
upvoted 0 times
...
...
Man
27 days ago
Hold on, I think there's a catch here. The replace function takes a predicate function as its third argument, not a value to replace with. So this code won't even compile. The answer is C, compilation error.
upvoted 0 times
...
Ozell
1 months ago
I'm pretty sure the answer is B. The replace function replaces the even numbers with 10, but it doesn't preserve the original order of the elements. So the output should be 1 10 2 5 2 4 4 3 3 10.
upvoted 0 times
...
Janna
2 months ago
I'm not sure, but I think there might be a compilation error.
upvoted 0 times
...
Meghann
2 months ago
The output should be D. The replace function replaces all even numbers with 10, so the output should be 10 5 2 5 2 4 4 3 3 10.
upvoted 0 times
Ollie
13 days ago
D) 10 5 2 5 2 4 4 3 3 10
upvoted 0 times
...
Nana
18 days ago
C) compilation error
upvoted 0 times
...
Jess
23 days ago
B) 1 10 2 5 2 4 4 3 3 10
upvoted 0 times
...
Colette
1 months ago
A) 10 5 2 5 2 4 4 3 3 1
upvoted 0 times
...
...
Gianna
2 months ago
I believe it will output D.
upvoted 0 times
...
Janna
2 months ago
I think the answer is B.
upvoted 0 times
...

Save Cancel