MCQs on Syntax and Basics | Go

Go is a powerful and efficient language, known for its simplicity and speed. In this guide, we’ll dive into its fundamental syntax, covering the program structure, variables, constants, data types, and basic I/O operations.


Chapter 2: Syntax and Basics – MCQs

1. Go Program Structure

  1. What is the first statement in a Go program?
    • a) import
    • b) func main()
    • c) package main
    • d) package
  2. What is the purpose of the main function in Go?
    • a) It defines the entry point for the program
    • b) It handles errors in the program
    • c) It imports necessary packages
    • d) It defines the variables for the program
  3. Which of the following is required in every Go program?
    • a) func
    • b) package main
    • c) import
    • d) main function
  4. How are Go packages imported?
    • a) import "packageName"
    • b) import("packageName")
    • c) import packageName;
    • d) include "packageName"
  5. What is the significance of the package main declaration in Go?
    • a) It indicates that the program is part of a library
    • b) It indicates that the program is the entry point
    • c) It is used to include third-party packages
    • d) It is used to define the variables
  6. Where must the main function be located in a Go program?
    • a) At the beginning of the program
    • b) After the imports section
    • c) At the end of the program
    • d) It can be placed anywhere in the program
  7. How are comments written in Go?
    • a) // This is a comment
    • b) # This is a comment
    • c) /* This is a comment */
    • d) ! This is a comment
  8. What is the file extension for Go source code files?
    • a) .g
    • b) .go
    • c) .golang
    • d) .goc
  9. Which of the following is true about Go code formatting?
    • a) Go code must be manually indented using spaces
    • b) Go uses the gofmt tool to automatically format code
    • c) Indentation in Go does not matter
    • d) Go code must be written in all uppercase
  10. How do you declare a new package in Go?
    • a) new package MyPackage
    • b) package MyPackage
    • c) define package MyPackage
    • d) declare package MyPackage

2. Variables, Constants, and Data Types

  1. How do you declare a variable in Go?
    • a) var x int
    • b) let x int
    • c) variable x int
    • d) const x int
  2. What keyword is used to declare constants in Go?
    • a) const
    • b) final
    • c) static
    • d) immutable
  3. Which of the following is a valid data type in Go?
    • a) integer
    • b) int
    • c) floatNum
    • d) stringValue
  4. How do you declare a variable with an initial value in Go?
    • a) var x = 10
    • b) x = 10 var
    • c) const x 10
    • d) variable x: 10
  5. How are multiple variables declared in a single statement in Go?
    • a) var x, y int
    • b) var x = 10, y = 20
    • c) let x, y
    • d) multiple var x, y
  6. Which type is used for declaring a floating-point variable in Go?
    • a) float32
    • b) double
    • c) float
    • d) decimal
  7. Which of the following is an example of an invalid Go data type?
    • a) int
    • b) bool
    • c) float64
    • d) character
  8. Can you change the value of a constant in Go after it has been initialized?
    • a) Yes, constants are mutable
    • b) No, constants cannot be changed once initialized
    • c) Yes, but only within the same function
    • d) Yes, constants can be reinitialized
  9. What is the default value of an uninitialized integer in Go?
    • a) 0
    • b) nil
    • c) undefined
    • d) false
  10. How do you declare a string variable in Go?
    • a) var str string
    • b) let str string
    • c) declare str string
    • d) string str

3. Basic Input and Output

  1. Which function is used to print output to the console in Go?
    • a) print()
    • b) println()
    • c) fmt.print()
    • d) fmt.Println()
  2. How do you read user input from the console in Go?
    • a) input()
    • b) fmt.Scan()
    • c) fmt.read()
    • d) getInput()
  3. Which package do you need to import to perform basic I/O in Go?
    • a) io
    • b) fmt
    • c) input
    • d) output
  4. Which of the following is used to output formatted text in Go?
    • a) fmt.Printf()
    • b) fmt.Print()
    • c) fmt.Scanf()
    • d) printFormatted()
  5. How do you scan a string input from the user in Go?
    • a) fmt.Scanln(&str)
    • b) input.Scan(&str)
    • c) fmt.Scan(str)
    • d) readInput(&str)
  6. What is the syntax for reading multiple values using fmt.Scan in Go?
    • a) fmt.Scan(value1, value2)
    • b) fmt.Scan(&value1, &value2)
    • c) fmt.Scanln(value1, value2)
    • d) input.read(value1, value2)
  7. Which function is used to print text without a newline in Go?
    • a) fmt.Print()
    • b) fmt.PrintLine()
    • c) fmt.Println()
    • d) fmt.printText()
  8. Which of the following is used to print a formatted message in Go?
    • a) fmt.Printf()
    • b) fmt.Printf()
    • c) fmt.Print()
    • d) fmt.Format()
  9. What will the following Go code print? fmt.Println("Hello", "World")
    • a) Hello World
    • b) HelloWorld
    • c) Hello, World
    • d) HelloWorld!
  10. How do you print an integer variable num in Go?
    • a) fmt.Print(num)
    • b) fmt.Println(num)
    • c) fmt.Println("num")
    • d) fmt.print(num)

Answers

QNoAnswer
1c) package main
2a) It defines the entry point for the program
3b) package main
4a) import "packageName"
5b) It indicates that the program is the entry point
6b) After the imports section
7a) // This is a comment
8b) .go
9b) Go uses the gofmt tool to automatically format code
10b) package MyPackage
11a) var x int
12a) const
13b) int
14a) var x = 10
15a) var x, y int
16a) float32
17d) character
18b) No, constants cannot be changed once initialized
19a) 0
20a) var str string
21d) fmt.Println()
22b) fmt.Scan()
23b) fmt
24a) fmt.Printf()
25a) fmt.Scanln(&str)
26b) fmt.Scan(&value1, &value2)
27a) fmt.Print()
28a) fmt.Printf()
29a) Hello World
30b) fmt.Println(num)

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