Difficulty: Easy
Correct Answer: The program will print the output 5.
Explanation:
Introduction / Context:
This checks whether a const-qualified member function can read and print a data member that was assigned in the constructor. It reinforces the difference between reading (allowed in const methods) and writing (disallowed) to members.
Given Data / Assumptions:
Concept / Approach:
A const member function promises not to modify any non-mutable data members. Reading x and sending it to cout is legal. Since x is assigned in the constructor, its value is well-defined when Show() executes.
Step-by-Step Solution:
Verification / Alternative check:
Marking Show() non-const would not change behavior here. Attempting to modify x inside Show() would fail to compile due to const qualification.
Why Other Options Are Wrong:
Common Pitfalls:
Assuming const methods can’t even read members or forgetting to initialize members before use.
Final Answer:
The program will print the output 5.
Discussion & Comments