0
0
Node.jsframework~20 mins

process.stdin and process.stdout in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Stream Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Node.js code output when you type 'hello' and press Enter?
Consider this code snippet that reads from standard input and writes to standard output:
Node.js
process.stdin.on('data', (data) => {
  process.stdout.write(`You typed: ${data}`);
});
process.stdin.resume();
A
You typed: hello
Bolleh :depyt uoY
Chello
DError: Cannot read data
Attempts:
2 left
💡 Hint
Remember that the data event includes the newline character when you press Enter.
📝 Syntax
intermediate
1:30remaining
Which option correctly sets up process.stdin to read input as UTF-8 strings?
You want to read user input as strings, not buffers. Which code snippet does this correctly?
Aprocess.stdin.encoding('utf8');
Bprocess.stdin.encoding = 'utf8';
Cprocess.stdin.setEncoding = 'utf8';
Dprocess.stdin.setEncoding('utf8');
Attempts:
2 left
💡 Hint
Look for the method that sets encoding properly.
🔧 Debug
advanced
2:00remaining
Why does this code never print anything after input?
Look at this code snippet:
Node.js
process.stdin.on('data', (chunk) => {
  console.log(chunk.toString());
});

// No other code
ABecause chunk is empty string always
BBecause console.log is asynchronous and buffers output
CBecause process.stdin is paused by default and never resumed
DBecause 'data' event never fires on process.stdin
Attempts:
2 left
💡 Hint
Check if the stream is flowing or paused by default.
state_output
advanced
1:30remaining
What is the output of this code after typing 'abc' and pressing Enter?
This code reads input and writes it back in uppercase:
Node.js
process.stdin.setEncoding('utf8');
process.stdin.on('data', (data) => {
  process.stdout.write(data.toUpperCase());
});
process.stdin.resume();
A
ABC
Bcba
CBC
Dabc
Attempts:
2 left
💡 Hint
Remember the input includes the newline character.
🧠 Conceptual
expert
2:30remaining
What happens if you write to process.stdout without a newline and then read from process.stdin?
Consider this code snippet:
Node.js
process.stdout.write('Enter your name: ');
process.stdin.setEncoding('utf8');
process.stdin.on('data', (data) => {
  process.stdout.write(`Hello, ${data}`);
  process.exit();
});
process.stdin.resume();
AThe prompt appears with a newline, waits for input, then greets without newline
BThe prompt appears without a newline, waits for input, then greets with input including newline
CThe prompt does not appear until after input is entered
DThe program crashes because process.exit() is called inside the data event
Attempts:
2 left
💡 Hint
process.stdout.write does not add a newline automatically.