C#.NET strings — evaluate the output of: String s1 = "ALL MEN ARE CREATED EQUAL"; String s2; s2 = s1.Substring(12, 3); Console.WriteLine(s2);

Difficulty: Easy

Correct Answer: CRE

Explanation:

Introduction / Context:This tests correct use of Substring(startIndex, length) with zero-based indexing.

Given Data / Assumptions:

  • s1 = "ALL MEN ARE CREATED EQUAL"
  • startIndex = 12, length = 3

Concept / Approach:Substring takes characters starting at startIndex and returns exactly length characters. Indexes are zero-based and include spaces as characters.

Step-by-Step Solution:

Enumerate indices: 0 A, 1 L, 2 L, 3 space, 4 M, 5 E, 6 N, 7 space, 8 A, 9 R, 10 E, 11 space, 12 C, 13 R, 14 E, ...Starting at 12 for 3 characters → "C", "R", "E" → "CRE".

Verification / Alternative check:Print s1[12], s1[13], s1[14] to confirm characters.

Why Other Options Are Wrong:ARE or REA correspond to other ranges; CREATED is too long.

Common Pitfalls:Off-by-one errors or forgetting spaces count toward indices.

Final Answer:CRE

More Questions from Strings

Discussion & Comments

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