BLACK FRIDAY SALE – Unlock Up to 25% OFF on All Exam Prep! - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

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

Posted by Jennie on Oct 28, 2025 at 10:19 AM

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:

Suggested Answer: C

Get Premium CPP Questions as Interactive Web-Based Practice Test or PDF Real Exam Format Detailed Explanations Instant Access Get Your Practice Test
27 comments · newest first
ED
Edda 8 months ago
I feel like it should output sorted values, but the code won't compile first.
AL
Alita 9 months ago
Definitely a compilation error. Can't sort a set directly like that.
CA
Carma 9 months ago
Agreed, the array type doesn't match the set type.
BE
Bette 9 months ago
Nah, it won't work as expected, definitely a compilation issue.
TY
Tyisha 9 months ago
The output should be sorted values, right?
LA
Laurel 10 months ago
Wait, why would it compile? I’m confused.
MI
Michel 10 months ago
Totally agree, can't sort a set directly like that!
IS
Isreal 10 months ago
Looks like a compilation error to me.
LE
Lettie 10 months ago
Hmm, I'm not sure about this one. Maybe I should have paid more attention in that data structures class.
DE
Delfina 10 months ago
I'm pretty sure I got this one right. The set container will sort the elements automatically.
KI
Kirby 11 months ago
Wow, this code looks like it's straight out of a textbook. I wonder if the professor wrote it themselves.
HU
Hui 11 months ago
Haha, I bet the person who wrote this question is a real stickler for proper formatting!
RE
Reid 11 months ago
I’m leaning towards option C, but I’m not completely sure. The way the set is initialized seems off to me.
TI
Tiffiny 11 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.
LE
Leonardo 11 months ago
I think it will give a compilation error. The set constructor needs a different type.
SA
Sabina 12 months ago
The correct answer is B. The set container will sort the elements in ascending order.
LA
Latosha 1 year 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.
EL
Elvera 1 year 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.
TE
Teri 1 year 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.
CA
Carma 1 year 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.
DE
Derick 1 year 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.
ZI
Zita 1 year 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.
RO
Roslyn 8 months ago
↳ replying to Zita
I’m leaning towards option B for the output.
BE
Belen 8 months ago
↳ replying to Zita
But the set constructor takes an array, right?
EL
Ellsworth 11 months ago
↳ replying to Zita
No way, it should output the sorted values.
TO
Tommy 12 months ago
↳ replying to Zita
Yeah, but `sort` won't work on a set.
LE
Lezlie 1 year ago
↳ replying to Zita
I think it will give a compilation error.
0/2000 characters