0
0
Node.jsframework~20 mins

Built-in modules overview in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Built-in Modules Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of using the 'path' module to join paths
What is the output of this Node.js code snippet using the built-in path module?
Node.js
import path from 'path';
console.log(path.join('folder', 'subfolder', 'file.txt'));
A'folder\\subfolder\\file.txt'
B'folder.subfolder.file.txt'
C'folder-subfolder-file.txt'
D'folder/subfolder/file.txt'
Attempts:
2 left
💡 Hint
Think about how the path.join method works on Windows systems.
component_behavior
intermediate
2:00remaining
Behavior of the 'fs' module's readFileSync method
What happens when you run this Node.js code using the fs module's readFileSync method on a non-existent file?
Node.js
import fs from 'fs';
try {
  const data = fs.readFileSync('nofile.txt', 'utf8');
  console.log(data);
} catch (err) {
  console.log('Error caught');
}
AThrows an uncaught exception and stops execution
BPrints 'undefined' if file does not exist
CPrints the file content if exists, otherwise prints 'Error caught'
DCreates an empty file named 'nofile.txt' and prints nothing
Attempts:
2 left
💡 Hint
Consider how readFileSync behaves when the file is missing and how the try-catch block works.
📝 Syntax
advanced
2:00remaining
Correct import syntax for the 'os' module in Node.js ES modules
Which option shows the correct way to import the built-in os module in a Node.js ES module environment?
Aimport * as os from 'os';
Bconst os = require('os');
Cimport os from 'os';
Dimport { os } from 'node:os';
Attempts:
2 left
💡 Hint
Remember that require is not used in ES modules and how to import entire modules.
🔧 Debug
advanced
2:00remaining
Identify the error in this 'http' server code snippet
What error will this Node.js code produce when run, and why?
Node.js
import http from 'http';
const server = http.createServer((req, res) => {
  res.write('Hello World');
  res.end();
});
server.listen(3000);
console.log('Server running on port 3000');

// Later in code
server.listen(3000);
ARuns fine and prints 'Server running on port 3000' twice
BServer listens on two ports simultaneously without error
CThrows a SyntaxError due to duplicate listen calls
DThrows an error: 'Error: listen EADDRINUSE: address already in use 3000'
Attempts:
2 left
💡 Hint
Think about what happens if you try to listen on the same port twice.
🧠 Conceptual
expert
3:00remaining
Understanding the behavior of the 'events' module with multiple listeners
Consider this Node.js code using the events module. What will be the output when the event is emitted?
Node.js
import EventEmitter from 'events';
const emitter = new EventEmitter();
emitter.on('ping', () => console.log('First listener'));
emitter.once('ping', () => console.log('Second listener'));
emitter.emit('ping');
emitter.emit('ping');
ASecond listener\nFirst listener\nSecond listener
BFirst listener\nSecond listener\nFirst listener
CFirst listener\nFirst listener
DSecond listener\nSecond listener
Attempts:
2 left
💡 Hint
Remember the difference between on and once listeners.