Cyber Monday 2024! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute Exam CPP Topic 5 Question 95 Discussion

Actual exam question for C++ Institute's CPP exam
Question #: 95
Topic #: 5
[All CPP Questions]

What happens when you attempt to compile and run the following code?

#include

#include

#include

#include

#include

using namespace std;

templateclass B { T val;

public:

B(T v):val(v){}

T getV() const {return val;} };

templateostream & operator <<(ostream & out, const B & v) { out<

out;}

templatestruct Out {

ostream & out;

Out(ostream & o): out(o){}

void operator() (const T & val ) { out<

string tolower(const string & s) {

string tmp(s);

for(unsigned i = 0; i< tmp.size(); ++i){

tmp[i] = tolower(tmp[i]); }

return tmp; }

bool Less(const B &a, const B &b) {

return tolower(a.getV())

int main() {

string t[]={"aaa","bbb","Aaa", "Bbb","aAa","bBb","aaA","bbB"};

vector > v1; v1.assign(t, t+8);

stable_sort(v1.begin(), v1.end(), Less);

for_each(v1.begin(), v1.end(), Out >(cout));cout<

return 0;

}

Program outputs:

Show Suggested Answer Hide Answer
Suggested Answer: C

Contribute your Thoughts:

Carey
2 months ago
This code is giving me a headache. I think I'll just skip this question and move on to the next one. Who needs to know about sorting strings in C++ anyway?
upvoted 0 times
Desirae
1 months ago
I agree, let's move on to the next question.
upvoted 0 times
...
Malcom
2 months ago
It's okay, sorting strings can be tricky.
upvoted 0 times
...
...
Altha
2 months ago
Wait, is this some kind of sorcery? I'm just going to guess and say A. Hopefully, the professor won't notice if I get this wrong.
upvoted 0 times
...
Jannette
2 months ago
Hmm, this code looks like it's using some fancy template magic. I bet the output is unpredictable. D is the way to go!
upvoted 0 times
Nelida
1 months ago
Yeah, templates can be tricky. D is probably the most accurate option.
upvoted 0 times
...
Nelida
1 months ago
I agree, D seems like the safest choice here.
upvoted 0 times
...
...
Quentin
2 months ago
I'm not sure, but I think the answer might be D) the exact output is impossible to determine because the code involves case-insensitive comparison which can vary depending on the compiler.
upvoted 0 times
...
Carey
2 months ago
I think the output will be in lowercase, so C is the correct answer.
upvoted 0 times
...
Corinne
2 months ago
The output will be sorted in alphabetical order, regardless of case. So the correct answer is B.
upvoted 0 times
Mammie
1 months ago
Great, so the answer is Aaa aaa aAa aaA bbb Bbb bbB bBb.
upvoted 0 times
...
Louisa
1 months ago
That's right, it doesn't matter if the letters are uppercase or lowercase.
upvoted 0 times
...
Vince
2 months ago
Yes, the output will be sorted in alphabetical order.
upvoted 0 times
...
Cyril
2 months ago
I think the correct answer is B.
upvoted 0 times
...
...
Barbra
3 months ago
I agree with Evangelina, the code uses stable_sort which maintains the relative order of equal elements, so the output should be in that specific order.
upvoted 0 times
...
Evangelina
3 months ago
I think the answer is A) Aaa aaa aAa aaA bbb Bbb bBb bbB.
upvoted 0 times
...

Save Cancel