Skip to content
QuiznExam

File System (fs) Module in Node.js

Test your knowledge of the Node.js File System (fs) module, including reading and writing files, creating and removing directories, file statistics, synchronous and asynchronous methods, and file system operations.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is the purpose of the fs module in Node.js?

    • A. To interact with the file system
    • B. To create HTTP requests only
    • C. To manage browser cookies
    • D. To compile JavaScript code
    Show answer

    A. To interact with the file system

    The built-in fs module provides APIs for working with files and directories on the file system.

  2. 2. How can the fs module be imported using CommonJS?

    • A. const fs = require('fs');
    • B. import fs from 'filesystem';
    • C. include('fs');
    • D. const fs = require('filesystem');
    Show answer

    A. const fs = require('fs');

    In CommonJS, the built-in fs module can be loaded using require('fs').

  3. 3. Which fs method is commonly used to read a file asynchronously?

    • A. fs.readFile()
    • B. fs.openFile()
    • C. fs.getFile()
    • D. fs.loadFile()
    Show answer

    A. fs.readFile()

    fs.readFile() asynchronously reads the contents of a file.

  4. 4. Which fs method reads a file synchronously?

    • A. fs.readFileSync()
    • B. fs.syncRead()
    • C. fs.readSyncFile()
    • D. fs.getFileSync()
    Show answer

    A. fs.readFileSync()

    fs.readFileSync() reads a file synchronously and blocks execution until the operation completes.

  5. 5. Which fs method is used to write data to a file asynchronously?

    • A. fs.writeFile()
    • B. fs.saveFile()
    • C. fs.writeData()
    • D. fs.createFile()
    Show answer

    A. fs.writeFile()

    fs.writeFile() asynchronously writes data to a file, replacing the file if it already exists unless a different flag or method is used.

  6. 6. Which fs method can be used to add data to the end of an existing file?

    • A. fs.appendFile()
    • B. fs.addFile()
    • C. fs.insertFile()
    • D. fs.extendFile()
    Show answer

    A. fs.appendFile()

    fs.appendFile() adds the specified data to the end of a file, creating the file if it does not exist.

  7. 7. Which fs method is used to create a directory?

    • A. fs.mkdir()
    • B. fs.createDirectory()
    • C. fs.newDir()
    • D. fs.makeDirectory()
    Show answer

    A. fs.mkdir()

    fs.mkdir() is used to create a new directory.

  8. 8. Which fs method is commonly used to read the contents of a directory?

    • A. fs.readdir()
    • B. fs.readDirectory()
    • C. fs.listFiles()
    • D. fs.getDirectory()
    Show answer

    A. fs.readdir()

    fs.readdir() reads the contents of a directory and returns its entries.

  9. 9. Which fs method is used to remove a file?

    • A. fs.unlink()
    • B. fs.deleteFile()
    • C. fs.removeFile()
    • D. fs.erase()
    Show answer

    A. fs.unlink()

    fs.unlink() asynchronously removes a file from the file system.

  10. 10. Which fs method can be used to obtain information about a file or directory?

    • A. fs.stat()
    • B. fs.info()
    • C. fs.details()
    • D. fs.inspectFile()
    Show answer

    A. fs.stat()

    fs.stat() retrieves information about a file or directory, such as its size and timestamps.

  11. 11. Which property of an fs.Stats object can be used to determine whether the path represents a directory?

    • A. isDirectory()
    • B. isFolder()
    • C. directory()
    • D. checkDirectory()
    Show answer

    A. isDirectory()

    The isDirectory() method on an fs.Stats object returns true when the referenced path represents a directory.

  12. 12. Which encoding is commonly specified when reading a text file as a string?

    • A. utf8
    • B. text16
    • C. unicodeText
    • D. string8
    Show answer

    A. utf8

    Using the utf8 encoding with fs.readFile() causes the file contents to be returned as a string instead of a Buffer.

  13. 13. Why should synchronous fs methods generally be used carefully in a Node.js server?

    • A. They can block the event loop while the operation completes
    • B. They cannot access files
    • C. They only work in browsers
    • D. They automatically delete files
    Show answer

    A. They can block the event loop while the operation completes

    Synchronous file system operations block the JavaScript thread until they finish, which can reduce server responsiveness when used during request handling.

  14. 14. Which fs method can be used to rename or move a file?

    • A. fs.rename()
    • B. fs.moveFile()
    • C. fs.changeName()
    • D. fs.relocate()
    Show answer

    A. fs.rename()

    fs.rename() can rename a file or directory and can also move it when the destination path is on the same file system.

  15. 15. Which statement best describes the promise-based version of the Node.js fs API?

    • A. It provides file system methods that return Promises
    • B. It only supports synchronous operations
    • C. It is available only in browsers
    • D. It removes the need for file permissions
    Show answer

    A. It provides file system methods that return Promises

    Node.js provides a promise-based file system API through fs/promises, allowing file operations to be used with async and await.