Node.js Buffer Quiz Quiz

Challenge your understanding of how Node.js Buffer works with these easy-to-understand questions on basic concepts and examples.

  1. Buffer Constructor Usage

    Which method creates a new Buffer instance in Node.js with a specific size of 10 bytes?

    1. Buffer.make(10)
    2. Buffer.size(10)
    3. Buffer.create(10)
    4. Buffer.alloc(10)

    Explanation: Buffer.alloc(10) creates a new buffer of 10 bytes. Buffer.make(10), Buffer.create(10), and Buffer.size(10) are not valid Buffer class methods in Node.js, so they would cause errors.

  2. Buffer Representation

    If you log a Buffer to the console, which data format is it typically displayed in by default?

    1. Hexadecimal
    2. Binary
    3. Base64
    4. Decimal

    Explanation: Node.js usually displays Buffer contents in hexadecimal format for readability. Decimal and binary are not the default formats, and base64 would require conversion.

  3. Buffer to String Conversion

    Which method is used to convert buffer data to a string in Node.js?

    1. toString()
    2. toChar()
    3. stringify()
    4. convert()

    Explanation: The toString() method is used to convert Buffer content to a string. stringify() and convert() are not Buffer methods, and toChar() does not exist in Buffer.

  4. Creating a Buffer From String

    How do you create a Buffer from the string 'Hello'?

    1. Buffer.from('Hello')
    2. Buffer.write('Hello')
    3. Buffer.input('Hello')
    4. Buffer.alloc('Hello')

    Explanation: Buffer.from('Hello') creates a buffer containing the UTF-8 bytes for 'Hello'. Buffer.alloc expects a size, Buffer.write is used to write into existing buffers, and Buffer.input does not exist.

  5. Checking Buffer Type

    Which method checks if a variable is a Buffer?

    1. Buffer.check(variable)
    2. Buffer.isType(variable)
    3. Buffer.isBuffer(variable)
    4. Buffer.exists(variable)

    Explanation: Buffer.isBuffer(variable) returns true if the variable is a Buffer. The other method names are invalid and do not exist in the Buffer module.

  6. Default Encoding

    What is the default character encoding used by Buffer when converting to or from a string?

    1. utf8
    2. base64
    3. hex
    4. ascii

    Explanation: utf8 is the default encoding for Buffer in Node.js. ascii, base64, and hex are supported but not the default encoding.

  7. Buffer Length

    Which property returns the number of bytes in a Buffer object?

    1. length
    2. bytes
    3. size
    4. count

    Explanation: The length property gives the size of the Buffer in bytes. size, bytes, and count are not valid Buffer properties.

  8. Writing Data to Buffer

    Which method writes a string into a Buffer?

    1. put()
    2. insert()
    3. append()
    4. write()

    Explanation: The write() method is used to store a string into a Buffer. insert(), append(), and put() are not Buffer methods in Node.js.

  9. Buffer Indexing

    How would you access the first byte in a Buffer stored as variable buf?

    1. buf.first()
    2. buf[0]
    3. buf.get(0)
    4. buf.byteAt(0)

    Explanation: Buffer elements can be accessed using zero-based array indexing, like buf[0]. The other options are not valid ways to access Buffer bytes.

  10. Buffer Copying

    Which method copies data from one Buffer to another?

    1. clone()
    2. move()
    3. duplicate()
    4. copy()

    Explanation: The copy() method copies data from one Buffer to another. move(), clone(), and duplicate() are not Buffer methods in Node.js.