New Year Sale 2026! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CPA-21-02 Exam - Topic 7 Question 44 Discussion

Actual exam question for C++ Institute's CPA-21-02 exam
Question #: 44
Topic #: 7
[All CPA-21-02 Questions]

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();

}

Show Suggested Answer Hide Answer
Suggested Answer: C

Contribute your Thoughts:

0/2000 characters
Rikki
2 months ago
Wait, calling the destructor manually? That seems risky...
upvoted 0 times
...
Bok
2 months ago
Totally agree, it should work fine!
upvoted 0 times
...
Gianna
2 months ago
I think that's a runtime error.
upvoted 0 times
...
Deja
3 months ago
Compilation error? Nah, I don't think so.
upvoted 0 times
...
Tonette
3 months ago
It prints: Text
upvoted 0 times
...
Sherly
3 months ago
I feel like it should compile fine, but I'm not completely confident about the memory management aspect.
upvoted 0 times
...
Helga
4 months ago
I practiced a similar question where the destructor was called automatically. I wonder if that changes the outcome here.
upvoted 0 times
...
Nikita
4 months ago
I think this might cause a runtime error because the destructor is called explicitly after the object is created.
upvoted 0 times
...
Shawnee
4 months 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.
upvoted 0 times
...
Ciara
4 months 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.
upvoted 0 times
...
Alona
4 months 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.
upvoted 0 times
...
Fanny
5 months 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.
upvoted 0 times
...
Dean
5 months 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.
upvoted 0 times
...
Sarah
5 months ago
B) Compilation error
upvoted 0 times
...
Estrella
5 months 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?
upvoted 0 times
Jin
2 months ago
I’m leaning towards B. There might be a compilation issue.
upvoted 0 times
...
Fletcher
2 months ago
A seems right. The object should print before deletion.
upvoted 0 times
...
Arlette
2 months ago
I think it will print "Text". So, A is my choice.
upvoted 0 times
...
Jess
3 months ago
I agree with C. The destructor is called too soon.
upvoted 0 times
...
...
Lera
6 months 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.
upvoted 0 times
Bonita
5 months ago
B) Compilation error
upvoted 0 times
...
Jutta
5 months ago
A) It prints: Text
upvoted 0 times
...
...
Charisse
6 months ago
A) It prints: Text
upvoted 0 times
...

Save Cancel