What happens when you attempt to compile and run the following code?
#include
#include
using namespace std;
class A {
int x;
protected:
int y;
public:
int z;
A() { x=1; y=2; z=3; }
};
class B : public A {
public:
void set() {
y = 4; z = 2;
}
void Print() {
cout << y << z;
}
};
int main () {
B b;
b.set();
b.Print();
return 0;
}
Suggested Answer: A
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
35 comments · newest first
YV
Yvonne
10 months ago
So, y gets updated but x is private? Interesting!
GA
Gary
10 months ago
Totally agree, that's the output!
CO
Colette
10 months ago
It prints: 42
CA
Cassi
10 months ago
I thought it would print 44.
YV
Yvonne
11 months ago
Wait, are you sure about that?
RO
Rory
11 months ago
I feel like it should print 44 because z is public and gets set to 2, but I’m not entirely confident about the initial values of y and z.
ER
Eric
11 months ago
I practiced a similar question, and I think the output depends on the order of initialization. I’m a bit confused about what gets printed first.
TY
Tyisha
11 months ago
I remember something about protected members being accessible in derived classes, so y should be set to 4 in set(). That might mean it prints 42?
FR
Frank
11 months ago
I think the constructor of class A initializes x, y, and z, but I'm not sure how that affects the output when we call Print in class B.
BO
Bonita
12 months ago
Hmm, I'm not entirely sure about this one. I think the protected member y in A is accessible to B, but I'm not 100% confident. I'll need to carefully walk through the code step-by-step to make sure I understand how the inheritance and access modifiers are working here.
MA
Margurite
12 months ago
I've got this! The key is understanding how the protected member y in the base class A is accessible to the derived class B. When B calls the set() method, it sets y to 4 and z to 2. Then the Print() method outputs those values, so the final output is 42.
HU
Huey
12 months ago
I'm a bit confused by the access modifiers here. I know protected members can be accessed by derived classes, but I'm not sure how that applies to the y and z members. I'll need to review my notes on inheritance and access control to figure this one out.
WI
William
1 year ago
Okay, let me think this through step-by-step. The base class A has a private member x, a protected member y, and a public member z. The derived class B inherits publicly from A and has access to y and z. The set() method in B sets y to 4 and z to 2. The Print() method then prints y and z, so I think the output should be 42.
SY
Sylvia
1 year ago
Hmm, this looks like a tricky one. I'll need to carefully trace the code to understand how the inheritance and access modifiers are working here.
AL
Alease
2 years ago
Ha! This is a classic trick question. The answer is obviously C) It prints: 22. Why, you ask? Because the protected member y is set to 4, and the public member z is set to 2. So when we print them, we get 42. Easy as pie!
YO
Yoko
1 year ago
No way, it prints: 22. Trust me on this one.
TU
Tula
1 year ago
I think you're mistaken. It actually prints: 42
VI
Vincenza
2 years ago
I believe it prints: 44 too, because the Print function in class B outputs the values of y and z
CL
Claudia
2 years ago
Hmm, this is tricky. I'm going to have to think about it carefully. Wait, I've got it! The protected member y is accessible in the derived class, so we can modify it. And since z is public, we can also change its value. The output should be 44. I'll go with B) It prints: 44.
YV
Yvonne
1 year ago
So, the correct answer is B) It prints: 44.
KI
Kina
1 year ago
Yes, and since z is public, its value can also be changed.
BE
Bethanie
1 year ago
I agree, the protected member y can be modified in the derived class.
CE
Celestine
1 year ago
I think the output will be 44.
AV
Avery
2 years ago
I'm not sure, but I think it prints: 44 as well
PE
Pearly
2 years ago
I agree with Cammy, because the set function in class B changes the value of y to 4 and z to 2
CA
Cammy
2 years ago
I think it prints: 44
SH
Shayne
2 years ago
Okay, let's think this through. The default constructor of class A initializes x to 1, y to 2, and z to 3. In the set() function, we change y to 4 and z to 2. So the output should be 42. I'll go with B) It prints: 44.
DA
Daryl
2 years ago
Ah, a classic inheritance question. The protected member y is accessible in the derived class B, so we can modify it in the set() function. And since z is public, we can also change its value. The output should be 44. This is an easy one!
ME
Melina
2 years ago
Hmm, let's see. The code creates an object of class B, which inherits from class A. The set() function sets the value of the protected member y to 4 and the public member z to 2. The Print() function then prints the values of y and z. I think the answer is B) It prints: 44.
YV
Yvonne
1 year ago
Actually, it prints: 42.
LA
Laticia
1 year ago
Actually, it prints: 44.
RI
Ricarda
1 year ago
No, I believe it prints: 42.
AM
Amber
1 year ago
No, I believe it prints: 42.
WI
Willodean
2 years ago
I think the answer is B) It prints: 44.
BR
Brynn
2 years ago
I think the answer is B) It prints: 44.