Bird
0
0

Identify the error in this code snippet that reads all lines from "notes.txt":

medium📝 Debug Q14 of 15
C Sharp (C#) - File IO
Identify the error in this code snippet that reads all lines from "notes.txt":
string[] lines = File.ReadAllLines(notes.txt);
foreach (string line in lines)
{
    Console.WriteLine(line);
}
AConsole.WriteLine cannot print strings.
BFile.ReadAllLines returns a string, not string array.
Cforeach loop syntax is incorrect.
DMissing quotes around the file name in ReadAllLines.
Step-by-Step Solution
Solution:
  1. Step 1: Check file path syntax

    The file name must be a string literal, so it needs quotes: "notes.txt".
  2. Step 2: Verify other code parts

    File.ReadAllLines returns string array, foreach syntax is correct, and Console.WriteLine can print strings.
  3. Final Answer:

    Missing quotes around the file name in ReadAllLines. -> Option D
  4. Quick Check:

    File path must be in quotes [OK]
Quick Trick: File names must be in quotes in method calls [OK]
Common Mistakes:
MISTAKES
  • Forgetting quotes around file path
  • Thinking ReadAllLines returns string
  • Misunderstanding foreach syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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