Bird
0
0

What will be the output of this code snippet if the file "numbers.txt" contains the lines: "1", "2", "3"?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - File IO
What will be the output of this code snippet if the file "numbers.txt" contains the lines: "1", "2", "3"?
var lines = File.ReadAllLines("numbers.txt");
Console.WriteLine(lines.Length);
A1
B3
C0
DThrows an exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand what ReadAllLines returns

    It returns an array of strings, each line is one element.
  2. Step 2: Count the number of lines in the file

    The file has 3 lines, so lines.Length is 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    Lines count = 3 [OK]
Quick Trick: ReadAllLines returns array; Length gives line count [OK]
Common Mistakes:
MISTAKES
  • Assuming it returns a single string
  • Expecting zero if file exists
  • Thinking it throws exception without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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