New Year Sale ! 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
1 months 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
1 months 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
1 months 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
1 months 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
Oliva
8 days ago
I agree with you, I also think it's A. The for_each loop is printing the elements in reverse order.
upvoted 0 times
...
Oliva
13 days ago
I'm leaning towards A. The initial elements in the vector are being printed in reverse order.
upvoted 0 times
...
Oliva
14 days ago
I disagree, I believe it's D. The elements are being incremented by 1 in the transform function.
upvoted 0 times
...
Oliva
16 days ago
I think it's B. The transform function is adding 1 to each element in the vector.
upvoted 0 times
...
...
Daniel
2 months 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
2 months 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
2 months 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
Lai
11 days ago
I agree with you, the answer is D. The values are shifted up by 1 and printed in reverse order.
upvoted 0 times
...
Felicia
21 days ago
I'm leaning towards C. The values are being shifted up by 1 and printed in reverse order.
upvoted 0 times
...
Marg
1 months 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
1 months ago
I think the answer is B. The values are being shifted up by 1 and printed in reverse order.
upvoted 0 times
...
...
Truman
2 months 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
Reta
26 days ago
I think it's D. The elements might be shifted differently than we expect.
upvoted 0 times
...
Shawn
1 months ago
I agree with you, it should be B. The plus function adds 1 to each element in the vector.
upvoted 0 times
...
Lezlie
1 months 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
2 months ago
I think the answer is B too. The transform function adds 1 to each element.
upvoted 0 times
...
...