yoklainterview sim

Backend Nodejs Modules Streams Interview Questions

75 verified Backend Nodejs Modules Streams interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Nodejs Modules StreamsDifficulty 1
// math.js
module.exports = function add(a, b) {
  return a + b;
};

// main.js
const add = require('./math');
console.log(add(2, 3));

What does main.js print when it runs?
  • a5
  • b23
  • cundefined
  • dTypeError: add is not a function
Explanation:module.exports is set to the add function itself, so require('./math') returns that function directly. Calling add(2, 3) computes 2 + 3, which is 5.
Nodejs Modules StreamsDifficulty 2
// counter.js
let count = 0;
module.exports = {
  increment() {
    count++;
    return count;
  }
};

// main.js
const counterA = require('./counter');
const counterB = require('./counter');
console.log(counterA.increment());
console.log(counterB.increment());

What does this code print?
  • a1, then 1
  • b0, then 1
  • cundefined, then undefined
  • d1, then 2
Explanation:Node caches modules by resolved file path, so counterA and counterB reference the exact same object returned by require('./counter'). The shared count variable persists between calls, so the first increment() returns 1 and the second returns 2.
Nodejs Modules StreamsDifficulty 2
const EventEmitter = require('events');
const emitter = new EventEmitter();

emitter.on('greet', () => console.log('A'));
emitter.emit('greet');
console.log('B');

What is printed, in order?
  • aB, then A
  • bA, then B
  • cOnly B
  • dOnly A
Explanation:emit() calls all registered listeners synchronously before returning. So the listener logging 'A' runs and finishes before emit() returns control to the next line, which logs 'B'.
Nodejs Modules StreamsDifficulty 2
const EventEmitter = require('events');
const emitter = new EventEmitter();

emitter.on('tick', () => console.log('first'));
emitter.on('tick', () => console.log('second'));
emitter.emit('tick');

What is printed?
  • asecond, then first
  • bonly second, because the last listener overrides earlier ones
  • cfirst, then second
  • dthe order is not guaranteed and varies between runs
Explanation:EventEmitter calls listeners for the same event in the order they were registered with .on(). Registering a second listener does not overwrite the first one.
Nodejs Modules StreamsDifficulty 2
In Node.js streams, what happens as soon as you attach a 'data' event listener to a Readable stream?
  • aThe stream switches to flowing mode and starts emitting chunks automatically.
  • bThe stream pauses permanently until .resume() is called explicitly.
  • cNothing changes until .pipe() is also called.
  • dThe stream reads the entire source into memory before emitting any data.
Explanation:Attaching a 'data' listener is one of the ways to switch a Readable stream into flowing mode, in which chunks are pushed to listeners as soon as they're available, without needing to call .read() manually.
Nodejs Modules StreamsDifficulty 1
const { Writable } = require('stream');

const writable = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  }
});

writable.write('hello');
writable.end();

What is printed?
  • aundefined
  • b[object Object]
  • chello
  • dNothing — write() requires a Buffer, not a string
Explanation:The custom write() implementation converts the chunk to a string with .toString() and logs it. Writable streams accept string chunks and convert them to Buffers internally by default, so 'hello' is printed.

Test yourself against the 3300-question Backend bank.

Start interview