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 CPP Exam - Topic 3 Question 123 Discussion

Posted by Dorethea on May 12, 2026 at 10:21 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};

vector v1(t, t+10);

sort(v1.begin(), v1.end(), greater());

for_each(v1.begin(), v1.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
22 comments · newest first
AN
Annett 5 days ago
I feel like there might be a compilation error.
GI
Giuseppe 10 days ago
I think it will compile fine.
WI
Wilda 15 days ago
I believe it should output D too, looks good to me!
BO
Bok 20 days ago
I’m leaning towards C, something feels off.
EA
Earleen 25 days ago
I agree with Barrett, it looks correct.
BA
Barrett 1 month ago
The output should be D, sorted in descending order.
LU
Lucina 1 month ago
I feel like there's a compilation error.
WE
Wei 1 month ago
I think it will compile fine.
EL
Eliz 2 months ago
Is there a chance of a compilation error here?
LE
Lezlie 2 months ago
Wait, how does it sort in descending order?
RO
Roselle 2 months ago
Definitely outputs D, right?
HE
Helaine 2 months ago
I think it should compile fine.
WI
Wilda 2 months ago
The vector is initialized with 10 elements.
KA
Kallie 2 months ago
Not so sure about that, seems like a compilation error could happen.
BE
Berry 3 months ago
Totally agree with Shad, D is the right answer!
CA
Carlota 3 months ago
Wait, isn't there a mistake with the vector initialization?
SH
Shad 3 months ago
I think it will output D, sorted in descending order.
AM
Amber 3 months ago
Looks like it should compile fine.
SH
Shonda 3 months ago
I’m confused about the vector initialization. If it’s not compiling, then option C could be the right answer, but I hope it runs correctly and gives us the sorted output.
LE
Leonor 3 months ago
I'm not entirely sure, but I feel like the output could be option D since we're sorting the objects of class B, and they should print in descending order.
SH
Shaunna 4 months ago
I remember practicing a similar question where we had to sort a custom class. I think the output should be in descending order because of the `greater()` comparator.
VI
Vivienne 5 months ago
I think there might be a compilation error because the vector is initialized with an array of integers, but the class B expects an integer in its constructor.
0/2000 characters