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< template 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
31 comments · newest first
QU
Quentin
3 days ago
I’m not sure, but I lean towards C.
RE
Reita
9 days ago
I agree with Rolande, it should be D.
RO
Rolande
14 days ago
Definitely outputs D, sorted in descending order.
HE
Herman
19 days ago
I feel like there's a compilation error.
MI
Mila
24 days ago
I think it will compile fine.
DI
Diego
29 days ago
I’m confident it will output D, everything seems in order.
LA
Lavonna
1 month ago
I’m leaning towards C, the operator overload might cause issues.
WI
Wilda
1 month ago
I agree with Berry, it looks correct to me.
BE
Berry
1 month ago
The output should be D, sorted in descending order.
AN
Annett
2 months ago
I feel like there might be a compilation error.
GI
Giuseppe
2 months ago
I think it will compile fine.
WI
Wilda
2 months ago
I believe it should output D too, looks good to me!
BO
Bok
2 months ago
I’m leaning towards C, something feels off.
EA
Earleen
2 months ago
I agree with Barrett, it looks correct.
BA
Barrett
3 months ago
The output should be D, sorted in descending order.
LU
Lucina
3 months ago
I feel like there's a compilation error.
WE
Wei
3 months ago
I think it will compile fine.
EL
Eliz
3 months ago
Is there a chance of a compilation error here?
LE
Lezlie
3 months ago
Wait, how does it sort in descending order?
RO
Roselle
3 months ago
Definitely outputs D, right?
HE
Helaine
4 months ago
I think it should compile fine.
WI
Wilda
4 months ago
The vector is initialized with 10 elements.
KA
Kallie
4 months ago
Not so sure about that, seems like a compilation error could happen.
BE
Berry
4 months ago
Totally agree with Shad, D is the right answer!
CA
Carlota
4 months ago
Wait, isn't there a mistake with the vector initialization?
SH
Shad
4 months ago
I think it will output D, sorted in descending order.
AM
Amber
5 months ago
Looks like it should compile fine.
SH
Shonda
5 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
5 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
5 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
7 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.