0
0
Node.jsframework~20 mins

Single-threaded non-blocking I/O concept in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Non-blocking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does Node.js handle multiple I/O operations?
Node.js uses a single thread for JavaScript execution. How does it manage to handle multiple I/O operations without blocking the thread?
AIt uses multiple threads internally to run JavaScript code in parallel.
BIt delegates I/O operations to the system kernel and uses callbacks to handle results asynchronously.
CIt queues all I/O operations and runs them one by one synchronously.
DIt pauses JavaScript execution until each I/O operation completes.
Attempts:
2 left
💡 Hint
Think about how Node.js can keep running code while waiting for files or network responses.
component_behavior
intermediate
2:00remaining
What is the output order of this Node.js code?
Consider this code snippet: console.log('Start'); setTimeout(() => console.log('Timeout'), 0); console.log('End'); What will be the order of the printed lines?
Node.js
console.log('Start');
setTimeout(() => console.log('Timeout'), 0);
console.log('End');
AStart, Timeout, End
BTimeout, Start, End
CStart, End, Timeout
DEnd, Start, Timeout
Attempts:
2 left
💡 Hint
Remember that setTimeout with 0 delay still runs after the current code finishes.
🔧 Debug
advanced
2:00remaining
Why does this Node.js code block the event loop?
Look at this code: const fs = require('fs'); const data = fs.readFileSync('file.txt'); console.log('File read'); What is the problem with this code in terms of Node.js single-threaded non-blocking I/O?
Node.js
const fs = require('fs');

const data = fs.readFileSync('file.txt');
console.log('File read');
AreadFileSync blocks the event loop until the file is read, stopping other code from running.
BreadFileSync runs asynchronously and does not block the event loop.
CThe code will throw an error because readFileSync requires a callback.
DThe console.log will run before the file is read.
Attempts:
2 left
💡 Hint
Check if readFileSync is synchronous or asynchronous.
📝 Syntax
advanced
2:00remaining
Which option correctly uses a Promise to read a file asynchronously in Node.js?
You want to read a file asynchronously using Promises. Which code snippet is correct?
A
const fs = require('fs');
const data = fs.readFile('file.txt').then(console.log);
B
const fs = require('fs/promises');
const data = fs.readFileSync('file.txt');
console.log(data);
C
const fs = require('fs');
fs.readFile('file.txt', (err, data) => { if (!err) console.log(data); });
D
const fs = require('fs/promises');
fs.readFile('file.txt').then(data => console.log(data.toString()));
Attempts:
2 left
💡 Hint
Look for the correct module and method that returns a Promise.
state_output
expert
2:00remaining
What is the output of this Node.js event loop example?
Analyze this code: console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); console.log('4');
Node.js
console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
console.log('4');
A1, 4, 3, 2
B1, 3, 4, 2
C1, 2, 3, 4
D1, 4, 2, 3
Attempts:
2 left
💡 Hint
Remember microtasks (Promises) run before timers in the event loop.