String and StringBuilder in C#.NET: select the correct statements. 1) Two strings can be concatenated using s3 = s1 + s2; 2) String is a primitive in C#.NET. 3) A string built with StringBuilder is mutable. 4) A string built with String is immutable. 5) Two strings can be concatenated using s3 = s1 & s2;

Difficulty: Easy

Correct Answer: 1, 3, 4

Explanation:


Introduction / Context:
This question reviews fundamental properties of string and StringBuilder and the standard concatenation syntax in C#.



Given Data / Assumptions:

  • Statements to validate include concatenation forms and mutability claims.


Concept / Approach:
In C#, strings are immutable, and the + operator concatenates strings. StringBuilder provides a mutable buffer for efficient concatenations or edits. The & concatenation operator is a VB feature, not C#.



Step-by-Step Solution:

1) s3 = s1 + s2 → Valid in C#.2) 'String is a primitive' → False; it is a reference type with special handling.3) StringBuilder-built strings are mutable → True (the builder is mutable; resulting ToString() is immutable).4) Strings are immutable → True.5) s3 = s1 & s2 → Not C# syntax; false.


Verification / Alternative check:
Creating a StringBuilder and appending confirms mutability; attempting to modify an existing string shows it produces a new instance.



Why Other Options Are Wrong:
Options including 2 or 5 assert incorrect claims about primitives or use non-C# operators.



Common Pitfalls:
Assuming VB operators apply in C#; forgetting the performance implications of repeated + in loops.



Final Answer:
1, 3, 4

Discussion & Comments

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