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 CPA-21-02 Exam - Topic 1 Question 7 Discussion

Posted by Raul on Dec 25, 2023 at 04:17 AM

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

#include

#include

using namespace std;

class A {

public:

A() { cout << "A0 ";}

A(string s) { cout << "A1";}

};

class B : public A {

public:

B() { cout << "B0 ";}

B(string s) { cout << "B1 ";}

};

class C : private B {

public:

C() { cout << "C0 ";}

C(string s) { cout << "C1 ";}

};

int main () {

B b1;

C c1;

return 0;

}

Suggested Answer: D

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
37 comments · newest first
AH
Ahmed 11 months ago
This seems off, I doubt it prints that way.
MO
Mona 11 months ago
Wait, does it really not call A1 at all?
TA
Tamesha 11 months ago
Nah, I’m pretty sure it’s just B0 B1.
RO
Rozella 11 months ago
I think it should be A0 B0 C0 for sure!
RH
Rhea 11 months ago
It prints: A0 B0 C0
MU
Muriel 12 months ago
I thought the output would include B1 since there's a string constructor in B, but I guess it only gets called if you create a B with a string?
TI
Timmy 12 months ago
I practiced a similar question where the base class constructor was called first. I feel like it should print A0 B0 and then C0, but I could be wrong.
RO
Ronny 1 year ago
I'm not entirely sure, but I remember something about private inheritance affecting how constructors are called. Does that mean C's constructor won't call B's?
RE
Reuben 1 year ago
I think when you create an object of class B, it should call the constructor of A first, right? So maybe it prints A0 B0?
WI
William 1 year ago
This is a tricky one. The private inheritance in class C is throwing me off a bit. I'll need to review the rules around constructor calls and inheritance to make sure I understand what's happening here.
SA
Samira 1 year ago
I'm a bit confused by the private inheritance in class C. Does that affect the constructor calls in some way? I'll have to think through the order of constructor invocations to solve this.
AL
Alfred 1 year ago
Hmm, this looks like it's testing our understanding of inheritance and constructor calls. I'll need to carefully trace the object creation and constructor invocations to figure out the correct output.
ME
Mendy 1 year ago
Okay, I think I've got it. Since class B is publicly inherited from A, the B constructors will call the A constructors. And since class C is privately inherited from B, the C constructors will only call the default B constructor. The output should be straightforward to determine.
PA
Pamella 1 year ago
I feel pretty confident that a Visual Prototype is the way to go here. It would give the customer a clear visual representation of the new online experience, which seems like the most effective way to communicate the changes.
IS
Isaiah 1 year ago
This seems pretty straightforward. The focus group was used to test the customer experience, so I'd go with option A - Focus group.
AM
Ammie 1 year ago
Okay, let's see. We need to create three interfaces, so we'll want to use a loop to do that. I'm leaning towards A, 'with_items', since that's the most straightforward way to iterate over a list of interface names.
LE
Leonida 1 year ago
Ah, the age-old constructor conundrum strikes again! I'm going with C) for this one. After all, who doesn't love a good old-fashioned 'A no parameters' followed by a 'B string parameter'? It's like a symphony of C++ goodness.
DI
Diego 1 year ago
↳ replying to Leonida
Great, let's go with C) then.
RO
Rolf 1 year ago
↳ replying to Leonida
That's what I was thinking too. C) it is.
DA
Daniela 1 year ago
↳ replying to Leonida
Yeah, I agree. It should print: A no parameters B string parameter.
CH
Chaya 1 year ago
↳ replying to Leonida
I think C) is the correct answer.
DE
Deangelo 1 year ago
Wait, wait, wait... is the answer D) It prints: A no parametersA no parameters? I'm pretty sure that's wrong, but I can't quite figure out the right answer. Maybe I should have paid more attention in that inheritance lecture.
DU
Dustin 1 year ago
Ha! This is a classic constructor chaining problem. The answer is clearly C) It prints: A no parametersB string parameter. I've seen this kind of question before, it's all about understanding the order of constructor calls.
JO
Joaquin 1 year ago
↳ replying to Dustin
Understanding constructor chaining is key in this scenario.
DE
Dell 1 year ago
↳ replying to Dustin
Yes, the answer is definitely C) It prints: A no parametersB string parameter.
AN
Antonio 1 year ago
↳ replying to Dustin
I agree, it's all about the order of constructor calls.
VO
Von 1 year ago
Hmm, this question is tricky. I think the answer is B) It prints: A string parameterA no parametersB string parameterA object A parameter. But I'm not sure, I might need to double-check the inheritance rules in C++.
KI
Kirby 1 year ago
The correct answer is C) It prints: A no parametersB string parameter. The constructors of the base classes are called in the order of inheritance, so the output should be A0 B1 C0.
KA
Karma 1 year ago
↳ replying to Kirby
So the output should be A0 B1 C0.
RO
Rocco 1 year ago
↳ replying to Kirby
Oh, I see. The constructors of the base classes are called in the order of inheritance.
DA
Daryl 1 year ago
↳ replying to Kirby
I think the correct answer is C) It prints: A no parametersB string parameter.
HU
Huey 1 year ago
Hmm, that makes sense too. Let's see what others think before deciding.
XO
Xochitl 2 years ago
I disagree, I believe the answer is B because the code creates objects of class B and class C with string parameters.
HU
Huey 2 years ago
I think the answer is A because the code creates an object of class B without any parameters.
CA
Cathrine 2 years ago
Hmm, I see your point. But I still think option A makes more sense based on the code logic.
KE
Kenneth 2 years ago
I disagree, I believe the correct answer is C. It prints: A no parametersB string parameter.
CA
Cathrine 2 years ago
I think the answer is A. It prints: A no parametersA no parametersB string parameter.
0/2000 characters

Save Cancel