Bird
0
0

What will be the output of the following C# code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - File IO
What will be the output of the following C# code?
using System;
using System.IO;

class Program {
    static void Main() {
        using (var fs = new FileStream("test.txt", FileMode.Create)) {
            byte[] data = {72, 105};
            fs.Write(data, 0, data.Length);
        }
        using (var fs = new FileStream("test.txt", FileMode.Open)) {
            byte[] buffer = new byte[2];
            fs.Read(buffer, 0, buffer.Length);
            Console.WriteLine(System.Text.Encoding.ASCII.GetString(buffer));
        }
    }
}
AError: File not found
B72,105
CSystem.Byte[]
DHi
Step-by-Step Solution
Solution:
  1. Step 1: Write bytes to file

    The code writes bytes 72 and 105 to "test.txt". These bytes represent ASCII characters 'H' and 'i'.
  2. Step 2: Read bytes and convert to string

    The code reads the two bytes back and converts them to a string using ASCII encoding, resulting in "Hi".
  3. Final Answer:

    Hi -> Option D
  4. Quick Check:

    Bytes 72,105 = 'Hi' string [OK]
Quick Trick: ASCII codes 72 and 105 spell 'Hi' [OK]
Common Mistakes:
MISTAKES
  • Expecting byte array printed instead of string
  • Confusing byte values with characters
  • Assuming file read fails without checking creation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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