Node.js Fundamentals: Server-side Concepts Quiz Quiz

Assess your understanding of Node.js server-side development with questions covering event-driven architecture, asynchronous programming, common built-in modules, and essential JavaScript concepts used in Node.js environments. This quiz helps reinforce key skills for anyone interested in building scalable network applications using Node.js.

  1. Event Loop Functionality

    What is the primary role of the event loop in Node.js, especially when multiple client requests are made to a server at once?

    1. It allows Node.js to process many requests simultaneously without creating multiple threads.
    2. It blocks execution until all tasks are finished.
    3. It converts JavaScript code into machine code directly.
    4. It automatically scales the server’s hardware resources.

    Explanation: The event loop is central to Node.js, enabling it to handle many concurrent connections efficiently using a single thread. This non-blocking approach increases scalability and performance. Blocking execution, as in option B, defeats the purpose of the event loop. Option C is managed at the system level, not by the event loop. Option D describes a compiler function, which is not the event loop’s job.

  2. Non-blocking Code Example

    Which method illustrates non-blocking code execution in Node.js when reading a file?

    1. Reading a file asynchronously using a callback function.
    2. Reading a file synchronously and waiting for completion.
    3. Using a busy-wait loop to check file status.
    4. Locking the main thread before starting file operations.

    Explanation: Asynchronous reading with a callback allows the rest of the code to execute while waiting for the file operation to finish, demonstrating non-blocking behavior. Busy-wait loops, as in option B, can block the event loop. Synchronous reading, option C, halts progress until completed. Locking the thread, option D, also blocks execution, which is against Node.js’s asynchronous principles.

  3. Common Built-in Module

    Which built-in module in Node.js provides functionalities for creating HTTP servers?

    1. webserver
    2. serverkit
    3. http
    4. netconnect

    Explanation: The 'http' module is built into Node.js and supplies key methods for building web servers and handling HTTP requests and responses. 'Netconnect' and 'webserver' are not core modules in Node.js, while 'serverkit' is not a recognized module. Only 'http' directly fits the function described.

  4. Global Object in Node.js

    What global object in Node.js can be used to access environment-specific variables and information about the current operating system process?

    1. osinfo
    2. window
    3. env
    4. process

    Explanation: The 'process' object is globally available in Node.js and gives access to environment variables, arguments, and process-related methods. 'Window' is typically used in browsers, not in Node.js. 'Env' is a property of 'process', not a global object. 'Osinfo' does not exist as a core object in Node.js.

  5. Exporting and Importing

    When sharing functions between modules in Node.js, which statement is correct for exporting a function?

    1. Assign the function to exportdefault.
    2. Assign the function to module.exports.
    3. Assign the function to this.
    4. Assign the function to global.

    Explanation: Using 'module.exports' is the standard approach for exporting values, functions, or objects from a Node.js module. Assigning to 'global' would make it available everywhere but is not recommended for exports. 'This' does not reliably reference the export object in modules. 'Exportdefault' is not a recognized syntax in Node.js modules.

  6. Asynchronous Callback Example

    Which statement best describes a proper use of a callback function in Node.js?

    1. A function is declared globally to be accessed by any module.
    2. A function is attached to the window object for browser usage.
    3. A function is called synchronously inside a loop.
    4. A function is passed as an argument to be called after an asynchronous task completes.

    Explanation: Node.js uses callbacks extensively to continue processing after asynchronous tasks, ensuring non-blocking flow. Declaring a global function (option B) does not relate to asynchronous logic. Calling synchronously in a loop (option C) is unrelated to asynchronous execution. Attaching to 'window' (option D) is for browser environments, not Node.js.

  7. Listening on a Port

    When starting an HTTP server in Node.js, which method is commonly used to bind the server to a specific port, such as 3000?

    1. connect(3000)
    2. subscribe(3000)
    3. listen(3000)
    4. port(3000)

    Explanation: The 'listen' method is used on server instances to begin listening for incoming requests on a given port. 'Subscribe' and 'connect' are not standard methods for HTTP server setup. 'Port' is not a recognized method for this purpose in Node.js.

  8. Handling JSON Data

    What is the correct way to convert a JavaScript object to a JSON string before sending it as an HTTP response in Node.js?

    1. Invoke toJSONString() on the object.
    2. Call object.convertToJson().
    3. Use JSON.stringify() on the object.
    4. Pass the object directly to res.sendString().

    Explanation: The built-in 'JSON.stringify()' method turns a JavaScript object into a JSON-formatted string. 'ConvertToJson()' and 'toJSONString()' are not standard JavaScript methods. Passing an object directly to 'sendString()' (option D) is not a typical pattern in Node.js.

  9. Error Handling in Callbacks

    Which convention is typically followed in Node.js callback functions for reporting errors?

    1. Error details are logged but not passed to the callback.
    2. The callback returns true if an error occurred.
    3. The first argument of the callback is reserved for an error object or null.
    4. Errors are always thrown and caught outside the callback.

    Explanation: Node.js commonly uses an error-first callback pattern, where the first parameter contains the error or null if no error occurred. Returning true (option B) or only logging errors (option D) are not standard practices. Throwing and catching errors outside of callbacks (option C) can lead to unhandled exceptions.

  10. Single-threaded Nature

    Why is Node.js described as single-threaded, and how does it handle multiple tasks efficiently?

    1. It assigns a separate thread to each client connection.
    2. It uses internal thread pools for all JavaScript code execution.
    3. It serializes all tasks, executing only one at a time with no concurrency.
    4. It uses a single thread along with asynchronous non-blocking I/O operations.

    Explanation: Node.js is single-threaded for JavaScript code execution but relies on asynchronous, non-blocking I/O to manage multiple operations concurrently. Assigning separate threads (option B) contradicts the single-threaded model. Option C is inaccurate, as thread pools are used for some background operations only. Serializing all tasks (option D) would prevent efficiently handling concurrent operations, which is not how Node.js works.