0
0
Node.jsframework~20 mins

Appending to files in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Append Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Appending text to a file with fs.appendFile
What will be the content of the file 'log.txt' after running this code if the file initially contains 'Hello'? Assume no errors occur.
Node.js
import { appendFileSync, readFileSync } from 'node:fs';

appendFileSync('log.txt', ' World');
const content = readFileSync('log.txt', 'utf8');
console.log(content);
A"Hello World"
B" World"
C"Hello"
D"Hello\n World"
Attempts:
2 left
💡 Hint
Appending adds new content to the end of the existing file content.
Predict Output
intermediate
2:00remaining
Asynchronous file append behavior
What will be logged to the console after running this code snippet?
Node.js
import { appendFile } from 'node:fs';

appendFile('notes.txt', 'New note\n', (err) => {
  if (err) throw err;
  console.log('Note added');
});
console.log('Appending started');
AAppending started\nNote added
BNote added\nAppending started
CAppending started
DNote added
Attempts:
2 left
💡 Hint
Asynchronous functions run their callback after the main thread continues.
🔧 Debug
advanced
2:00remaining
Identifying error in appending to a file
This code is intended to append 'Data' to 'file.txt'. What error will it raise when run?
Node.js
import fs from 'node:fs';

fs.appendFile('file.txt', 'Data', (error) => {
  if (error) throw error;
});

fs.appendFileSync('file.txt', 'More data')
AError: appendFileSync called without encoding
BError: Callback must be a function
CTypeError: fs.appendFile is not a function
DNo error, both append operations succeed
Attempts:
2 left
💡 Hint
Check the usage of appendFile and appendFileSync methods.
component_behavior
advanced
2:00remaining
Effect of encoding on appending files
What will be the content of 'data.txt' after running this code if the file initially contains 'abc'?
Node.js
import { appendFileSync, readFileSync } from 'node:fs';

appendFileSync('data.txt', Buffer.from([0x20, 0x64, 0x65, 0x66]));
const content = readFileSync('data.txt', 'utf8');
console.log(content);
A"abcdef"
B"abc\u0000def"
C"abc def"
D"abc def"
Attempts:
2 left
💡 Hint
Buffer bytes correspond to ASCII characters; 0x20 is a space.
📝 Syntax
expert
3:00remaining
Correct syntax for appending JSON data to a file
Which option correctly appends a JSON string to 'output.json' without overwriting existing content?
A
import { appendFile } from 'node:fs';
appendFile('output.json', JSON.stringify({a:1}), () => {});
B
import { appendFileSync } from 'node:fs';
appendFileSync('output.json', JSON.stringify({a:1}) + '\n');
C
import { writeFileSync } from 'node:fs';
writeFileSync('output.json', JSON.stringify({a:1}));
D
import { appendFileSync } from 'node:fs';
appendFileSync('output.json', JSON.stringify({a:1}));
appendFileSync('output.json', '\n');
Attempts:
2 left
💡 Hint
Appending JSON data as a string with a newline keeps entries separated.