Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 0d 16h 17m 53s Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute Exam CPP Topic 9 Question 91 Discussion

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

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

#include

#include

#include

#include

using namespace std;

templatestruct Out {

ostream & out;

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

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

int main() {

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

vector v1(t, t+10);

for_each(v1.begin(), v1.end(), bind1st(plus(), 1));

for_each(v1.rbegin(), v1.rend(), Out(cout));cout<

return 0;

}

Program outputs:

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

Avery
27 days ago
The correct answer is B) 0 1. The for_each loop adds 1 to each element, so the vector becomes 4 3 5 2 6 1 11 9 8 10. Then the reversed for_each loop prints these elements, but the last element is printed as 0 because it's out of range.
upvoted 0 times
...
Rikki
30 days ago
This code is like a game of 'Guess the Output' - you never know what you're going to get!
upvoted 0 times
Tamekia
20 hours ago
A) 0 0
upvoted 0 times
...
...
Mozell
1 months ago
Hmm, I'm not sure. The code looks a bit complex, but I'd guess the output is 1 1, since the for_each loop is adding 1 to each element and then the reversed for_each is printing 1 for each element.
upvoted 0 times
Ocie
19 hours ago
I think it will output 1 1 as well.
upvoted 0 times
...
...
Rebecka
1 months ago
I think the output will be 4 3 5 2 6 1 10 8 7 9. The for_each function is adding 1 to each element, and then the reversed for_each is printing the elements.
upvoted 0 times
Jose
28 days ago
A) 0 0
upvoted 0 times
...
...
Dulce
2 months ago
The code seems to be adding 1 to each element of the vector and then printing them in reverse order. So the output should be 10 9 8 7 6 5 4 3 2 1.
upvoted 0 times
Annmarie
22 days ago
User2
upvoted 0 times
...
Lura
27 days ago
User1
upvoted 0 times
...
...
Marti
2 months ago
I'm not sure about the output, but I think it might be 0 1 because of the way the bind1st function is being called.
upvoted 0 times
...
Ashlyn
2 months ago
I agree with Franklyn, the bind1st function is not correctly used, so the output should be 0 0.
upvoted 0 times
...
Franklyn
2 months ago
I think the program will output 0 0 because the bind1st function is not correctly used.
upvoted 0 times
...

Save Cancel
a