CPA-21-02 practice question 19 of 35

What happens when you attempt to compile and run the following code? #include #include using namespace std; class A { public: A() { cout

Full set: 257 questions
Answer options
A: It prints: A no parametersA no parametersB string parameter
B: It prints: A string parameterA no parametersB int parameterA object A parameter Correct answer
C: It prints: A no parametersB string parameter
D: It prints: A no parametersA no parameters
Correct Answer:
B. It prints: A string parameterA no parametersB int parameterA object A parameter
Explanation

First, A a2("Test") invokes the parameterized constructor of A, printing "A string parameter". Second, B b1(10) invokes B's integer constructor; because no base class constructor is explicitly called in an initializer list, A's default constructor runs first ("A no parameters"), followed by B's constructor body ("B int parameter"). Finally, B b2(b1) invokes the implicitly generated copy constructor of B. This implicit copy constructor automatically calls the base class A's user-defined copy constructor A(A &a), printing "A object A parameter".

Why Incorrect

A. Incorrect because it misses the output from a2's initialization and incorrectly identifies the constructors called for b1 and b2.
C. Incorrect because it omits the output from a2 and the copy constructor invocation for b2.
D. Incorrect because it fails to account for the parameterized constructors and the copy constructor being invoked.

References

- ISO/IEC 14882:2017 (C++17 Standard), Section 15.6.2 [class.base.init], Paragraph 13.

- ISO/IEC 14882:2017 (C++17 Standard), Section 15.8.1 [class.copy.ctor], Paragraph 14.

Scroll to Top