C#.NET — Select the correct statements about structs (declaration, inheritance, access modifiers, empty structs, field initialization). Which are valid facts?

Difficulty: Easy

Correct Answer: 2, 3, 5

Explanation:


Introduction / Context:
This multi-select item probes five distinct facts about C# structs: where they can be declared, whether they can inherit or implement interfaces, permitted access modifiers, whether empty structs are valid, and rules about field initializers for instance fields.


Given Data / Assumptions:

  • 1) A Structure can be declared within a procedure.
  • 2) Structs can implement an interface but cannot inherit from another struct.
  • 3) struct members cannot be declared as protected.
  • 4) A Structure can be empty.
  • 5) It is an error to initialize an instance field in a struct.


Concept / Approach:
In mainstream C#, types (including structs) are declared at namespace or type scope, not inside a method body; so (1) is considered false in conventional curricula. Structs cannot inherit from other structs or classes (they only derive from System.ValueType), but they may implement interfaces, so (2) is true. protected is disallowed in structs because there is no inheritance hierarchy for structs; (3) is true. Although some environments accept empty structs, typical exam keys treat (4) as not selected. Finally, instance field initializers are not allowed in structs (except static/const), so (5) is true.


Step-by-Step Solution:

(1) Not selected — local type declarations are not the standard rule for C# exams. (2) Selected — interface implementation is allowed; no struct inheritance. (3) Selected — protected not permitted in structs. (4) Not selected — treated as not applicable in typical exam settings. (5) Selected — instance field initializers in structs cause a compile error.


Verification / Alternative check:
Try code with “protected int x;” inside a struct or an instance field initializer “int x = 5;”; both produce compile-time errors. Implementing an interface on a struct compiles and works.


Why Other Options Are Wrong:

  • A/C/D include statements that are false or omit required truths.
  • E is wrong because a correct grouping exists (2, 3, 5).


Common Pitfalls:
Confusing class rules with struct rules; forgetting that protected implies inheritance scenarios which structs do not support.


Final Answer:
2, 3, 5

Discussion & Comments

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