MCQs on File Handling and I/O | Go

Master file handling and I/O operations in Go with these 30 multiple-choice questions. Explore reading and writing files, working with CSV, JSON, XML, and error handling in file operations.


MCQs on File Handling and I/O in Go

1. Reading and Writing Files

  1. How do you open a file for reading in Go?
    a) file, err := os.Open("file.txt")
    b) file := os.Open("file.txt")
    c) file, err := os.Create("file.txt")
    d) file := os.ReadFile("file.txt")
  2. Which function is used to create a file in Go?
    a) os.Create("file.txt")
    b) os.NewFile("file.txt")
    c) os.Open("file.txt")
    d) os.WriteFile("file.txt")
  3. What is returned by the os.Open function in Go?
    a) A file pointer and an error
    b) A file handler and data
    c) A file name and a byte slice
    d) A boolean indicating success
  4. Which of the following is used to read data from a file in Go?
    a) file.Read()
    b) file.Scan()
    c) file.Write()
    d) file.Open()
  5. How can you write data to a file in Go?
    a) file.Write([]byte("Hello World"))
    b) file.Append([]byte("Hello World"))
    c) file.WriteString("Hello World")
    d) file.WriteText("Hello World")
  6. Which function is used to close a file in Go?
    a) file.Close()
    b) file.End()
    c) file.Finish()
    d) file.Exit()
  7. How do you check if there was an error while opening a file in Go?
    a) if err != nil {}
    b) if file.isErr() {}
    c) if err.exists() {}
    d) if file.hasError() {}
  8. What does the os.ReadFile function do in Go?
    a) It reads the entire content of a file and returns it as a byte slice
    b) It reads the first line of a file
    c) It opens a file in read mode
    d) It reads a file character by character
  9. What will happen if you try to open a file that doesn’t exist in Go?
    a) It will return an error
    b) The file will be automatically created
    c) It will return nil
    d) It will return an empty string
  10. How can you read the contents of a file line by line in Go?
    a) scanner := bufio.NewScanner(file)
    b) file.ReadLine()
    c) file.ReadLines()
    d) scanner := os.NewScanner(file)

2. Working with CSV, JSON, and XML

  1. Which package is used to work with CSV files in Go?
    a) encoding/csv
    b) csv
    c) encoding/json
    d) os/csv
  2. How do you write data to a CSV file in Go?
    a) csv.Writer(file).Write()
    b) csv.Append(file)
    c) csv.Open(file)
    d) csv.WriteFile(file)
  3. Which method reads a CSV file and returns the rows in Go?
    a) csv.NewReader(file).ReadAll()
    b) csv.Open(file)
    c) csv.Read(file)
    d) csv.ReadFile(file)
  4. How can you encode data into JSON format in Go?
    a) json.Marshal(data)
    b) json.Encode(data)
    c) json.Write(data)
    d) json.Convert(data)
  5. Which method is used to decode JSON data into a Go struct?
    a) json.Unmarshal()
    b) json.Decode()
    c) json.Deserialize()
    d) json.Parse()
  6. How do you write JSON data to a file in Go?
    a) json.NewEncoder(file).Encode(data)
    b) json.WriteFile(file, data)
    c) json.MarshalToFile(file, data)
    d) json.Write(file, data)
  7. What does xml.Unmarshal do in Go?
    a) It converts XML data into Go data structures
    b) It encodes Go data structures into XML
    c) It creates an XML document
    d) It writes XML to a file
  8. How do you create an XML file in Go?
    a) xml.NewEncoder(file).Encode(data)
    b) xml.WriteFile(file, data)
    c) xml.Marshal(data)
    d) xml.Write(file, data)
  9. Which package is used for XML manipulation in Go?
    a) encoding/xml
    b) xml
    c) os/xml
    d) xmlparser
  10. What is the result of calling json.Marshal(data) in Go?
    a) It converts Go data structures into JSON
    b) It writes JSON to a file
    c) It reads JSON from a file
    d) It converts JSON into Go data structures
  11. How can you read a CSV file line by line in Go?
    a) reader.Read()
    b) scanner.Scan()
    c) csv.ReadLine()
    d) csv.GetRow()
  12. How do you handle missing or invalid keys when unmarshalling JSON in Go?
    a) By using json.RawMessage
    b) By using the omitempty tag in struct fields
    c) By ignoring the error from json.Unmarshal
    d) Go does not support handling invalid keys
  13. Which method in Go is used to close a CSV file after reading or writing?
    a) file.Close()
    b) csv.Close()
    c) csv.End()
    d) file.End()
  14. How can you pretty-print JSON data in Go?
    a) json.MarshalIndent()
    b) json.Pretty()
    c) json.Print()
    d) json.Indent()
  15. How do you encode an array to JSON format in Go?
    a) json.Marshal(array)
    b) json.Encode(array)
    c) json.ToJSON(array)
    d) json.ConvertToJSON(array)
  16. What does os.OpenFile do in Go?
    a) It opens a file with specific permissions
    b) It opens a file in read-only mode
    c) It opens a file and returns a handle
    d) It opens a file in append mode
  17. How do you read an entire file into a byte slice in Go?
    a) ioutil.ReadFile()
    b) os.ReadFile()
    c) file.ReadAll()
    d) file.Read()
  18. What does encoding/xml.Marshal do in Go?
    a) It converts Go data structures into XML
    b) It encodes XML files into Go structures
    c) It writes XML to a file
    d) It reads XML data
  19. How can you read a JSON file into a Go struct?
    a) json.NewDecoder(file).Decode(&data)
    b) json.Read(file, data)
    c) json.Unmarshal(file, data)
    d) json.Decode(file, data)
  20. Which of the following can be handled by Go’s error handling in file operations?
    a) File not found
    b) Permission denied
    c) Invalid data format
    d) All of the above

Answers Table

QnoAnswer (Option with Text)
1a) file, err := os.Open("file.txt")
2a) os.Create("file.txt")
3a) A file pointer and an error
4a) file.Read()
5a) file.Write([]byte("Hello World"))
6a) file.Close()
7a) if err != nil {}
8a) It reads the entire content of a file and returns it as a byte slice
9a) It will return an error
10a) scanner := bufio.NewScanner(file)
11a) encoding/csv
12a) csv.Writer(file).Write()
13a) csv.NewReader(file).ReadAll()
14a) json.Marshal(data)
15a) json.Unmarshal()
16a) json.NewEncoder(file).Encode(data)
17a) It converts XML data into Go data structures
18a) xml.NewEncoder(file).Encode(data)
19a) encoding/xml
20a) It converts Go data structures into JSON
21a) reader.Read()
22b) By using the omitempty tag in struct fields
23a) file.Close()
24a) json.MarshalIndent()
25a) json.Marshal(array)
26a) It opens a file with specific permissions
27a) ioutil.ReadFile()
28a) It converts Go data structures into XML
29a) json.NewDecoder(file).Decode(&data)
30d) All of the above

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top