Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q14 of 15
C Sharp (C#) - File IO
Identify the error in the following code snippet:
File.Copy("source.txt", "dest.txt");
File.Delete("source.txt");
File.Copy("source.txt", "dest.txt");
ANo error, code runs fine
BFile.Delete should be called before the first File.Copy
CFile.Copy cannot copy files with .txt extension
DSecond File.Copy will throw an exception because source.txt was deleted
Step-by-Step Solution
Solution:
  1. Step 1: Trace the file operations

    First, source.txt is copied to dest.txt. Then source.txt is deleted. Finally, the code tries to copy source.txt again.
  2. Step 2: Identify the problem

    After deletion, source.txt no longer exists, so the second copy call will throw a FileNotFoundException.
  3. Final Answer:

    Second File.Copy will throw an exception because source.txt was deleted -> Option D
  4. Quick Check:

    Copy after delete causes error [OK]
Quick Trick: Cannot copy a file after deleting it [OK]
Common Mistakes:
MISTAKES
  • Assuming File.Copy works on deleted files
  • Thinking order of Delete and Copy doesn't matter
  • Believing .txt files cannot be copied

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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