0
0
Node.jsframework~20 mins

path.resolve for absolute paths in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Path Resolver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of path.resolve with relative and absolute paths?
Consider the following Node.js code using path.resolve. What will be printed?
Node.js
import path from 'path';
console.log(path.resolve('/foo', './bar', 'baz'));
A"/foo/./bar/baz"
B"/foo/bar/baz"
C"/foo/bar/./baz"
D"foo/bar/baz"
Attempts:
2 left
💡 Hint
Remember that path.resolve normalizes and joins paths, resolving '.' and '..' segments.
Predict Output
intermediate
2:00remaining
What does path.resolve return when given an absolute path last?
What will this code output?
Node.js
import path from 'path';
console.log(path.resolve('foo', '/bar', 'baz'));
A"foo//bar/baz"
B"foo/bar/baz"
C"/foo/bar/baz"
D"/bar/baz"
Attempts:
2 left
💡 Hint
When an absolute path appears, path.resolve resets the base to that path.
🔧 Debug
advanced
2:30remaining
Why does this path.resolve call produce an unexpected path?
This code is intended to resolve to '/home/user/project/src/lib', but it outputs '/home/user/project/lib'. What is the cause?
Node.js
import path from 'path';
const base = '/home/user/project/src';
const result = path.resolve(base, '../lib');
console.log(result);
ABecause '../lib' moves one directory up from 'src', so the result is '/home/user/project/lib'.
BBecause path.resolve does not normalize '..' segments, so it keeps '../lib' literally.
CBecause base should be relative, not absolute, for path.resolve to work correctly.
DBecause path.resolve always appends paths without resolving '..' segments.
Attempts:
2 left
💡 Hint
Think about what '../lib' means relative to the base path.
component_behavior
advanced
2:00remaining
How does path.resolve behave with empty strings in arguments?
What will this code output?
Node.js
import path from 'path';
console.log(path.resolve('', '/foo', '', 'bar'));
A"/foo/bar"
B"foo/bar"
C"/foo//bar"
D"/foo"
Attempts:
2 left
💡 Hint
Empty strings are ignored in path.resolve arguments.
📝 Syntax
expert
2:30remaining
Which option causes a SyntaxError when importing and using path.resolve in Node.js ES modules?
Identify the code snippet that will cause a SyntaxError.
Aimport path from 'path'; console.log(path.resolve('/a', 'b'));
Bconst path = require('path'); console.log(path.resolve('/a', 'b'));
Cimport path from 'path'; console.log(path.resolve('/a', 'b')
Dimport { resolve } from 'path'; console.log(resolve('/a', 'b'));
Attempts:
2 left
💡 Hint
Look for missing or extra characters that break syntax.