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 Exam CPP Topic 8 Question 101 Discussion

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

Precious
2 months ago
Wow, this guy must be a real code wizard to come up with something like this. I bet he's spending his weekends hacking the Pentagon or something.
upvoted 0 times
...
Man
2 months ago
I think there might be an issue with the way the template class is defined, causing a compilation error
upvoted 0 times
...
Dacia
2 months ago
Oh, I know this one! The program will display 'Hello world!' because the add() function for the string type concatenates the argument to the beginning of the string. Simple as that!
upvoted 0 times
Reena
1 months ago
That's correct! The string 'world!' will be added to the beginning of 'Hello'.
upvoted 0 times
...
Melissa
1 months ago
Yes, you're right! It will display 'Hello world!'
upvoted 0 times
...
...
Theola
2 months ago
But the code seems to be correct, why do you think it will result in a compilation error?
upvoted 0 times
...
Ressie
2 months ago
Bah, this is a piece of cake! The code will compile and run just fine. The output will be 'Hello world!', as expected.
upvoted 0 times
...
Man
2 months ago
I disagree, I believe the answer is B) compilation error
upvoted 0 times
...
Cathrine
2 months ago
Haha, I bet the person who wrote this code was trying to be too clever. But it looks like they got it right!
upvoted 0 times
Lyda
1 months ago
Definitely, the person who wrote this code knew what they were doing.
upvoted 0 times
...
Luis
1 months ago
I agree, the output should be Hello world!
upvoted 0 times
...
Rupert
1 months ago
Yeah, the code looks correct. It should work without any errors.
upvoted 0 times
...
Sheridan
2 months ago
I think the program will display: Hello world!
upvoted 0 times
...
...
Elina
2 months ago
The program will display 'Hello world!' because the add() function for the string type inserts the argument at the beginning of the string.
upvoted 0 times
Jaime
1 months ago
C) program will display: world!Hello
upvoted 0 times
...
Tricia
1 months ago
B) compilation error
upvoted 0 times
...
Julieta
1 months ago
A) program will display: Hello world!
upvoted 0 times
...
...
Theola
2 months ago
I think the answer is A) program will display: Hello world!
upvoted 0 times
...

Save Cancel