Bird
Raised Fist0
Node.jsframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does process.stdin do in a Node.js program?
easy
A. It manages file system operations.
B. It reads input typed by the user in the terminal.
C. It writes output to the terminal screen.
D. It handles network requests.

Solution

  1. Step 1: Understand the role of process.stdin

    process.stdin is used to read data from the terminal where the user types input.
  2. Step 2: Differentiate from process.stdout

    process.stdout is for output, not input. File system and network are unrelated here.
  3. Final Answer:

    It reads input typed by the user in the terminal. -> Option B
  4. Quick Check:

    Input reading = C [OK]
Hint: Remember: stdin = input, stdout = output [OK]
Common Mistakes:
  • Confusing stdin with stdout
  • Thinking stdin writes output
  • Mixing input with file or network operations
2. Which of the following is the correct way to write 'Hello World' to the terminal using process.stdout?
easy
A. console.read('Hello World');
B. process.stdin.write('Hello World');
C. process.stdout.write('Hello World\n');
D. process.stdout.read('Hello World');

Solution

  1. Step 1: Identify correct method for output

    process.stdout.write() is the method to write output to the terminal.
  2. Step 2: Check syntax and usage

    process.stdin is for input, not output. console.read and process.stdout.read are invalid.
  3. Final Answer:

    process.stdout.write('Hello World\n'); -> Option C
  4. Quick Check:

    Output uses stdout.write = A [OK]
Hint: Use stdout.write() to print text, not stdin [OK]
Common Mistakes:
  • Using stdin to write output
  • Using non-existent console.read method
  • Confusing read and write methods
3. What will the following Node.js code output if the user types 'NodeJS' and presses Enter?
process.stdin.on('data', (data) => {
  process.stdout.write('You typed: ' + data);
});
medium
A. You typed: NodeJS\r\n
B. You typed: NodeJS
C. You typed: NodeJS\n
D. SyntaxError

Solution

  1. Step 1: Understand data event input

    The data event receives a Buffer including the newline characters from pressing Enter, usually \r\n.
  2. Step 2: Output includes raw input

    Concatenating data directly includes the newline characters, so output ends with NodeJS\r\n.
  3. Final Answer:

    You typed: NodeJS\r\n -> Option A
  4. Quick Check:

    Input includes newline chars = B [OK]
Hint: Data event includes Enter key chars like \r\n [OK]
Common Mistakes:
  • Assuming input has no newline characters
  • Expecting trimmed input automatically
  • Confusing syntax errors with output format
4. Identify the error in this code snippet that tries to read user input and print it:
process.stdin.on('data', function(input) {
  console.log(input.toString);
});
medium
A. process.stdin cannot be used with on() method.
B. Using console.log instead of process.stdout.write.
C. Incorrect event name; should be 'input' not 'data'.
D. Missing parentheses after toString method call.

Solution

  1. Step 1: Check method usage on input

    input.toString is a method and needs parentheses to call it: input.toString().
  2. Step 2: Verify event and method correctness

    data event is correct for reading input. Using console.log is valid for output.
  3. Final Answer:

    Missing parentheses after toString method call. -> Option D
  4. Quick Check:

    Method calls need () = D [OK]
Hint: Call methods with () to avoid undefined output [OK]
Common Mistakes:
  • Forgetting parentheses on toString
  • Thinking event name is wrong
  • Believing console.log can't print input
5. You want to create a Node.js program that asks the user for their name, then prints 'Hello, [name]!' and exits. Which code snippet correctly uses process.stdin and process.stdout to achieve this?
hard
A. process.stdout.write('Enter your name: '); process.stdin.on('data', data => { process.stdout.write(`Hello, ${data.toString().trim()}!\n`); process.exit(); });
B. process.stdin.write('Enter your name: '); process.stdout.on('data', data => { process.stdout.write(`Hello, ${data}!\n`); });
C. console.log('Enter your name: '); process.stdin.on('input', data => { console.log('Hello, ' + data); });
D. process.stdout.write('Enter your name: '); process.stdin.on('data', data => { console.log('Hello, ' + data); });

Solution

  1. Step 1: Prompt user correctly

    process.stdout.write is used to show the prompt without newline.
  2. Step 2: Read input and trim newline

    data.toString().trim() converts input buffer to string and removes newline characters.
  3. Step 3: Output greeting and exit

    Print greeting with template string and call process.exit() to end program.
  4. Final Answer:

    Code snippet A correctly prompts, reads, trims, outputs, and exits. -> Option A
  5. Quick Check:

    Prompt + trim input + exit = A [OK]
Hint: Trim input and call process.exit() after output [OK]
Common Mistakes:
  • Using stdin.write instead of stdout.write for prompt
  • Not trimming input causing newline in output
  • Missing process.exit causing program to hang