C#.NET — Which statements about static methods are correct?

Difficulty: Easy

Correct Answer: 1) Static methods can access only static data. 2) Static methods cannot call instance methods. 4) Instance methods can call static methods and access static data.

Explanation:


Introduction / Context:
Static members are associated with the type, not any particular object instance. This affects what they can access and how they are invoked.



Given Data / Assumptions:

  • We assess five statements about accessibility and invocation.


Concept / Approach:
A static method lacks a this reference; therefore, it cannot touch instance fields or call instance methods unless given an explicit instance. Conversely, instance methods run with this and can freely call static methods or read static fields. Static data is zero-initialized by the runtime, so explicit initialization is not strictly necessary.



Step-by-Step Solution:

1) True — only static data is directly available to static methods.2) True — cannot call instance methods without an instance.3) False — static fields have default initialization; explicit initialization is optional.4) True — instance methods can access static members.5) False — no this in static methods.


Verification / Alternative check:
Compile code that references instance members from a static method; you will get compile-time errors unless you use an instance variable.



Why Other Options Are Wrong:

  • B/C/D: Each includes at least one false item (3 or 5).
  • E: Not applicable; there is a correct set (1, 2, 4).


Common Pitfalls:
Believing static members must be explicitly initialized; the CLR provides default values.



Final Answer:
1, 2, 4

More Questions from Constructors

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion