Cyber Monday 2024! 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 8 Question 100 Discussion

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

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

#include

#include

#include

#include

using namespace std;

class B { int val;

public:

B(int v=0):val(v){}

int getV() const {return val;}

operator int () const { return val;} };

ostream & operator <<(ostream & out, const B & v) { out<

templatestruct Out {

ostream & out;

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

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

int main() {

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

vector v1(t, t+10);

transform(v1.begin(), v1.end(), v1.begin(), bind2nd(plus(), 1));

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

return 0;

}

Program outputs:

Show Suggested Answer Hide Answer
Suggested Answer: D

Contribute your Thoughts:

Arleen
5 days ago
This code looks like it was written by a programmer who's been drinking way too much coffee. I'm just hoping the output doesn't crash my computer!
upvoted 0 times
...
Katlyn
6 days ago
Hold on, I think there's a syntax error in the code. I'm gonna go with E, there's gotta be a compilation error somewhere.
upvoted 0 times
...
Nydia
8 days ago
That's a good point, Daniel. I see where you're coming from now. Let's see what others think about this question.
upvoted 0 times
...
Emile
8 days ago
Hmm, this is a tricky one. I'm going to go with C. The for_each loop is printing the elements in reverse order, so that's gotta be the right answer.
upvoted 0 times
...
Daniel
14 days ago
I disagree, I believe the correct answer is A) 3 2 4 1 5 6 10 8 7 9 because the code is using the rbegin() and rend() functions to print the vector in reverse order.
upvoted 0 times
...
Nydia
18 days ago
I think the answer is B) 4 3 5 2 6 7 11 9 8 10 because the code is using transform function with bind2nd(plus(), 1) which adds 1 to each element in the vector.
upvoted 0 times
...
Coral
21 days ago
I'm pretty sure the answer is D. The transform function is shifting the values up by 1, and the for_each loop is printing them in reverse order.
upvoted 0 times
Marg
8 days ago
I disagree, I believe the correct answer is A. The values are shifted up by 1 and printed in the original order.
upvoted 0 times
...
Wei
10 days ago
I think the answer is B. The values are being shifted up by 1 and printed in reverse order.
upvoted 0 times
...
...
Truman
26 days ago
This code looks pretty complex, but I think the answer is B. The transform function is adding 1 to each element in the vector, so the output should be shifted by 1.
upvoted 0 times
Shawn
3 days ago
I agree with you, it should be B. The plus function adds 1 to each element in the vector.
upvoted 0 times
...
Lezlie
10 days ago
I'm not sure, but I think it might be C. The elements might be rearranged in a different way.
upvoted 0 times
...
Carolynn
14 days ago
I think the answer is B too. The transform function adds 1 to each element.
upvoted 0 times
...
...