0
0
Node.jsframework~20 mins

Checking file existence and stats in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File System Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Node.js code snippet?
Consider the following code that checks if a file exists and prints its size in bytes:
Node.js
import { promises as fs } from 'fs';

async function checkFile() {
  try {
    await fs.access('example.txt');
    const stats = await fs.stat('example.txt');
    console.log(`Size: ${stats.size}`);
  } catch {
    console.log('File does not exist');
  }
}

checkFile();
APrints 'Size: 0' if 'example.txt' is empty, or 'File does not exist' if missing
BAlways prints 'File does not exist' regardless of file presence
CThrows a runtime error if 'example.txt' does not exist
DPrints 'Size: undefined' if the file exists
Attempts:
2 left
💡 Hint
Check what fs.access and fs.stat do and how errors are handled in async/await.
component_behavior
intermediate
1:30remaining
What happens when using fs.existsSync to check a file synchronously?
Given this code snippet:
Node.js
import fs from 'fs';

if (fs.existsSync('data.json')) {
  console.log('File found');
} else {
  console.log('File missing');
}
AAlways prints 'File missing' because existsSync is deprecated
BPrints 'File found' if 'data.json' exists, otherwise 'File missing'
CThrows an error if 'data.json' does not exist
DPrints 'File found' even if the file is missing
Attempts:
2 left
💡 Hint
Check the behavior of fs.existsSync in Node.js.
📝 Syntax
advanced
2:00remaining
Which option correctly uses fs.promises.stat to get file stats?
Choose the code snippet that correctly gets file stats asynchronously and logs the file size:
Aawait fs.stat('file.txt', (err, stats) => { console.log(stats.size); });
Bfs.promises.stat('file.txt', (err, stats) => { console.log(stats.size); });
Cconst stats = fs.promises.stat('file.txt'); console.log(stats.size);
Dconst stats = await fs.promises.stat('file.txt'); console.log(stats.size);
Attempts:
2 left
💡 Hint
Remember that fs.promises methods return promises and do not use callbacks.
🔧 Debug
advanced
2:00remaining
Why does this code throw an error when checking file stats?
Analyze this code snippet:
Node.js
import fs from 'fs';

fs.stat('missing.txt', (err, stats) => {
  if (stats.size > 0) {
    console.log('File has content');
  }
});
AIt throws because the callback is missing the error check for 'err'
BIt throws because fs.stat requires promises, not callbacks
CIt throws because stats is undefined when the file is missing, causing 'stats.size' to fail
DIt throws because 'missing.txt' is a directory, not a file
Attempts:
2 left
💡 Hint
What happens if the file does not exist? What is the value of stats?
🧠 Conceptual
expert
2:30remaining
What is the difference between fs.access and fs.stat when checking file existence?
Select the most accurate statement about fs.access and fs.stat:
Afs.access checks permissions and existence without returning file info; fs.stat returns detailed file info but throws if file missing
Bfs.access returns file size; fs.stat only checks if file exists
Cfs.access throws an error if file exists; fs.stat never throws errors
Dfs.access is synchronous only; fs.stat is asynchronous only
Attempts:
2 left
💡 Hint
Think about what each function is designed to do and their error behavior.