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 questionsCorrect 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.
Keep practicing
Take the free CPA-21-02 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other CPA-21-02 practice questions
- Question 1What happens when you attempt to compile and run the following code? #include using names…
- Question 2What happens when you attempt to compile and run the following code? #include #include us…
- Question 3Which of the following statements are correct about an array? int tab[10];
- Question 4What happens when you attempt to compile and run the following code? #include #include us…
- Question 5What is the output of the program if characters 'h', 'e', 'l', 'l' , 'o' and enter are su…
- Question 6What happens when you attempt to compile and run the following code? #include using names…
