Bird
Raised Fist0

Identify the error in this code snippet:

medium📝 Debug Q14 of Q15
C Sharp (C#) - Strings and StringBuilder
Identify the error in this code snippet:
string first = "Good";
string second = "Morning";
string message = first + second;
message += 5;
Console.WriteLine(message);
AMissing space between first and second strings.
BCannot add integer 5 to a string using += operator.
CVariable 'message' is not declared.
DConsole.WriteLine syntax is incorrect.
Step-by-Step Solution
Solution:
  1. Step 1: Check string concatenation and += usage

    The code concatenates "Good" and "Morning" without space, so message becomes "GoodMorning". Adding 5 converts 5 to string "5" and appends it, resulting in "GoodMorning5".
  2. Step 2: Identify the main issue

    The code runs without error, but the missing space between words is a logical mistake causing output to look wrong.
  3. Final Answer:

    Missing space between first and second strings. -> Option A
  4. Quick Check:

    Check spaces when joining strings [OK]
Quick Trick: Add spaces explicitly when joining words [OK]
Common Mistakes:
MISTAKES
  • Assuming += 5 causes error
  • Ignoring missing spaces in output
  • Thinking Console.WriteLine is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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