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

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
E) compilation error
A) 3
B) 1
C) 6
D) 10

C++ Institute CPP Exam - Topic 6 Question 127 Discussion

Actual exam question for C++ Institute's CPP exam
Question #: 127
Topic #: 6
[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>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:

Show Suggested Answer Hide Answer
Suggested Answer: E

Contribute your Thoughts:

0/2000 characters
Marg
3 days ago
I think it will compile fine.
upvoted 0 times
...
Yolando
8 days ago
I disagree, it should output 3 based on the sorting logic.
upvoted 0 times
...
Rosio
13 days ago
Definitely not a compilation error, that's for sure!
upvoted 0 times
...
Shenika
19 days ago
Wait, does it really sort in descending order?
upvoted 0 times
...
Alfreda
24 days ago
I think it outputs 1, since that's the smallest value.
upvoted 0 times
...
Felix
29 days ago
The code compiles fine, no errors.
upvoted 0 times
...
Martina
1 month ago
I have a feeling there might be a compilation error because of how the array of `B` objects is initialized.
upvoted 0 times
...
Lino
1 month 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.
upvoted 0 times
...
Alise
1 month 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.
upvoted 0 times
...
Vicki
2 months ago
I think the code should compile fine since all the necessary operators and constructors are defined.
upvoted 0 times
...