C#.NET strings — identify the correct statements about string type, literals, equality, bounds, and mutability.

Difficulty: Easy

Correct Answer: 2, 4

Explanation:


Introduction / Context:
Immutable strings are fundamental in .NET. This item checks knowledge of string type (reference vs value), literal rules, equality operator behavior, bounds checking, and whether contents can change after creation.



Given Data / Assumptions:

  • Five statements about strings are presented; select all true ones.
  • C# string is an alias for System.String.


Concept / Approach:
Evaluate each claim against .NET semantics: immutability, operator overloading for equality, and runtime exceptions on invalid indexing.



Step-by-Step Solution:

1) “String is a value type.” → False. It is a reference type.2) “String literals can contain escape sequences.” → True. Examples include "Hello\nWorld" or verbatim strings @""C:\temp"".3) “Equality operators compare values as well as references.” → False. == for string is overloaded to compare values (contents), not reference identity.4) “Out-of-bounds character access throws IndexOutOfRangeException.” → True for string indexer.5) “String contents can be changed after creation.” → False. Strings are immutable; use StringBuilder for in-place-like edits.


Verification / Alternative check:
Test object.ReferenceEquals(s1, s2) vs s1 == s2 to see value vs reference behavior; try s[9999] to observe the exception.



Why Other Options Are Wrong:
Any option containing 1, 3, or 5 is invalid due to the reasons above.



Common Pitfalls:
Confusing string interning (which can make references equal) with the semantics of ==, which compares contents.



Final Answer:
2, 4

Discussion & Comments

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