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 5 Question 95 Discussion

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

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

#include

#include

#include

using namespace std;

class B { int val;

public:

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

int getV() const {return val;} bool operator < (const B & v) 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 t1[]={3,2,4,1,5};

B t2[]={6,10,8,7,9};

vector v1(10);

sort(t1, t1+5);

sort(t2, t2+5);

merge(t1,t1+5,t2,t2+5,v1.begin());

for_each(v1.begin(), v1.end(), Out(cout));cout<

return 0;

}

Program outputs:

Show Suggested Answer Hide Answer
Suggested Answer: C

Contribute your Thoughts:

Catarina
12 days ago
Wait, did they forget to include the 'greater' comparison function for the B class? This is going to be a tough one! *grabs popcorn*
upvoted 0 times
...
Kendra
16 days ago
This is easy! The answer is A) 8 10 5 1 4 6 2 7 9 3. The merge operation interleaves the elements of the two arrays, which is exactly what the output shows.
upvoted 0 times
...
Stephaine
1 months ago
I'm going with C) compilation error. The code has a missing header file for the vector class, so it won't compile.
upvoted 0 times
Elly
16 days ago
I'm going with C) compilation error. The code has a missing header file for the vector class, so it won't compile.
upvoted 0 times
...
Samuel
27 days ago
No, I believe it's B) 1 2 3 4 5 6 7 8 9 10
upvoted 0 times
...
Lili
29 days ago
I think it's A) 8 10 5 1 4 6 2 7 9 3
upvoted 0 times
...
...
Nada
2 months ago
I'm not sure, but I think the correct answer is B) 1 2 3 4 5 6 7 8 9 10 because of the sorting and merging operations in the code.
upvoted 0 times
...
Launa
2 months ago
Hmm, the code seems to be merging two sorted arrays into a vector. I think the correct answer is B) 1 2 3 4 5 6 7 8 9 10, as the merge operation should preserve the sorted order of the input arrays.
upvoted 0 times
Carma
27 days ago
Yes, the merge operation should maintain the sorted order, so the output should be B) 1 2 3 4 5 6 7 8 9 10.
upvoted 0 times
...
Carma
30 days ago
I agree, the correct answer is B) 1 2 3 4 5 6 7 8 9 10.
upvoted 0 times
...
...
Yolando
2 months ago
I agree with Milly, because the code sorts the arrays t1 and t2 and then merges them into vector v1 in ascending order.
upvoted 0 times
...
Milly
2 months ago
I think the answer is B) 1 2 3 4 5 6 7 8 9 10.
upvoted 0 times
...