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

File paths and Directory operations in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Paths and Directories Master
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 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));
    }
}
ATrue
BFalse
CThrows IOException
DThrows UnauthorizedAccessException
Attempts:
2 left
💡 Hint
Think about what Directory.CreateDirectory does if the directory already exists.
Predict Output
intermediate
2: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));
    }
}
Areport.txt
BC:\Users\Public\Documents\report.txt
Creport
DDocuments
Attempts:
2 left
💡 Hint
Path.GetFileName returns the file name and extension from a full path.
Predict Output
advanced
2: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);
    }
}
A1
B2
CThrows DirectoryNotFoundException
D0
Attempts:
2 left
💡 Hint
Files are created before listing, so count should reflect that.
Predict Output
advanced
2: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");
    }
}
AThrows UnauthorizedAccessException
BPrints 'Deleted'
CThrows IOException
DThrows DirectoryNotFoundException
Attempts:
2 left
💡 Hint
Deleting a directory with files without recursive flag causes an error.
🧠 Conceptual
expert
3: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);
    }
}
AThrows IOException
B0
C1
D2
Attempts:
2 left
💡 Hint
Each file in the directory is added as a dictionary entry.