0
0
C Sharp (C#)programming~20 mins

File class static methods in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code using File.ReadAllText?

Consider a file named example.txt containing the text Hello World. What will the following C# code output?

C Sharp (C#)
string content = System.IO.File.ReadAllText("example.txt");
Console.WriteLine(content);
AHello World
Bexample.txt
CSystem.IO.File
DFileNotFoundException
Attempts:
2 left
💡 Hint

File.ReadAllText reads the entire content of the file as a string.

Predict Output
intermediate
2:00remaining
What does File.Exists return for a missing file?

What will the following code print if the file missing.txt does not exist?

C Sharp (C#)
bool exists = System.IO.File.Exists("missing.txt");
Console.WriteLine(exists);
ATrue
BFalse
CThrows FileNotFoundException
Dnull
Attempts:
2 left
💡 Hint

File.Exists returns a boolean indicating if the file is present.

Predict Output
advanced
2:00remaining
What is the output of File.ReadAllLines with multiple lines?

Given a file data.txt with these lines:

Line1
Line2
Line3

What will the following code print?

C Sharp (C#)
string[] lines = System.IO.File.ReadAllLines("data.txt");
Console.WriteLine(lines.Length);
A3
B1
C0
DThrows IOException
Attempts:
2 left
💡 Hint

File.ReadAllLines returns an array of lines from the file.

Predict Output
advanced
2:00remaining
What error does File.Delete throw if file is open and locked?

What exception will be thrown if you try to delete a file that is currently open and locked by another process?

C Sharp (C#)
System.IO.File.Delete("lockedfile.txt");
AUnauthorizedAccessException
BFileNotFoundException
CIOException
DNo exception, file deleted
Attempts:
2 left
💡 Hint

Deleting a locked file causes an IO error.

🧠 Conceptual
expert
2:00remaining
Which File method creates a new file or overwrites existing one with text?

Which static method of the File class will create a new file or overwrite an existing file with the given string content?

AFile.AppendAllText(path, content)
BFile.OpenRead(path)
CFile.Create(path)
DFile.WriteAllText(path, content)
Attempts:
2 left
💡 Hint

One method writes text and replaces existing content.