New Year Sale 2026! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CPP Exam - Topic 7 Question 115 Discussion

Actual exam question for C++ Institute's CPP exam
Question #: 115
Topic #: 7
[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() {

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

set s1(t, t+10);

sort(s1.begin(), s1.end());

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

return 0;

}

Program outputs:

Show Suggested Answer Hide Answer
Suggested Answer: C

Contribute your Thoughts:

0/2000 characters
Carma
1 day ago
Agreed, the array type doesn't match the set type.
upvoted 0 times
...
Bette
7 days ago
Nah, it won't work as expected, definitely a compilation issue.
upvoted 0 times
...
Tyisha
12 days ago
The output should be sorted values, right?
upvoted 0 times
...
Laurel
17 days ago
Wait, why would it compile? I’m confused.
upvoted 0 times
...
Michel
22 days ago
Totally agree, can't sort a set directly like that!
upvoted 0 times
...
Isreal
27 days ago
Looks like a compilation error to me.
upvoted 0 times
...
Lettie
1 month ago
Hmm, I'm not sure about this one. Maybe I should have paid more attention in that data structures class.
upvoted 0 times
...
Delfina
1 month ago
I'm pretty sure I got this one right. The set container will sort the elements automatically.
upvoted 0 times
...
Kirby
1 month ago
Wow, this code looks like it's straight out of a textbook. I wonder if the professor wrote it themselves.
upvoted 0 times
...
Hui
2 months ago
Haha, I bet the person who wrote this question is a real stickler for proper formatting!
upvoted 0 times
...
Reid
2 months ago
I’m leaning towards option C, but I’m not completely sure. The way the set is initialized seems off to me.
upvoted 0 times
...
Tiffiny
2 months ago
I feel like the output should be sorted, but I can't recall if the set itself maintains order or if we need to sort it after.
upvoted 0 times
...
Leonardo
2 months ago
I think it will give a compilation error. The set constructor needs a different type.
upvoted 0 times
...
Sabina
2 months ago
The correct answer is B. The set container will sort the elements in ascending order.
upvoted 0 times
...
Latosha
3 months ago
I remember something similar where we had to sort a set, but I’m not sure if you can sort a set directly like that.
upvoted 0 times
...
Elvera
3 months ago
I think there might be a compilation error because the set constructor takes a range, but the array isn't a compatible type for that.
upvoted 0 times
...
Teri
4 months ago
This looks like a tricky one. All that custom class and template stuff is throwing me off a bit. I'll need to really focus and make sure I understand how the set and sorting are working here. Might be worth a closer look at the operator overloading too.
upvoted 0 times
...
Carma
4 months ago
Okay, let me think this through. The code is creating a set of B objects, sorting them, and then printing them out. I'm guessing the output will be the sorted values of the B objects. I'll need to double-check the comparison operator, but I think I can figure this out.
upvoted 0 times
...
Derick
4 months ago
Hmm, I'm a bit confused by all the custom class and template stuff. I'll need to carefully read through the code and make sure I understand how the set and for_each are being used. Might be a tricky one.
upvoted 0 times
...
Zita
4 months ago
This looks like a pretty straightforward C++ question. I think I can handle it - the code seems to be working with a set of custom objects, so I'll need to pay attention to the comparison operator and output operator.
upvoted 0 times
Ellsworth
2 months ago
No way, it should output the sorted values.
upvoted 0 times
...
Tommy
3 months ago
Yeah, but `sort` won't work on a set.
upvoted 0 times
...
Lezlie
3 months ago
I think it will give a compilation error.
upvoted 0 times
...
...