What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
class First
{
string *s;
public:
First() { s = new string("Text");}
~First() { delete s;}
void Print(){ cout<<*s;}
};
int main()
{
First FirstObject;
FirstObject.Print();
FirstObject.~First();
}
Suggested Answer: C
Get Premium CPA-21-02 Questions
as Interactive Web-Based Practice Test or PDF
Real Exam Format
Detailed Explanations
Instant Access
Get Your Practice Test
23 comments · newest first
RI
Rikki
11 months ago
Wait, calling the destructor manually? That seems risky...
BO
Bok
11 months ago
Totally agree, it should work fine!
GI
Gianna
12 months ago
I think that's a runtime error.
DE
Deja
1 year ago
Compilation error? Nah, I don't think so.
TO
Tonette
1 year ago
It prints: Text
SH
Sherly
1 year ago
I feel like it should compile fine, but I'm not completely confident about the memory management aspect.
HE
Helga
1 year ago
I practiced a similar question where the destructor was called automatically. I wonder if that changes the outcome here.
NI
Nikita
1 year ago
I think this might cause a runtime error because the destructor is called explicitly after the object is created.
SH
Shawnee
1 year ago
I remember something about destructors and how they work, but I'm not sure if calling it manually like that is a good idea.
CI
Ciara
1 year ago
I'm feeling pretty confident about this one. The key is to understand the lifecycle of the `First` object and how the constructor and destructor are interacting with the dynamically allocated memory. I think I can work through this step-by-step and arrive at the correct answer.
AL
Alona
1 year ago
Okay, I've got a strategy for this. I need to focus on the memory management aspect - the object is dynamically allocating memory for the `string` pointer, and then the destructor is deleting that memory. I'll need to think about when that memory is being accessed and whether that could cause any issues.
FA
Fanny
1 year ago
Hmm, I'm a bit confused here. The code is creating an object and then immediately destroying it, but it's also trying to print something. I'll need to think through the order of operations and how that might affect the output.
DE
Dean
1 year ago
I think this is a tricky one. The code is creating a `First` object and then immediately calling its destructor, which could lead to a runtime error. I'll need to carefully consider the implications of that.
SA
Sarah
1 year ago
B) Compilation error
ES
Estrella
1 year ago
Hmm, I'm not sure about this one. The code seems to be creating and then immediately destroying the object, so I'm guessing it might be a runtime error. Maybe C is the correct answer?
JI
Jin
11 months ago
I’m leaning towards B. There might be a compilation issue.
FL
Fletcher
11 months ago
A seems right. The object should print before deletion.
AR
Arlette
11 months ago
I think it will print "Text". So, A is my choice.
JE
Jess
12 months ago
I agree with C. The destructor is called too soon.
LE
Lera
1 year ago
I think the answer is A. It should print 'Text' since we're creating an object of the First class and calling the Print() function.
BO
Bonita
1 year ago
B) Compilation error
JU
Jutta
1 year ago
A) It prints: Text
CH
Charisse
1 year ago
A) It prints: Text