U.S. Independence Day Deal! Unlock 25% OFF Today – Limited-Time Offer - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CPP Exam - Topic 8 Question 58 Discussion

What will happen when you attempt to compile and run the following code?#include using namespace std;class C {public:int _c;C():_c(0){}C(int c) { _c = c;}C operator+=(C & b) {C tmp;tmp._c = _c+b._c;return tmp;}};template class A {T_v;public:A() {}A(T v): _v(v){}T getV() { return _v; }void add(T & a) { _v+=a; }};int main(){A b(2);Aa (5);Cc;a.add(c);cout
B) program will not compile
A) program will display:2
C) program will compile
D) program will cause runtime exception

C++ Institute CPP Exam - Topic 8 Question 58 Discussion

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

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

#include

using namespace std;

class C {

public:

int _c;

C():_c(0){}

C(int c) { _c = c;}

C operator+=(C & b) {

C tmp;

tmp._c = _c+b._c;

return tmp;

}

};

template

class A {

T_v;

public:

A() {}

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

T getV() { return _v; }

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

};

int main()

{

A b(2);

Aa (5);

Cc;

a.add(c);

cout << a.getV() <

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

0/2000 characters
Patria
8 months ago
Definitely a runtime exception, I can feel it!
upvoted 0 times
...
Leota
8 months ago
I thought it would run fine, what's the issue?
upvoted 0 times
...
Coleen
8 months ago
Wait, why wouldn't it compile?
upvoted 0 times
...
Wynell
8 months ago
I agree, it won't compile.
upvoted 0 times
...
Danilo
8 months ago
Looks like a compilation error to me.
upvoted 0 times
...
Cassi
9 months ago
I practiced a similar question where a class was not properly defined, and it didn't compile. I wonder if this is the same case.
upvoted 0 times
...
Ryan
9 months ago
The `add` function takes a reference, but I feel like it might be missing a proper operator for `C`. Could that be why it fails?
upvoted 0 times
...
Mireya
9 months ago
I remember something about operator overloading, but I'm not sure if it applies here since `C` is being used in a template class.
upvoted 0 times
...
Daron
9 months ago
I think the code won't compile because of the way the `add` function is trying to use the `C` class.
upvoted 0 times
...
Erinn
9 months ago
Ah, I think I've got it! The key is checking the Allow Inline Editing for Inventory Items preference. That's likely the culprit here.
upvoted 0 times
...
Elina
9 months ago
Hmm, this looks like a tricky one. I'd probably start by adding a System.debug() statement before the insert method to see what's going on.
upvoted 0 times
...

Save Cancel