Bird
Raised Fist0

Find the error in this code snippet:

medium📝 Debug Q7 of Q15
C Sharp (C#) - Strings and StringBuilder
Find the error in this code snippet:
StringBuilder sb = new StringBuilder();
sb.AppendLine("Line1");
sb.AppendLine(123);
Console.WriteLine(sb);
AMissing ToString() when printing StringBuilder
BAppendLine adds no newline character
CStringBuilder must be initialized with a string
DAppendLine cannot take an integer argument
Step-by-Step Solution
Solution:
  1. Step 1: Check AppendLine method overloads

    AppendLine only accepts no argument or string; no overload for integer or object.
  2. Step 2: Identify the invalid call

    sb.AppendLine(123) fails to compile as there is no AppendLine(int) overload.
  3. Final Answer:

    AppendLine cannot take an integer argument -> Option D
  4. Quick Check:

    AppendLine overloads = string only [OK]
Quick Trick: AppendLine(string) only; use Append for ints/objects [OK]
Common Mistakes:
MISTAKES
  • Confusing AppendLine with Append (which accepts object)
  • Believing explicit ToString() needed for Console.WriteLine(sb)
  • Thinking AppendLine adds no newline

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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