Bird
Raised Fist0

Identify the error in this C# code snippet for reading a file:

medium📝 Debug Q14 of Q15
C Sharp (C#) - File IO
Identify the error in this C# code snippet for reading a file:
string content = File.ReadAllText("missing.txt");
Console.WriteLine(content);
AThe file path is incorrect syntax.
BConsole.WriteLine cannot print strings.
CFile.ReadAllText throws an exception if file is missing.
DThe code is missing a semicolon.
Step-by-Step Solution
Solution:
  1. Step 1: Understand File.ReadAllText behavior

    If the file "missing.txt" does not exist, File.ReadAllText throws a FileNotFoundException.
  2. Step 2: Check other options

    Syntax is correct, Console.WriteLine can print strings, semicolons are present.
  3. Final Answer:

    File.ReadAllText throws an exception if file is missing. -> Option C
  4. Quick Check:

    Missing file causes exception in ReadAllText [OK]
Quick Trick: Missing file causes exception on read [OK]
Common Mistakes:
MISTAKES
  • Assuming missing file returns empty string
  • Thinking Console.WriteLine can't print strings
  • Believing syntax error due to semicolon

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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