Node.js File System Basics Quiz

Explore essential concepts of the Node.js file system module with easy questions covering usage, syntax, and core features. Perfect for reviewing foundational knowledge about handling files in Node.js.

  1. Importing the File System Module

    Which statement correctly imports the built-in file system module in Node.js?

    1. import fs from 'filesystem';
    2. const fs = require('fs');
    3. const filesystem = require('file');
    4. require('node:fs') as fs;

    Explanation: The correct syntax to load the built-in file system module in Node.js is 'const fs = require('fs');'. The second and fourth options use incorrect import syntax or module names, and the third uses the wrong module identifier.

  2. Reading a File Synchronously

    Which Node.js function reads the contents of a file synchronously?

    1. fs.readSyncFile()
    2. fs.fileReadSync()
    3. fs.readFile()
    4. fs.readFileSync()

    Explanation: 'fs.readFileSync()' reads files synchronously. 'fs.readFile()' is the asynchronous version, while the other two are not valid Node.js functions.

  3. Writing to a File

    What is the purpose of the 'fs.writeFile()' function in Node.js?

    1. Lists directory contents
    2. Writes data to a file asynchronously
    3. Reads data from a file synchronously
    4. Deletes files from the system

    Explanation: 'fs.writeFile()' writes data to a file asynchronously. It does not read files, delete files, or list directories, which are tasks handled by other specific functions.

  4. Checking File Existence

    Which method checks if a file exists in a specified path in Node.js?

    1. fs.existsSync()
    2. fs.isFile()
    3. fs.fileStatus()
    4. fs.fileExists()

    Explanation: 'fs.existsSync()' synchronously checks if a file exists. 'fs.fileExists()', 'fs.isFile()', and 'fs.fileStatus()' are not valid methods and do not perform this check.

  5. Deleting a File

    Which Node.js function can be used to remove a file from the file system?

    1. fs.deleteFile()
    2. fs.erase()
    3. fs.unlink()
    4. fs.removeFile()

    Explanation: 'fs.unlink()' deletes files in Node.js. The other options look plausible but are not actual Node.js methods for file deletion.

  6. Reading a Directory

    How can you read the contents of a directory in Node.js?

    1. fs.readDirSync()
    2. fs.dirList()
    3. fs.dirRead()
    4. fs.readdir()

    Explanation: 'fs.readdir()' is the correct method for reading directory contents. 'fs.dirList()', 'fs.readDirSync()', and 'fs.dirRead()' are not valid functions in the context of Node.js.

  7. Appending Data to a File

    Which function appends content to the end of a file asynchronously in Node.js?

    1. fs.appendFile()
    2. fs.appendSync()
    3. fs.addToFile()
    4. fs.increaseFile()

    Explanation: 'fs.appendFile()' appends content asynchronously. 'fs.addToFile()' and 'fs.increaseFile()' are not standard methods, and 'fs.appendSync()' implies a sync method not present in Node.js.

  8. Renaming a File

    In Node.js, which method can be used to rename a file?

    1. fs.changeName()
    2. fs.moveFile()
    3. fs.fileRename()
    4. fs.rename()

    Explanation: 'fs.rename()' changes a file's name. 'fs.moveFile()', 'fs.changeName()', and 'fs.fileRename()' are not valid file-renaming functions in Node.js.

  9. Creating a Directory

    What function should you use to create a new folder in Node.js?

    1. fs.dirCreate()
    2. fs.mkdir()
    3. fs.createDir()
    4. fs.makeDirectory()

    Explanation: 'fs.mkdir()' is the method for creating directories. The other options use incorrect or imaginary method names and will not work for this purpose.

  10. Asynchronous vs Synchronous Methods

    What is a key difference between 'fs.readFile()' and 'fs.readFileSync()'?

    1. 'fs.readFileSync()' does not exist in Node.js
    2. 'fs.readFile()' is faster in all cases
    3. 'fs.readFile()' is asynchronous, 'fs.readFileSync()' is synchronous
    4. 'fs.readFile()' is used only for directories

    Explanation: 'fs.readFile()' operates asynchronously and does not block the event loop, while 'fs.readFileSync()' performs operations synchronously. 'fs.readFileSync()' does exist, speed depends on context, and neither is only for directories.