What happens when you attempt to compile and run the following code?
#include
using namespace std;
class A {
public:
int x;
A() { x=0;}
};
class B : public A {
public:
B() { x=1;}
};
class C : private B {
public:
C() { x=2;}
};
int main () {
C c1;
cout << c1.x;
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
20 comments · newest first
HO
Hobert
1 month ago
I’m leaning towards D, the access to x is restricted.
HA
Haley
2 months ago
I agree with Phuong, it initializes x in C.
BE
Bette
2 months ago
It prints 010 because of private inheritance.
LI
Lili
2 months ago
I feel like there's a compilation error.
TY
Tyra
2 months ago
No way, it should be 110.
PH
Phuong
2 months ago
I think it will print 210.
TW
Twana
2 months ago
No way it prints 110, that's incorrect!
GO
Golda
3 months ago
I thought it would print 210, but now I'm not so sure.
VE
Vesta
3 months ago
Wait, can C access x from A like that?
DE
Delila
3 months ago
Definitely a compilation error!
GE
Georgene
3 months ago
It prints: 010
EL
Elza
3 months ago
Totally agree with the compilation error theory!
MA
Margery
3 months ago
Wait, why can't we access x from C?
VA
Vallie
4 months ago
I think it prints 210.
AL
Alishia
5 months ago
No way, it should be a compilation error!
LE
Lenora
5 months ago
It prints: 010
RU
Rupert
6 months ago
I’m leaning towards a compilation error because of the private inheritance from B. I don’t think main can access x in class C.
LO
Lou
6 months ago
I practiced a similar question where inheritance affected variable access. I think this one might print 0 or 1, but I can't recall exactly how private inheritance works here.
AD
Adelaide
6 months ago
I remember something about private inheritance. I feel like that might prevent access to x in class C, which could lead to a compilation error.
CH
Chaya
6 months ago
I think the code will compile, but I'm not sure what value will be printed. It seems like it should be 2 since C sets x to 2.