Bird
Raised Fist0

What is the output of the following code?

medium📝 Predict Output Q13 of Q15
C Sharp (C#) - Strings and StringBuilder
What is the output of the following code?
string s = "  Hello World  ";
string result = s.Trim().Substring(0, 5);
Console.WriteLine(result);
A" Hel"
B"Hello"
C"Hello "
D"Hello World"
Step-by-Step Solution
Solution:
  1. Step 1: Apply Trim() method

    Trim() removes spaces at the start and end, so " Hello World " becomes "Hello World".
  2. Step 2: Apply Substring(0, 5)

    Substring(0, 5) extracts characters from index 0 to 4, which is "Hello".
  3. Final Answer:

    "Hello" -> Option B
  4. Quick Check:

    Trim + Substring(0,5) = "Hello" [OK]
Quick Trick: Trim removes spaces; Substring extracts exact part [OK]
Common Mistakes:
MISTAKES
  • Not trimming before substring
  • Counting spaces in substring
  • Confusing substring length

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes