Deno File System Operations Quiz Quiz

Challenge your understanding of Deno's file system operations, including reading, writing, manipulating files, and working with directories. This quiz helps you solidify your skills in handling and managing files efficiently using Deno's standard APIs.

  1. Reading Files

    Which Deno function reads the entire contents of a file asynchronously as a string, given a UTF-8 encoded file path?

    1. Deno.openFile
    2. Deno.readTextFile
    3. Deno.writeFile
    4. Deno.getFile

    Explanation: Deno.readTextFile is used to asynchronously read the entire contents of a file as a UTF-8 string. Deno.writeFile is for writing data to files, not reading. Deno.getFile and Deno.openFile are not valid Deno APIs; they may look similar but do not exist, which makes them distractors. Only Deno.readTextFile matches the described scenario.

  2. Writing Files

    Suppose you want to create or overwrite a text file with new content. Which built-in Deno function should you use to write a string to a file asynchronously?

    1. Deno.readFile
    2. Deno.readTextFileSync
    3. Deno.writeTextFile
    4. Deno.appendFile

    Explanation: Deno.writeTextFile is used to asynchronously write a string to a file, creating or overwriting it as needed. Deno.readTextFileSync is for reading files synchronously, not writing. Deno.appendFile does not exist in Deno; the correct Deno API is appendTextFile. Deno.readFile reads raw bytes instead of writing data.

  3. Checking File Existence

    Which method allows you to find out whether a file exists at a given path before performing further operations?

    1. Deno.exists
    2. Deno.pathCheck
    3. Deno.stat
    4. Deno.isFile

    Explanation: Deno.stat is the asynchronous function that retrieves information about a file or directory; it can be used to determine if a file exists by handling exceptions. Deno.exists is not a built-in function in Deno. Deno.pathCheck and Deno.isFile are incorrect and not native functions. Deno.stat provides access to file metadata, making it the correct choice here.

  4. Removing Files

    Given the path of a file you wish to delete, which Deno function would you use to remove it asynchronously?

    1. Deno.deleteFile
    2. Deno.unlinkFile
    3. Deno.remove
    4. Deno.clearFile

    Explanation: Deno.remove is the correct asynchronous method for removing files or directories by their path. Deno.deleteFile and Deno.unlinkFile may look familiar from other environments, but they are not part of Deno's API. Deno.clearFile is not a real API call. The only valid choice within Deno is Deno.remove.

  5. Creating Directories

    If you want to make a new directory, including any necessary parent directories that do not exist, which Deno function and option should you use?

    1. Deno.createDirectory with { all: true }
    2. Deno.makeDir with { parent: true }
    3. Deno.mkdir with { recursive: true }
    4. Deno.mkdirAll with {}

    Explanation: Deno.mkdir with the option { recursive: true } allows for creating directories along with any non-existing parent directories. Deno.makeDir and Deno.createDirectory are not part of Deno’s API. Deno.mkdirAll does not exist. Hence, only Deno.mkdir with the specified option achieves the described behavior.

  6. Listing Directory Contents

    To obtain all the entries (such as files and folders) inside a specific directory, which asynchronous Deno function would you choose?

    1. Deno.listFiles
    2. Deno.scanDir
    3. Deno.readDir
    4. Deno.getDirectory

    Explanation: Deno.readDir asynchronously returns an iterable of directory entries within a directory. Deno.scanDir and Deno.listFiles are not actual Deno functions. Deno.getDirectory is also incorrect. Of the choices given, only Deno.readDir is valid in Deno for listing directory contents.

  7. Renaming Files

    If you need to change the name of an existing file using Deno, which function should you use?

    1. Deno.move
    2. Deno.rename
    3. Deno.modifyFile
    4. Deno.switchFile

    Explanation: Deno.rename is the function for renaming files or moving them to a new location. Deno.move and Deno.switchFile are not functions in Deno, and Deno.modifyFile does not exist. Therefore, Deno.rename is the only suitable and correct option here.

  8. Copying Files

    Suppose you want to duplicate a file to a different path on disk. Which synchronous Deno function should you use?

    1. Deno.duplicateFileSync
    2. Deno.cloneFile
    3. Deno.copyFileSync
    4. Deno.copyFile

    Explanation: Deno.copyFileSync is the correct synchronous API for copying a file. Deno.copyFile does exist but is asynchronous, not synchronous. Deno.duplicateFileSync and Deno.cloneFile are not actual Deno functions. Only Deno.copyFileSync fits the requirement for a synchronous file copy.

  9. Reading Raw Bytes

    You wish to read the raw byte content of a file for binary processing. Which function is designed for this purpose in Deno?

    1. Deno.readBytes
    2. Deno.readBuffer
    3. Deno.readFile
    4. Deno.readTextFile

    Explanation: Deno.readFile reads the contents of a file into a Uint8Array (bytes), which is ideal for binary data. Deno.readTextFile is for strings, not raw bytes. Deno.readBytes and Deno.readBuffer are not valid Deno APIs. Only Deno.readFile allows direct access to a file's raw bytes.

  10. Setting Permissions

    Which Deno function allows you to change the permissions of a file, such as making it executable, given the file's path and permission mode?

    1. Deno.fileMode
    2. Deno.chmod
    3. Deno.modifyPerms
    4. Deno.setPermissions

    Explanation: Deno.chmod changes the permissions of a file or directory using a specified mode (for example, making a file executable). Deno.setPermissions and Deno.modifyPerms are not part of Deno’s API, and Deno.fileMode does not set permissions. Only Deno.chmod provides the ability to directly adjust file permissions.