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 9 Question 91 Discussion

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

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

#include

#include

using namespace std;

template

class A {

T_v;

public:

A() {}

A(T v): _v(v){}

T getV() { return _v; }

void add(T & a);

void add(string & a);

};

template

void A::add(T & a) { _v+=a; }

void A::add(string & a) {

_v.insert(0, a);

}

int main()

{

Aa("Hello");

string s(" world!");

a.add(s);

cout << a.getV() <

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

Geraldo
3 months ago
Wait, is this code even legal? I'm just going to guess option B and hope the compiler catches this mess. If not, I'll be the one in a world of 'Hello' and 'world!'
upvoted 0 times
...
Hubert
3 months ago
This is a bit of a brain-teaser, but I'm feeling confident. I'm going to have to go with option A - 'Hello world!'. That's my final answer, and no, I don't need to phone a friend.
upvoted 0 times
...
Norah
3 months ago
Ah, I see what's going on here. The string addition is using insert(0, a), so it's going to prepend the string. I'll go with option C - 'world!Hello'.
upvoted 0 times
Tony
2 months ago
Yes, that's correct. The string will be prepended, so it will show: world!Hello.
upvoted 0 times
...
Tony
3 months ago
I think you're right, it will display: world!Hello.
upvoted 0 times
...
Britt
3 months ago
Yes, the insert(0, a) will add 'world!' before 'Hello'. Option C is the answer.
upvoted 0 times
...
Shay
3 months ago
I think you're right, it will prepend the string. Option C sounds correct.
upvoted 0 times
...
...
Lucia
3 months ago
Hmm, let me think this through. The code is using templates, and it looks like there's a specialization for the string type. I'm going to guess option A - the program will display 'Hello world!'
upvoted 0 times
Erinn
2 months ago
Great job! Option A is the correct answer.
upvoted 0 times
...
Stephanie
3 months ago
Yes, that's correct. The code is using templates and there's a specialization for the string type.
upvoted 0 times
...
Willodean
3 months ago
I think you're right, the program will display 'Hello world!'
upvoted 0 times
...
...
Alaine
4 months ago
I think the correct answer is C) program will display: world!Hello because of the insert function in the add method
upvoted 0 times
...
Robt
4 months ago
I agree with Aja, A) makes sense because the code should concatenate 'Hello' and 'world!'
upvoted 0 times
...
Lajuana
4 months ago
This looks like a tricky one. I'm going to go with option B - it's gotta be a compilation error, right?
upvoted 0 times
Lashandra
3 months ago
I'm pretty sure it's option A: program will display: Hello world!
upvoted 0 times
...
Art
3 months ago
I guess I was wrong, thanks for clarifying!
upvoted 0 times
...
Giuseppe
3 months ago
You're right, it will display: Hello world!
upvoted 0 times
...
Frank
3 months ago
No, I believe it will give a compilation error.
upvoted 0 times
...
Linette
3 months ago
I believe it will show: Hello world!
upvoted 0 times
...
Noble
4 months ago
I think it will display: Hello world!
upvoted 0 times
...
Roxanne
4 months ago
I think it will display: Hello world!
upvoted 0 times
...
...
Jennifer
4 months ago
I disagree, I believe the answer is B) compilation error
upvoted 0 times
...
Aja
4 months ago
I think the answer is A) program will display: Hello world!
upvoted 0 times
...

Save Cancel