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.
Which type of stream in Node.js allows you to read data piece by piece, such as when reading a file line by line?
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.
How do you create a buffer from a string of text in Node.js, for example, '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.
Why are buffers commonly used in Node.js, especially when working with streams?
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.
Which method do you use to send data to a writable stream, for instance to write '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.
What event should you listen for on a readable stream to handle incoming data chunks?
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.
Which event signals that there is no more data to be read from a readable stream?
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.
How do you check the number of bytes contained in a Node.js buffer named '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.
What is a 'duplex stream' in Node.js, and how does it function differently from other streams?
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.
When converting a buffer to a string, what parameter do you optionally provide to specify the encoding, for example 'utf8'?
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.
If you want to direct the output of a readable stream into a writable stream, which built-in method should you use?
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.