CPA-21-02 practice question 25 of 35

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; }; class B : public A { string name; public: void set() { y = 2; z = 3; } void Print() { cout

Full set: 257 questions
Answer options
A: It prints: 123
B: It prints: 000
C: It prints: 23 Correct answer
D: It prints: 12
Correct Answer:
C. It prints: 23
Explanation

Class B inherits publicly from Class A. The protected member y and public member z of Class A are accessible within the derived Class B. The set() method assigns 2 to y and 3 to z. The Print() method outputs these values sequentially without spaces, resulting in "23".

Why Incorrect

A: Incorrect because x is private in A and not printed, and the values are 2 and 3. B: Incorrect because the values are explicitly set to 2 and 3, not 0. D: Incorrect because it prints y and z (2 and 3), not 1 and 2.

References

- ISO/IEC 14882:2017 (C++17 Standard), Section 14.2 [class.access.base], Paragraph 1.

Scroll to Top