Go Standard Library Essentials Quiz Quiz

Explore essential packages, functions, and usage patterns from the Go standard library. This quiz helps reinforce core Go concepts, covering input/output, string processing, error management, and data structures – key for programmers aiming to master foundational Go skills.

  1. String Containment

    Which package provides the Contains function to check if a substring is present within a string?

    1. io
    2. strings
    3. strconv
    4. fmt

    Explanation: The strings package offers the Contains function, which returns true if the substring exists within the main string. The strconv package is intended for type conversions between strings and other basic data types. The io package is used for input and output operations, not string manipulation. The fmt package is mainly for formatted I/O, not for searching within strings.

  2. Input from Standard Input

    Which function from the bufio package reads a line of text from standard input in Go?

    1. ScanLine
    2. ReadAll
    3. InputLine
    4. ReadString

    Explanation: ReadString in the bufio package can be used with an appropriate delimiter, such as a newline, to read a line of text. ScanLine is not an actual function in bufio—Scan is used within bufio.Scanner. ReadAll is found in io/ioutil and reads entire data streams, not a single line. InputLine does not exist in the standard library.

  3. Formatting Output

    Which function would you use to print formatted text with a new line at the end to the standard output?

    1. os.Output
    2. fmt.Write
    3. output.Print
    4. fmt.Println

    Explanation: fmt.Println displays arguments followed by a newline, making it ideal for printing lines of text. output.Print and os.Output are not real functions in Go’s standard library. fmt.Write does not exist; the equivalent function for writing is fmt.Fprintf or similar.

  4. Error Creation

    How do you create a new error with a custom string message in Go using the standard library?

    1. errors.Create
    2. fmt.NewError
    3. errors.New
    4. error.Make

    Explanation: The errors.New function creates a basic error type with a given message. fmt.NewError and error.Make are not functions provided in Go. errors.Create also does not exist; you should use errors.New for this purpose.

  5. JSON Encoding

    Which package is used to encode and decode JSON data in Go?

    1. jsonlib
    2. net/json
    3. encoding/json
    4. serialization

    Explanation: The encoding/json package provides marshaling and unmarshaling functions for JSON. net/json and jsonlib are not standard packages. serialization is a general term and not a package in Go.

  6. Time Formatting

    To format dates and times in Go, which package provides the Format method?

    1. datetime
    2. time
    3. calendar
    4. clock

    Explanation: The time package offers the Format method for formatting time.Time values according to layout patterns. datetime, clock, and calendar are not recognized Go standard library packages. Only time contains the relevant formatting functionality.

  7. Random Integer Generation

    Which standard package is commonly used to generate random integers in Go?

    1. random
    2. integers
    3. randomizer
    4. math/rand

    Explanation: math/rand is the standard package for generating pseudo-random numbers, including whole numbers with its Intn and similar functions. randomizer and integers are not standard packages, while random is not available under that name in Go.

  8. Reading a File Entirely

    Which function from the os or io/ioutil package reads an entire file into memory as a byte slice?

    1. os.GetFile
    2. file.ReadBytes
    3. ioutil.ReadFile
    4. os.ReadData

    Explanation: ioutil.ReadFile reads an entire file and returns its contents as a slice of bytes. os.ReadData, file.ReadBytes, and os.GetFile are not standard functions provided for file reading in Go. ioutil.ReadFile is commonly used for this task.

  9. Sorting a Slice of Integers

    Which function sorts a slice of integers in ascending order in the standard library?

    1. sort.Ints
    2. slice.Sort
    3. ints.Sort
    4. array.Sort

    Explanation: The sort.Ints function is designed to sort slices of integers in ascending order. slice.Sort and ints.Sort are not valid functions in Go. array.Sort does not exist either; the correct approach uses sort.Ints.

  10. Getting the Current Working Directory

    Which function can you use to obtain the current working directory of your Go program?

    1. os.CurrentDir
    2. os.Getwd
    3. os.GetDir
    4. os.WorkDir

    Explanation: os.Getwd returns a string with the current working directory and an error value if something goes wrong. os.WorkDir, os.CurrentDir, and os.GetDir are not actual functions in the Go standard library. Only os.Getwd retrieves the working directory path.