Bird
Raised Fist0

What will be the output of this code if "data.txt" contains the lines: "One", "Two", "Three"?

medium📝 Predict Output Q4 of Q15
C Sharp (C#) - File IO
What will be the output of this code if "data.txt" contains the lines: "One", "Two", "Three"?
var lines = File.ReadAllLines("data.txt");
Console.WriteLine(lines[1]);
ATwo
BOne
CThree
DIndex out of range error
Step-by-Step Solution
Solution:
  1. Step 1: Understand ReadAllLines output

    ReadAllLines returns an array where each element is a line from the file, zero-indexed.
  2. Step 2: Identify the line at index 1

    Index 0 is "One", index 1 is "Two", index 2 is "Three".
  3. Final Answer:

    Two -> Option A
  4. Quick Check:

    Array index 1 = second line = "Two" [OK]
Quick Trick: Arrays start at 0, so index 1 is second line [OK]
Common Mistakes:
MISTAKES
  • Assuming index 1 is first line
  • Expecting output 'One'
  • Thinking it throws error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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