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 6 Question 127 Discussion

Posted by Malika on Jul 17, 2026 at 08:52 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>v.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,10,9,7,8,6};

vector v1(t,t+10);

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

cout<<*min_element(v1.begin(), v1.end());

return 0;

}

Program outputs:

Suggested Answer: E

Get Premium CPP Questions as Interactive Web-Based Practice Test or PDF Real Exam Format Detailed Explanations Instant Access Get Your Practice Test
15 comments · newest first
BR
Brandon 18 days ago
Right, so B) 1 makes sense.
DI
Dierdre 23 days ago
Exactly, so the min_element will still find the smallest.
MA
Marg 28 days ago
But it sorts in descending order first.
BR
Brandon 1 month ago
The output should be 1, since it's the smallest after sorting.
DI
Dierdre 1 month ago
I feel like there's a compilation error.
MA
Marg 1 month ago
I think it will compile fine.
YO
Yolando 2 months ago
I disagree, it should output 3 based on the sorting logic.
RO
Rosio 2 months ago
Definitely not a compilation error, that's for sure!
SH
Shenika 2 months ago
Wait, does it really sort in descending order?
AL
Alfreda 2 months ago
I think it outputs 1, since that's the smallest value.
FE
Felix 2 months ago
The code compiles fine, no errors.
MA
Martina 2 months ago
I have a feeling there might be a compilation error because of how the array of `B` objects is initialized.
LI
Lino 3 months ago
This reminds me of a practice question where we had to sort a vector of objects. I feel like the output should be the smallest value after sorting.
AL
Alise 3 months ago
I'm not entirely sure, but I remember something about how `min_element` works with custom types. It might not return what we expect.
VI
Vicki 3 months ago
I think the code should compile fine since all the necessary operators and constructors are defined.
0/2000 characters