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
What is the first statement in a Go program?
a) import
b) func main()
c) package main
d) package
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
Which of the following is required in every Go program?
a) func
b) package main
c) import
d) main function
How are Go packages imported?
a) import "packageName"
b) import("packageName")
c) import packageName;
d) include "packageName"
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
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
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
What is the file extension for Go source code files?
a) .g
b) .go
c) .golang
d) .goc
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
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
How do you declare a variable in Go?
a) var x int
b) let x int
c) variable x int
d) const x int
What keyword is used to declare constants in Go?
a) const
b) final
c) static
d) immutable
Which of the following is a valid data type in Go?
a) integer
b) int
c) floatNum
d) stringValue
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
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
Which type is used for declaring a floating-point variable in Go?
a) float32
b) double
c) float
d) decimal
Which of the following is an example of an invalid Go data type?
a) int
b) bool
c) float64
d) character
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
What is the default value of an uninitialized integer in Go?
a) 0
b) nil
c) undefined
d) false
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
Which function is used to print output to the console in Go?
a) print()
b) println()
c) fmt.print()
d) fmt.Println()
How do you read user input from the console in Go?
a) input()
b) fmt.Scan()
c) fmt.read()
d) getInput()
Which package do you need to import to perform basic I/O in Go?
a) io
b) fmt
c) input
d) output
Which of the following is used to output formatted text in Go?
a) fmt.Printf()
b) fmt.Print()
c) fmt.Scanf()
d) printFormatted()
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)
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)
Which function is used to print text without a newline in Go?
a) fmt.Print()
b) fmt.PrintLine()
c) fmt.Println()
d) fmt.printText()
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()
What will the following Go code print? fmt.Println("Hello", "World")
a) Hello World
b) HelloWorld
c) Hello, World
d) HelloWorld!
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
QNo
Answer
1
c) package main
2
a) It defines the entry point for the program
3
b) package main
4
a) import "packageName"
5
b) It indicates that the program is the entry point
6
b) After the imports section
7
a) // This is a comment
8
b) .go
9
b) Go uses the gofmt tool to automatically format code
10
b) package MyPackage
11
a) var x int
12
a) const
13
b) int
14
a) var x = 10
15
a) var x, y int
16
a) float32
17
d) character
18
b) No, constants cannot be changed once initialized