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.
Which statement correctly imports the built-in file system module in Node.js?
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.
Which Node.js function reads the contents of a file synchronously?
Explanation: 'fs.readFileSync()' reads files synchronously. 'fs.readFile()' is the asynchronous version, while the other two are not valid Node.js functions.
What is the purpose of the 'fs.writeFile()' function in Node.js?
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.
Which method checks if a file exists in a specified path in Node.js?
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.
Which Node.js function can be used to remove a file from the file system?
Explanation: 'fs.unlink()' deletes files in Node.js. The other options look plausible but are not actual Node.js methods for file deletion.
How can you read the contents of a directory in Node.js?
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.
Which function appends content to the end of a file asynchronously in Node.js?
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.
In Node.js, which method can be used to rename a file?
Explanation: 'fs.rename()' changes a file's name. 'fs.moveFile()', 'fs.changeName()', and 'fs.fileRename()' are not valid file-renaming functions in Node.js.
What function should you use to create a new folder in Node.js?
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.
What is a key difference between 'fs.readFile()' and 'fs.readFileSync()'?
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.