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 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
1 months 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
1 months ago
This code is like a game of 'Guess the Output' - you never know what you're going to get!
upvoted 0 times
Daren
5 days ago
C) 1 0
upvoted 0 times
...
Larae
13 days ago
B) 0 1
upvoted 0 times
...
Tamekia
14 days ago
A) 0 0
upvoted 0 times
...
...
Mozell
2 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
9 days ago
I agree, the code adds 1 to each element and then prints 1 for each element in reverse.
upvoted 0 times
...
Ocie
14 days ago
I think it will output 1 1 as well.
upvoted 0 times
...
...
Rebecka
2 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
Trina
4 days ago
B) 0 1
upvoted 0 times
...
Jose
1 months 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
1 months ago
User2
upvoted 0 times
...
Lura
1 months 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
3 months ago
I think the program will output 0 0 because the bind1st function is not correctly used.
upvoted 0 times
...

Save Cancel
az-700  pass4success  az-104  200-301  200-201  cissp  350-401  350-201  350-501  350-601  350-801  350-901  az-720  az-305  pl-300  

Warning: Cannot modify header information - headers already sent by (output started at /pass.php:70) in /pass.php on line 77