Node.js Streams and Buffers Fundamentals Quiz Quiz

Explore key concepts of streams and buffers in Node.js with this easy quiz. Assess your understanding of readable and writable streams, buffer manipulation, and typical scenarios encountered in Node.js data handling.

  1. Identifying Stream Types

    Which type of stream in Node.js allows you to read data piece by piece, such as when reading a file line by line?

    1. Writable stream
    2. Readable stream
    3. Duplex buffer
    4. Full buffer

    Explanation: A readable stream is used to read data chunk by chunk from a source, such as a file or network connection. Writable streams are for writing data, not reading. 'Duplex buffer' is not a correct term; the proper concept is a duplex stream, which allows both read and write. 'Full buffer' is unrelated; buffers are storage areas, not streams.

  2. Basic Buffer Creation

    How do you create a buffer from a string of text in Node.js, for example, 'hello'?

    1. Buffer.from('hello')
    2. Buffer.generate('hello')
    3. Buffer.write('hello')
    4. Buffer.make('hello')

    Explanation: The Buffer.from() method creates a buffer from a string such as 'hello'. Buffer.write() is used to write data into an existing buffer, not to create one. Buffer.generate() and Buffer.make() are not valid methods in this context and would result in errors.

  3. Purpose of Buffers

    Why are buffers commonly used in Node.js, especially when working with streams?

    1. They increase application speed automatically
    2. They store binary data temporarily
    3. They convert binary data to images
    4. They encrypt all data by default

    Explanation: Buffers provide a way to handle and store binary data temporarily, which is especially useful when data arrives in chunks. They do not automatically speed up applications, encrypt data, or convert binaries to images. The other options either misunderstand the primary function of buffers or refer to additional operations.

  4. Writable Stream Usage

    Which method do you use to send data to a writable stream, for instance to write 'world'?

    1. read('world')
    2. create('world')
    3. write('world')
    4. input('world')

    Explanation: The write() method is called on writable streams to send data such as 'world'. The read() method is intended for readable streams, not writable ones. Neither input() nor create() are valid methods for writing data to streams.

  5. Stream Event Handling

    What event should you listen for on a readable stream to handle incoming data chunks?

    1. push
    2. send
    3. receive
    4. data

    Explanation: The 'data' event is emitted every time a chunk of data is available to be consumed from a readable stream. 'send' and 'receive' are not valid events on streams, while 'push' is not used as an event in this context. Choosing other options could result in missing incoming data.

  6. End of Stream

    Which event signals that there is no more data to be read from a readable stream?

    1. close
    2. finish
    3. stop
    4. end

    Explanation: The 'end' event indicates that all data has been read from a readable stream. 'close' marks when a stream is closed, which may not always mean all data was read. 'finish' is for writable streams, and 'stop' is not an event in the standard stream interface.

  7. Buffer Length Check

    How do you check the number of bytes contained in a Node.js buffer named 'buf'?

    1. buf.length
    2. buf.size
    3. size(buf)
    4. length(buf)

    Explanation: The length property of a buffer, buf.length, returns the number of bytes stored. buf.size is not a standard property, and neither length(buf) nor size(buf) represent valid syntax for buffers. It is important to use the correct property to avoid errors.

  8. Duplex Streams

    What is a 'duplex stream' in Node.js, and how does it function differently from other streams?

    1. It only writes data
    2. It buffers data, but cannot stream
    3. It only reads data
    4. It is both readable and writable

    Explanation: A duplex stream in Node.js can read and write data, unlike standard readable or writable streams that only perform one of these functions. It does not exclusively read or write, and it doesn't only buffer data or lack streaming capabilities. The other choices misrepresent the role of duplex streams.

  9. Buffer Encoding

    When converting a buffer to a string, what parameter do you optionally provide to specify the encoding, for example 'utf8'?

    1. 'binarystr'
    2. 'utf8'
    3. 'encrypt'
    4. 'textcode'

    Explanation: Passing 'utf8' as an encoding parameter in methods like toString ensures correct interpretation of buffer contents as text. 'binarystr', 'encrypt', and 'textcode' are not valid encodings in this context and would cause errors or unexpected behaviors.

  10. Pipe Method

    If you want to direct the output of a readable stream into a writable stream, which built-in method should you use?

    1. link
    2. route
    3. pipe
    4. connect

    Explanation: The pipe method allows data to flow from a readable stream directly into a writable stream, facilitating efficient data transfer. 'connect', 'route', and 'link' are not valid methods for this operation. Using anything other than pipe would not achieve the desired direct piping of data.