Difficulty: Easy
Correct Answer: Both A and B.
Explanation:
Introduction / Context:
Static members in C++ belong to the class itself rather than to any individual object. Understanding their access rules and lifetime is critical for designing shared counters, cached state, or class-wide configuration. This question asks which statements accurately describe static data members and static member functions.
Given Data / Assumptions:
Concept / Approach:
A static member function lacks the implicit object pointer (this), so it cannot access non-static members directly because those require an object. It can, however, access static data members. A static data member is indeed shared across all objects. While you can access a public static data member “from main” using ClassName::member (or through an object), the option’s wording “directly” is ambiguous. Therefore, the best precise choice among the provided options is “Both A and B.”
Step-by-Step Solution:
Verification / Alternative check:
Write a small program with a class counter and a static incrementor(). Attempting to read a non-static field from a static function without an object fails to compile. Accessing the static field as ClassName::counter works as expected.
Why Other Options Are Wrong:
C alone overstates “direct” access without clarifying syntax and access level.
A or B alone omit the other equally correct fact.
Common Pitfalls:
Final Answer:
Both A and B.
Discussion & Comments