JavaScript Node.js Basics 2 — Questions and Answers
Question 1: Which Node.js global object provides information about the current process, such as its PID and environment variables?
- global
- process (Correct answer)
- runtime
- system
Correct answer: process
The `process` global object exposes process-level data including `process.pid`, `process.env`, `process.argv`, and lifecycle methods like `process.exit()`.
Question 2: What does the `fs.readFileSync()` method do differently from `fs.readFile()`?
- It reads only binary files
- It blocks the event loop until the file is fully read (Correct answer)
- It streams the file in chunks
- It caches the file contents in memory permanently
Correct answer: It blocks the event loop until the file is fully read
`fs.readFileSync()` is synchronous and blocks the event loop until the operation completes, whereas `fs.readFile()` is asynchronous and uses a callback.
Question 3: In Node.js, what is the purpose of the `__dirname` variable?
- It returns the current working directory of the process
- It returns the absolute path of the directory containing the current module file (Correct answer)
- It returns the root directory of the Node.js installation
- It returns the name of the current executing script
Correct answer: It returns the absolute path of the directory containing the current module file
`__dirname` is a module-scoped variable that holds the absolute path of the directory where the current module file resides.
Question 4: Which method is used to make a directory recursively in Node.js's `fs` module?
- fs.makeDir(path, { recursive: true })
- fs.mkdir(path, { recursive: true }) (Correct answer)
- fs.createDir(path)
- fs.mkdirSync(path, { deep: true })
Correct answer: fs.mkdir(path, { recursive: true })
`fs.mkdir(path, { recursive: true })` creates the target directory and any necessary parent directories without throwing if they already exist.
Question 5: What does `process.argv[2]` typically contain when you run `node app.js hello`?
- 'node'
- 'app.js'
- 'hello' (Correct answer)
- undefined
Correct answer: 'hello'
`process.argv[0]` is the path to node, `[1]` is the script path, and `[2]` onwards are the user-supplied command-line arguments.
Question 6: Which Node.js built-in module allows you to work with file and directory paths in a cross-platform way?
- url
- os
- path (Correct answer)
- fs
Correct answer: path
The `path` module provides utilities like `path.join()`, `path.resolve()`, and `path.extname()` that handle OS-specific path separators automatically.
Question 7: What value does `require()` return when you require a JSON file in Node.js?
- A raw string of the file contents
- A parsed JavaScript object (Correct answer)
- A Buffer containing the file bytes
- A ReadStream for the file
Correct answer: A parsed JavaScript object
Node.js automatically parses JSON files when they are `require()`d, returning a ready-to-use JavaScript object.
Which Node.js global object provides information about the current process, such as its PID and environment variables?