Sample questions
Question 1Easy
Why use the path module instead of building paths with string concatenation?
- It makes file reads faster
- It checks that files exist
- It is required for using fs
- It handles separators and edge cases across platforms
Answer: D. It handles separators and edge cases across platforms
Explanation
path uses the right separator for the OS and normalises ".." and duplicate slashes, avoiding subtle bugs.
Step-by-step solution
- Path module handles different separators like / and \ automatically.
- It removes extra slashes and resolves .. to parent directories correctly.
- String concatenation can lead to incorrect paths on different operating systems.
- The path module ensures consistent behavior across platforms.
- This avoids bugs caused by platform-specific path formatting.
- Final answer: It handles separators and edge cases across platforms.
Why the other options are wrong
- A. path functions only join and split strings; they do not change how fast a file is read.
- B. path does not check whether a file exists; it works only on the strings given to it.
- C. fs does not need the path module, so path is not required to use fs.
Tip: Use the path module for cross-platform safety and consistency.
Question 2Easy
Which error code does Node report when you try to read a file that does not exist?
- ENOENT
- EACCES
- EEXIST
- EISDIR
Answer: A. ENOENT
Explanation
ENOENT means "no such file or directory". EACCES is a permission error and EEXIST means the file already exists.
Step-by-step solution
- Node throws an error when a file is not found.
- The error code for 'no such file or directory' is ENOENT.
- ENOENT is the correct error code for missing files.
- Other options like EACCES and EEXIST refer to different issues.
- Thus, the answer is ENOENT.
Why the other options are wrong
- B. EACCES means permission was denied, not that the file is missing.
- C. EEXIST means the path already exists, which is the opposite of a missing file.
- D. EISDIR means the path is a directory, when a file was expected; the file is not missing here.
Tip: Remember: ENOENT = No such file or directory.
Question 3Easy
You want each call to add a line to log.txt without erasing earlier lines. Which call fits?
- fs.appendFile
- fs.writeFile
- fs.truncate
- fs.copyFile
Answer: A. fs.appendFile
Explanation
writeFile replaces the contents by default (flag "w"); appendFile uses flag "a" and adds to the end.
Step-by-step solution
- fs.appendFile adds data to the end of a file without erasing it.
- fs.writeFile replaces the file content by default with the
w flag. - The question asks for adding lines without erasing earlier ones.
- So, fs.appendFile is the correct method for appending logs.
- Final answer: fs.appendFile.
Why the other options are wrong
- B. writeFile replaces the whole file with the new data, so earlier lines are erased.
- C. truncate cuts a file down to a set length, so it removes content instead of adding a line.
- D. copyFile copies one file to another; it does not add a line to log.txt.
Tip: For logging, always use append mode to keep previous entries.
Question 4Easy
What does this print on Linux or macOS?
const path = require('path');
console.log(path.join('a', 'b', '../c'));
- a/b/c
- a/c
- a/b/../c
- /a/c
Answer: B. a/c
Explanation
path.join concatenates the segments and normalises the result, so "b/.." cancels out.
Step-by-step solution
- path.join joins the parts with a slash: 'a/b/../c'.
- It then normalises the path, so '..' removes 'b'.
- The result is 'a/c'. The correct answer is B (a/c).
Why the other options are wrong
- A. The '..' segment removes b, so b is not kept.
- C. Join always normalises, so '..' is not left in the output.
- D. Join keeps the path relative because the first part has no leading slash.
Tip: path.join normalises '..' and '.' segments; the result keeps the start as relative unless the first part starts with a slash.
Question 5Medium
What is the output of this script?
const fs = require('fs');
console.log('start');
fs.readFileSync(__filename);
fs.readFile(__filename, () => console.log('async read'));
console.log('end');
- start async read end
- start end async read
- async read start end
- start end
Answer: B. start end async read
Explanation
readFileSync completes inline, but the readFile callback runs only after the synchronous code ends and the I/O finishes.
Step-by-step solution
- 'start' is printed first.
- ReadFileSync blocks until the file is read, and nothing is printed for it.
- ReadFile starts in the background, and its callback waits for the event loop.
- 'end' is printed before the callback runs, so the order is start, end, async read. The correct answer is B (start end async read).
Why the other options are wrong
- A. The callback of the async read runs after the synchronous code, so it is not before end.
- C. Synchronous code runs first, so 'start' cannot come after 'async read'.
- D. The async callback always runs later, so it is printed.
Tip: All synchronous code runs first. Callbacks from fs or timers run after it, in the order they finish.
Question 6Medium
What does this print on Linux or macOS?
const path = require('path');
console.log(path.normalize('/a//b/./c/..'));
- /a/b/c
- /a//b/c
- /a/b/.
- /a/b
Answer: D. /a/b
Explanation
normalize collapses double slashes, removes "." and resolves "c/.." away.
Step-by-step solution
- Normalize removes empty segments, so '//' becomes '/'.
- It removes '.' segments and resolves '..' by removing the previous segment.
- '/a/b/./c/..' becomes '/a/b'. The correct answer is D (/a/b).
Why the other options are wrong
- A. The c segment is removed by the '..', so c is not kept.
- B. Double slashes are collapsed by normalize.
- C. The '.' and '..' are resolved, so they do not remain in the output.
Tip: Normalize cleans a path: it removes doubled slashes, dots and resolvable '..' parts.
Question 7Medium
What is the key difference between path.join() and path.resolve()?
- join checks that the path exists on disk
- resolve does not normalise ".." segments, while join always does
- resolve always returns an absolute path, using the cwd
- join always returns an absolute path
Answer: C. resolve always returns an absolute path, using the cwd
Explanation
resolve prepends process.cwd() if no argument is absolute. join only joins and normalises, so it can return a relative path.
Step-by-step solution
- path.join joins the parts and normalises them, but does not look at the cwd.
- path.resolve works from right to left and adds the current directory if no absolute path is found.
- So resolve always gives an absolute path. The correct answer is C (resolve always returns an absolute path, using the cwd).
Why the other options are wrong
- A. Neither function checks the disk; both only work on strings.
- B. Both functions normalise '..' segments; the claim that join does not is false.
- D. Join can return a relative path, for example join('a', 'b') gives 'a/b'.
Tip: If you need an absolute path from a relative one, use path.resolve. join only glues and cleans text.
Question 8Medium
Directory "a" does not exist. What does fs.mkdirSync('a/b/c') do without any options?
- Throws ENOENT because the parent is missing
- Creates a, a/b and a/b/c
- Creates only c in the current directory
- Returns false and creates nothing
Answer: A. Throws ENOENT because the parent is missing
Explanation
Without { recursive: true }, mkdir creates only the last segment and fails if a parent is missing.
Step-by-step solution
- fs.mkdirSync('a/b/c') tries to create directory 'a/b/c'.
- It does not automatically create parent directories like 'a' or 'a/b'.
- Since 'a' does not exist, it throws an error ENOENT.
- ENOENT means the file or directory does not exist.
- The correct answer is A (Throws ENOENT because the parent is missing).
Why the other options are wrong
- B. mkdirSync does not create missing parents unless the recursive option is given.
- C. The call uses the path a/b/c, not c in the current directory, so it does not create c there.
- D. It does not return false; mkdirSync throws an error when it fails, such as a missing parent.
Tip: Without recursive: true, mkdirSync fails if any parent directories are missing.
Question 9Hard
What does this print on Linux or macOS?
const path = require('path');
console.log(path.join('/a', '/b'), path.resolve('/a', '/b'));
- /a/b /a/b
- /a/b /b
- /b /b
- /b /a/b
Answer: B. /a/b /b
Explanation
join just glues segments together. resolve works from right to left and stops at the first absolute path, so "/b" wins.
Step-by-step solution
- path.join('/a', '/b') joins the parts and gives '/a/b'.
- path.resolve('/a', '/b') works from right to left and stops at the last absolute path, '/b'.
- The output is '/a/b /b'. The correct answer is B (/a/b /b).
Why the other options are wrong
- A. Resolve stops at the last absolute path, so it gives /b, not /a/b.
- C. Join keeps the /a part, so the first result is /a/b.
- D. The two results are swapped; join gives /a/b and resolve gives /b.
Tip: Resolve treats an absolute segment as a reset, so only the segments after the last absolute one count. join just glues the parts.
Question 10Hard
Code checks fs.existsSync(p) and then opens p. Why is this pattern discouraged?
existsSync always returns false for directoriesexistsSync loads the whole file into memory- The file can change between check and open (a race)
open() refuses paths already checked with existsSync
Answer: C. The file can change between check and open (a race)
Explanation
This is a time-of-check to time-of-use race. It is safer to open the file directly and handle an ENOENT error.
Step-by-step solution
- ExistsSync checks if file exists before opening it.
- The file might be deleted or modified between check and open.
- This creates a race condition where the file state changes unexpectedly.
- Opening the file directly avoids this race condition safely.
- Handling ENOENT error is better than relying on existsSync.
- Final answer: The file can change between check and open (a race).
Why the other options are wrong
- A. existsSync returns true for directories too, so directories are not what makes the check wrong.
- B. existsSync only checks that a path exists; it does not read or load the file's contents.
- D. open() does not refuse paths that were checked; it simply opens whatever the path points to.
Tip: Always open files directly and handle errors instead of checking existence first.
Practise all 16 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →