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 1 Question 12 Discussion

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

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

#include

using namespace std;

class BaseC

{

int *ptr;

public:

BaseC() { ptr = new int(10);}

BaseC(int i) { ptr = new int(i); }

~BaseC() { delete ptr; }

void Print() { cout << *ptr; }

};

int main()

{

BaseC *o = new BaseC(5);

o?>Print();

}

Show Suggested Answer Hide Answer
Suggested Answer: D

Contribute your Thoughts:

0/2000 characters
Darrin
3 months ago
Not sure about that, seems off to me.
upvoted 0 times
...
Arlette
4 months ago
I thought it would print 10, not 5.
upvoted 0 times
...
Kimbery
4 months ago
Wait, how does it even compile with that `?>`?
upvoted 0 times
...
Svetlana
4 months ago
Totally agree, it's definitely 5!
upvoted 0 times
...
Clarence
4 months ago
It prints: 5
upvoted 0 times
...
Veronica
5 months ago
Wait, did I miss something? I thought the default constructor would set it to 10, but this one uses the parameter. So, it should be 5, right?
upvoted 0 times
...
Albert
5 months ago
I practiced a similar question, and I feel like it should print 5 since that's the value passed to the constructor.
upvoted 0 times
...
Sarah
5 months ago
I'm not entirely sure, but I remember something about how pointers work in constructors. Could it be 10 instead?
upvoted 0 times
...
Merilyn
5 months ago
I think the code should print 5 because the constructor with the parameter is called.
upvoted 0 times
...
Desmond
5 months ago
This is a good test of understanding pointers and object lifetime. I'll carefully walk through the code and think about when the objects are created and destroyed to determine the output. I'm confident I can get the right answer.
upvoted 0 times
...
Francine
5 months ago
Ah, I see what's going on now. The line "o->Print();" is using the pointer directly, so it will print the value that the pointer is pointing to, which is 5. The answer is A.
upvoted 0 times
...
Talia
5 months ago
I'm a bit confused here. The constructor with the int argument creates a new int and assigns the pointer to it, but the default constructor creates a pointer to 10. I'm not sure which one will be used in the end. I'll have to guess on this one.
upvoted 0 times
...
Tennie
5 months ago
Okay, let me think this through step-by-step. We're creating a BaseC object with the argument 5, so the int pointer should be pointing to 5. Then we're calling the Print() method, which should print the value of the pointer. I'll go with option A.
upvoted 0 times
...
Malcolm
5 months ago
Hmm, this looks like a tricky one. I'll need to carefully trace the code execution to figure out what's happening with the pointers and object creation.
upvoted 0 times
...
Theron
5 months ago
I'm a bit confused by this one. The question doesn't give a lot of context, so it's hard for me to determine the right answer. I'll have to make an educated guess.
upvoted 0 times
...
Gene
5 months ago
Definitely DHCP. I remember learning about that in class, it's the protocol that dynamically assigns IP addresses to devices on a network.
upvoted 0 times
...
Lynna
5 months ago
Okay, I've got this. The key is to identify which option covers a global-level configuration for time tracking, rather than a project-specific or user-specific setting.
upvoted 0 times
...
Phil
5 months ago
Okay, I think I've got this. I'll select the paragraphs, then use the Bullets option to format them properly.
upvoted 0 times
...
Lanie
10 months ago
I'm going to go with B. It's the most likely option, and I'm not going to overthink this. Time to move on to the next question!
upvoted 0 times
Rodrigo
9 months ago
Yep, the correct answer is: 5
upvoted 0 times
...
Leah
9 months ago
I agree, it should print: 5
upvoted 0 times
...
Pamella
9 months ago
I think it prints: 5
upvoted 0 times
...
...
Donte
10 months ago
Haha, this is a classic pointer-related question. I bet the answer is B, just to mess with us all!
upvoted 0 times
Valentin
9 months ago
User 3: I'm going with C on this one.
upvoted 0 times
...
Weldon
9 months ago
User 2: No way, it's definitely B.
upvoted 0 times
...
Kayleigh
10 months ago
User 1: I think the answer is A.
upvoted 0 times
...
...
Melinda
11 months ago
Wait, are we sure the object is being deleted properly? If there's a memory leak, it might print some random value like 1 or 0.
upvoted 0 times
Chantell
9 months ago
User3: Wait, but what if there's a memory leak? It could print a random value.
upvoted 0 times
...
Marguerita
9 months ago
User2: Yeah, it should print 5 since we're passing 5 to the constructor.
upvoted 0 times
...
Dorsey
9 months ago
Always remember to properly manage memory in your code.
upvoted 0 times
...
Yuette
9 months ago
Memory leaks can cause unexpected values to be printed.
upvoted 0 times
...
Jillian
10 months ago
User1: It prints: 5
upvoted 0 times
...
Micaela
10 months ago
It prints: 5
upvoted 0 times
...
Lawrence
10 months ago
Make sure to check for memory leaks in the code.
upvoted 0 times
...
...
Lai
11 months ago
That makes sense, but I'm not sure. Let's see what others think.
upvoted 0 times
...
Nguyet
11 months ago
I believe the answer is A) It prints: 5 because the object is initialized with 5.
upvoted 0 times
...
Sharen
11 months ago
Hmm, I'm not so sure. What if the compiler optimizes the code and the object ends up being created with the default constructor instead of the one that takes an argument? Then it would print 10.
upvoted 0 times
Candra
10 months ago
User 2: I think it prints: 10
upvoted 0 times
...
Mary
10 months ago
User 1: It prints: 5
upvoted 0 times
...
...
Lai
11 months ago
I think the answer is B) It prints: 10.
upvoted 0 times
...
Brynn
11 months ago
The code will print 5, as we create a BaseC object with the constructor that takes an integer argument and pass in the value 5.
upvoted 0 times
Eden
9 months ago
C) It prints: 5
upvoted 0 times
...
Van
9 months ago
B) It prints: 5
upvoted 0 times
...
Leana
10 months ago
A) It prints: 5
upvoted 0 times
...
...

Save Cancel