Explore core Node.js basics with this comprehensive quiz designed for beginners, covering modules, event handling, asynchronous code, and practical usage. Reinforce your knowledge of Node.js concepts, syntax, and best practices to improve your skills and understanding of server-side JavaScript.
Which statement best describes Node.js as a runtime?
Explanation: Node.js enables JavaScript to run outside of web browsers, allowing server-side applications. The option about running only in browsers is incorrect, as Node.js works on servers and desktops. Node.js does not require Java code compilation, making the Java-related statement incorrect. While Node.js can be used for desktop applications, it is not exclusive to them.
How do you include a built-in module like 'fs' for file operations in Node.js?
Explanation: The correct way to include modules in Node.js uses the require function, such as const fs = require('fs');. The statement 'import module.fs;' is invalid syntax. 'var fs = importModule('fs');' is not a standard Node.js method. 'use('fs');' does not exist in Node.js for loading modules.
Which feature in Node.js is designed for handling asynchronous operations like reading files?
Explanation: Callback functions allow Node.js to process tasks asynchronously by running code after an operation finishes. Synchronous blocks execute code sequentially and can block the thread, making them unsuitable for non-blocking operations. Infinite loops do not relate to handling async events and can freeze the application. Static typing is not involved in managing asynchronous logic.
Which global object provides the path of the current file in Node.js?
Explanation: The __filename global variable gives the full path and name of the currently-executing file in Node.js. The options 'filePath', 'thisFile', and '__mainfile' are not valid predefined Node.js global objects, making them incorrect choices.
Which core module is required to create an HTTP server in Node.js?
Explanation: The 'http' module is the built-in Node.js module for creating HTTP servers and handling requests. 'Networks' and 'webserver' are not standard modules in Node.js. 'request' used to be a common external package, but it is not a built-in module.
How do you access an environment variable named 'PORT' inside a Node.js script?
Explanation: Environment variables in Node.js are accessed through the 'process.env' object, so process.env.PORT is correct. There are no built-in 'env' or 'this.env' objects in Node.js, and 'PORT.getEnv()' is not a valid function.
Which tool is commonly used to manage packages and dependencies in Node.js projects?
Explanation: npm (Node Package Manager) is the standard tool for managing packages in Node.js projects. 'rpm' is related to Linux package management. 'pip' is used for Python, and 'gem' is associated with Ruby, not Node.js.
What is the standard way to print a message to the console in Node.js?
Explanation: console.log() is the standard JavaScript and Node.js function for printing to the console. 'print()' is not defined in core Node.js, and 'echo()' is generally used in shell scripting or PHP. 'System.out.println()' comes from other programming languages like Java, not JavaScript.
What mechanism does Node.js use to respond to events such as 'data received' or 'connection closed'?
Explanation: Event emitters and listeners are the primary pattern in Node.js for handling asynchronous events, such as network connections or data transmission. 'Policy handlers' and 'thread priorities' are not Node.js concepts, and 'direct interrupts' pertain more to low-level operating system operations.
What is the typical default entry file name for a Node.js application?
Explanation: index.js is conventionally used as the main entry point when starting a Node.js application if no alternative is specified. The other filenames, like 'appstart.js', 'mainfile.js', and 'runfile.js', are not standard defaults although they could be used if configured.