Independence Day Deal! Unlock 25% OFF Today – Limited-Time Offer - 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
1 months 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
Delmy
6 days ago
You're right, the output will be 10 5 2 5 2 4 4 3 3 10.
upvoted 0 times
...
Emile
7 days ago
Actually, I believe the correct answer is D.
upvoted 0 times
...
Juan
19 days ago
I think the answer is A.
upvoted 0 times
...
...
Man
1 months 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
Janet
13 days ago
Yes, that's correct. The code won't compile because of the incorrect argument in the replace function.
upvoted 0 times
...
Muriel
16 days ago
Oh, so the correct answer is C) compilation error then.
upvoted 0 times
...
Asuncion
17 days ago
I think you're right, the code won't compile because the replace function is expecting a predicate function.
upvoted 0 times
...
...
Ozell
2 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
30 days ago
D) 10 5 2 5 2 4 4 3 3 10
upvoted 0 times
...
Nana
1 months ago
C) compilation error
upvoted 0 times
...
Jess
1 months ago
B) 1 10 2 5 2 4 4 3 3 10
upvoted 0 times
...
Colette
2 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
3 months ago
I think the answer is B.
upvoted 0 times
...

Save Cancel