Difficulty: Easy
Correct Answer: 2, 3, 4
Explanation:
Introduction / Context:
This question checks your grasp of C#.NET inheritance basics: how access modifiers (public, protected) behave across a base–derived chain, and how constructors run when an instance of a derived class is created.
Given Data / Assumptions:
count
is declared in the base class index
.index1
derives from index
and writes to count
in increment()
.
Concept / Approach:
Members marked protected
are accessible in the declaring class and in derived classes. Public is broader (everywhere), but not required for inheritance visibility. Constructors are not inherited as members; they are invoked in a chain: base constructor executes first, then the derived constructor.
Step-by-Step Solution:
count
should be public to be available” → False. protected
is designed for inheritance access; public is not necessary.Statement 2: “count
should be protected to be available” → True. Matches C# rules.Statement 3: “While constructing, base constructor is called first, then derived” → True. Construction flows base → derived.Statement 4: “Constructors do not get inherited” → True. Constructors are not inherited; each class defines its own.Statement 5: “Friend
(VB) should be used” → False in C#. The nearest term is internal
, which is assembly visibility, not inheritance-specific.
Verification / Alternative check:
Make count
private and see the compile error in index1.increment()
. Switch back to protected
to fix it. Add a diagnostic print in base and derived constructors to observe the base-first, derived-second order.
Why Other Options Are Wrong:
Any option including 1 or 5 is wrong because they mis-state the correct access level for inheritance or use a non-C# access modifier concept.
Common Pitfalls:
Confusing public
(world-visible) with protected
(inheritance-visible). Assuming constructors are inherited like methods (they are not).
Final Answer:
2, 3, 4
Derived
object if each int
is 4 bytes (ignore header/alignment)?
Discussion & Comments