Node.js test (Intermediate)
30 questions · 35 minutes · Free
Event loop, async patterns, streams and Express APIs.
What it covers
- Event loop phases
- Promises and async/await
- Streams
- Express middleware
- Error handling
Interview format
- Questions: 30 questions — 18 multiple choice, 9 multi-answer, 3 predict-the-output
- Time: 35 minutes, timed from the first question
- Difficulty: Intermediate
- Skill area: Backend
- Price: Free
Earn a certificate
The Node.js test is one of the 8 tests of the Backend Developer learning path. Pass it and the other 7 tests at 70% or more and readyforhires issues you a verifiable certificate in your name, with its own code and verify link to share or add to LinkedIn.
Sample questions
5 sample questions from the Node.js test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.
Consider the following code: async function fetchData() { throw new Error('Data not found'); } try { fetchData(); } catch (err) { console.log('Error caught'); } What will happen?
- The error is not caught because the try/catch block is inside the async function.
- The error is caught by the try/catch block because it is inside the async function.
- The error is caught by the try/catch block because it is outside the async function.
- The error is not caught because the try/catch block is outside the async function.
Answer: D. The error is not caught because the try/catch block is outside the async function.
In JavaScript, errors thrown inside an async function must be caught using try/catch within the same async function or using .catch() on the returned promise. Placing the try/catch block outside the async function will not catch errors that occur inside it.
Which approach is preferred when dealing with nested promises and why?
- Using Promise.race() is preferred when dealing with nested promises because it allows for handling the first resolved promise.
- Using Promise chaining with .then() and .catch() is preferred when dealing with nested promises because it is more concise and efficient.
- Using async/await with try/catch is preferred when dealing with nested promises because it improves code readability and makes error handling more straightforward.
- Using Promise.all() is preferred when dealing with nested promises because it allows for parallel execution of promises.
Answer: C. Using async/await with try/catch is preferred when dealing with nested promises because it improves code readability and makes error handling more straightforward.
When dealing with nested promises, using async/await with try/catch is generally preferred over Promise chaining because it improves code readability and makes error handling more straightforward. Promise chaining can become difficult to manage with deeply nested promises, as it requires multiple .then() and .catch() calls, increasing the risk of errors and making the code harder to follow. Additionally, async/await allows for more natural control flow and better debugging capabilities.
Which of the following correctly describes the role of the highWaterMark in a readable stream in Node.js?
- The highWaterMark is used to specify the encoding of the data being read from the stream.
- The highWaterMark controls the maximum size of the internal buffer, and when it is exceeded, the stream will pause and wait for the 'drain' event before continuing to read data.
- The highWaterMark is a setting that determines how frequently the 'data' event is emitted.
- The highWaterMark is only relevant for writable streams and not for readable streams.
Answer: B. The highWaterMark controls the maximum size of the internal buffer, and when it is exceeded, the stream will pause and wait for the 'drain' event before continuing to read data.
The highWaterMark is a crucial concept in Node.js streams. It determines how much data can be buffered internally before the stream pauses to avoid overwhelming the system. When the buffer exceeds the highWaterMark, the stream will pause and wait for the 'drain' event before resuming data flow. This is a common mistake: not understanding how the highWaterMark affects performance and backpressure handling.
Which event is emitted by a writable stream when all data has been successfully flushed to the underlying system?
- error
- finish
- data
- close
Answer: B. finish
The 'finish' event in a writable stream is emitted when all data has been flushed to the underlying system, indicating that the stream is done writing. The 'data' event is for readable streams, 'error' is for handling errors, and 'close' is emitted when the stream is closed, but not necessarily when writing is complete.
What is the difference between Promise.any() and Promise.race()?
- Promise.any() and Promise.race() behave identically, returning the first settled promise.
- Promise.any() returns the first rejected promise, while Promise.race() returns the first fulfilled promise.
- Promise.any() returns the first fulfilled promise, while Promise.race() returns the first settled promise (whether fulfilled or rejected).
- Promise.any() returns the first settled promise, while Promise.race() returns the first fulfilled promise.
Answer: C. Promise.any() returns the first fulfilled promise, while Promise.race() returns the first settled promise (whether fulfilled or rejected).
Promise.any() returns the first fulfilled promise, whereas Promise.race() returns the first settled promise, regardless of whether it is fulfilled or rejected. This is a key distinction between the two methods, and understanding their behavior is crucial for handling asynchronous operations correctly.
Frequently asked questions
- How many questions are in the Node.js test?
- 30 questions (18 multiple choice, 9 multi-answer, 3 predict-the-output), with 35 minutes to answer them.
- What does the Node.js test cover?
- Event loop phases, Promises and async/await, Streams, Express middleware, Error handling.
- Is the Node.js test free?
- Yes. It is free to take with a readyforhires account.
- What level is the Node.js test?
- Intermediate, in Backend.