Challenge - 5 Problems
File Paths and Directories Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this directory creation code?
Consider the following C# code that creates a directory and checks if it exists. What will be printed?
C Sharp (C#)
using System; using System.IO; class Program { static void Main() { string path = "TestDir"; Directory.CreateDirectory(path); Console.WriteLine(Directory.Exists(path)); } }
Attempts:
2 left
💡 Hint
Think about what Directory.CreateDirectory does if the directory already exists.
✗ Incorrect
Directory.CreateDirectory creates the directory if it doesn't exist, or does nothing if it already exists. Directory.Exists returns true if the directory exists, so the output is True.
❓ Predict Output
intermediate2:00remaining
What does this code print about file paths?
Given this C# code snippet, what will be the output?
C Sharp (C#)
using System; using System.IO; class Program { static void Main() { string filePath = "C:\\Users\\Public\\Documents\\report.txt"; Console.WriteLine(Path.GetFileName(filePath)); } }
Attempts:
2 left
💡 Hint
Path.GetFileName returns the file name and extension from a full path.
✗ Incorrect
Path.GetFileName extracts the file name with extension from the full path, so it returns 'report.txt'.
❓ Predict Output
advanced2:00remaining
What is the output of this code listing files?
This code lists files in a directory. What will it print?
C Sharp (C#)
using System; using System.IO; class Program { static void Main() { string dir = "TestDir"; Directory.CreateDirectory(dir); File.WriteAllText(Path.Combine(dir, "file1.txt"), "Hello"); File.WriteAllText(Path.Combine(dir, "file2.txt"), "World"); var files = Directory.GetFiles(dir); Console.WriteLine(files.Length); } }
Attempts:
2 left
💡 Hint
Files are created before listing, so count should reflect that.
✗ Incorrect
Two files are created in the directory before calling GetFiles, so the length of the files array is 2.
❓ Predict Output
advanced2:00remaining
What error does this code raise when deleting a non-empty directory?
What happens when you run this code that tries to delete a directory containing files?
C Sharp (C#)
using System; using System.IO; class Program { static void Main() { string dir = "TestDir"; Directory.CreateDirectory(dir); File.WriteAllText(Path.Combine(dir, "file.txt"), "data"); Directory.Delete(dir); Console.WriteLine("Deleted"); } }
Attempts:
2 left
💡 Hint
Deleting a directory with files without recursive flag causes an error.
✗ Incorrect
Directory.Delete without recursive flag throws IOException if the directory is not empty.
🧠 Conceptual
expert3:00remaining
How many items are in the resulting dictionary after this path manipulation?
Consider this C# code that uses DirectoryInfo and FileInfo objects. How many entries will the dictionary contain?
C Sharp (C#)
using System; using System.Collections.Generic; using System.IO; class Program { static void Main() { string dir = "TestDir"; Directory.CreateDirectory(dir); File.WriteAllText(Path.Combine(dir, "a.txt"), "A"); File.WriteAllText(Path.Combine(dir, "b.txt"), "B"); var dict = new Dictionary<string, long>(); var directoryInfo = new DirectoryInfo(dir); foreach (var file in directoryInfo.GetFiles()) { dict[file.Name] = file.Length; } Console.WriteLine(dict.Count); } }
Attempts:
2 left
💡 Hint
Each file in the directory is added as a dictionary entry.
✗ Incorrect
Two files are created and added to the dictionary, so the count is 2.