Bird
0
0

What will the following code output if the file "log.txt" does not exist?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - File IO
What will the following code output if the file "log.txt" does not exist?
string content = File.ReadAllText("log.txt");
Console.WriteLine(content);
AEmpty string
BThrows FileNotFoundException
Cnull
DOutputs "log.txt"
Step-by-Step Solution
Solution:
  1. Step 1: Understand behavior of ReadAllText on missing file

    File.ReadAllText throws FileNotFoundException if file is missing.
  2. Step 2: Confirm no default return value

    It does not return empty string or null; it throws an exception.
  3. Final Answer:

    Throws FileNotFoundException -> Option B
  4. Quick Check:

    Missing file read = Exception thrown [OK]
Quick Trick: ReadAllText throws if file missing, handle exceptions [OK]
Common Mistakes:
MISTAKES
  • Expecting empty string
  • Assuming null return
  • Ignoring exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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