Golang Quiz – Golang Multiple Choice Questions and Answers: If you’re looking for Go Online Quiz for Experienced or Freshers, you are at the right place. Moreover, the Golang MCQ Quiz helps you in cracking your interview & acquiring a dream career. Our freshers now team has designed this Golang MCQ Quiz Questions & Answers to assess your knowledge and understanding of the Golang basics. And you can easily get the Golang Multiple Choice Questions and Answers that are repeatedly asked in various interviews by experts/ HR teams. And we have kept all the views of students and made this article about the Golang Multiple Choice Questions so that it will be easy for you to answer them depending upon your knowledge.
Golang Quiz – Golang Multiple Choice Questions and Answers
Quiz Name | Golang |
Category | Technical Quiz |
Exam Type | MCQ (Multiple Choice Questions) |
Mode of Quiz | Online |
Practice Golang Quiz Questions and Answers
What is the correct way to declare a variable in Golang?
A) var x int
B) int x
C) x := int
D) x int
Answer: A) var x int
Explanation: In Golang, the correct way to declare a variable is by using the ‘var’ keyword followed by the variable name and the data type.
What is the output of the following code?
func main() {
var x = 5
var y = 10
var z = x + y
fmt.Println(z)
}
A) 5
B) 10
C) 15
D) Error
Answer: C) 15
Explanation: The code declares three variables and assigns values to two of them. It then calculates the sum of the two variables and assigns it to the third variable. Finally, it prints the value of the third variable, which is the sum of the first two variables.
What is the correct way to create a new slice in Golang?
A) slice := make([]int)
B) slice := []int{}
C) slice := []int
D) slice := new([]int)
Answer: B) slice := []int{}
Explanation: The correct way to create a new slice in Golang is by using empty curly braces after the type declaration.
What is the difference between a map and a slice in Golang?
A) A map is an ordered collection of elements, while a slice is not.
B) A map is a reference type, while a slice is a value type.
C) A map can have any type of key and value, while a slice can only have one type of element.
D) A map is a collection of key-value pairs, while a slice is a collection of values.
Answer: D) A map is a collection of key-value pairs, while a slice is a collection of values.
Explanation: A map is a data structure that maps keys to values, while a slice is an ordered collection of values.
What is the output of the following code?
func main() {
var x = 5
if x > 3 {
fmt.Println(“x is greater than 3”)
}
}
A) x is greater than 3
B) 5
C) true
D) Error
Answer: A) x is greater than 3
Explanation: The code declares a variable x and checks if it is greater than 3 using an if statement. If x is greater than 3, it prints the message “x is greater than 3”.
What is the correct way to define a function that takes two parameters and returns their sum in Golang?
A) func sum(int x, int y) int { return x + y }
B) func sum(x int, y int) { return x + y }
C) func sum(x int, y int) int { return x + y }
D) func sum(x, y int) int { return x + y }
Answer: D) func sum(x, y int) int { return x + y }
Explanation: The correct way to define a function that takes two parameters and returns their sum in Golang is by specifying the types of the parameters and the return type, followed by the function body.
What is the correct way to append an element to a slice in Golang?
A) append(slice, element)
B) slice.append(element)
C) slice += element
D) slice = append(element, slice)
Answer: A) append(slice, element)
Explanation: The correct way to append an element to a slice in Golang is by using the ‘append’ function, which takes the slice and the element to be appended as arguments.
What is the output of the following code?
func main() {
var x = map[string]int{“a”: 1, “b”: 2}
delete(x, “a”)
fmt.Println(x)
}
A) {“a”: 1}
B) {“b”: 2}
C) {}
D) Error
Answer: B) {“b”: 2}
Explanation: The code declares a map with two key-value pairs and then deletes the “a” key from the map. The resulting map only contains the “b” key-value pair.
What is the correct way to declare a constant in Golang?
A) const x = 5
B) var x = 5
C) x := const 5
D) x const = 5
Answer: A) const x = 5
Explanation: In Golang, constants are declared using the ‘const’ keyword followed by the constant name and the value.
What is the output of the following code?
func main() {
var x = []int{1, 2, 3}
for i := range x {
fmt.Println(i)
}
}
A) 1, 2, 3
B) 0, 1, 2
C) [1, 2, 3]
D) Error
Answer: B) 0, 1, 2
Explanation: The code declares a slice and then uses a for loop to iterate over the indices of the slice. The loop prints the indices, which are 0, 1, and 2.
What is the correct way to create a new map in Golang?
A) map := make(map)
B) map := map[string]int{}
C) map := map{}
D) map := new(map[string]int)
Answer: B) map := map[string]int{}
Explanation: The correct way to create a new map in Golang is by using curly braces after the type declaration.
What is the output of the following code?
func main() {
var x = 5
switch x {
case 1:
fmt.Println(“x is 1”)
case 5:
fmt.Println(“x is 5”)
default:
fmt.Println(“x is neither 1 nor 5″)
}
}
A) x is 1
B) x is 5
C) x is neither 1 nor 5
D) Error
Answer: B) x is 5
Explanation: The code declares a variable x and uses a switch statement to check its value. Since x is 5, the second case is matched, and the corresponding message is printed.
What is the correct way to declare a struct in Golang?
A) struct Person { name string }
B) type Person struct { name string }
C) struct { name string } Person
D) Person := struct { name string }{}
Answer: B) type Person struct { name string }
Explanation: In Golang, structs are declared using the ‘type’ keyword followed by the struct name and the struct fields.
What is the output of the following code?
func main() {
var x = []int{1, 2, 3}
var y = x[:2]
y[0] = 5
fmt.Println(x)
}
A) [1, 2, 3]
B) [5, 2, 3]
C) [1, 5, 3]
D) Error
Answer: C) [1, 5, 3]
Explanation: The code declares a slice x and creates a new slice y that includes the first two elements of x. Then, the first element of y is changed to 5. Since slices in Golang are references to an underlying array, changing the value of an element in one slice will change it in the other slice as well.
What is the correct way to read user input from the console in Golang?
A) var input string = fmt.Scanln()
B) input, _ := reader.ReadString(‘\n’)
C) input := os.Stdin.Read()
D) input := bufio.NewScanner(os.Stdin).Scan()
Answer: D) input := bufio.NewScanner(os.Stdin).Scan()
Explanation: In Golang, the ‘bufio’ package provides a way to read input from the console using the ‘NewScanner’ function and the ‘Scan’ method.
What is the output of the following code?
func main() {
var x = 5
var y = &x
fmt.Println(y)
}
A) 5
B) &5
C) Error
D) Memory address of x
Answer: A) 5
Explanation: The code declares a variable x and creates a pointer to it using the ‘&’ operator. The pointer is then dereferenced using the ” operator to print the value of x.
What is the correct way to declare a function in Golang that returns multiple values?
A) func add(x int, y int) (int, int)
B) func add(x int, y int) (a int, b int)
C) func add(x int, y int) [2]int
D) func add(x int, y int) int, int
Answer: B) func add(x int, y int) (a int, b int)
Explanation: In Golang, functions that return multiple values are declared using parentheses to list the return types and then assigning names to the return values.
What is the output of the following code?
func main() {
var x interface{}
x = “hello”
if s, ok := x.(string); ok {
fmt.Println(s)
}
}
A) “hello”
B) “”
C) Error
D) Nothing will be printed
Answer: A) “hello”
Explanation: The code declares an empty interface x and assigns it the value “hello”. Then, it uses a type assertion to check whether x is a string, and if it is, it prints the string.
What is the correct way to run a Go program?
A) go run program.go
B) run program.go
C) execute program.go
D) program.go
Answer: A) go run program.go
Explanation: In Golang, programs are compiled and run using the ‘go’ command-line tool, which can be used to build and run Go programs. The ‘run’ subcommand can be used to run a Go program directly from the source code file.
What is the correct way to iterate over a slice in Golang?
A) for i := 0; i < len(slice); i++ {}
B) for i, v := range slice {}
C) for _, v := range slice {}
D) All of the above
Answer: B) for i, v := range slice {}
Explanation: In Golang, the ‘range’ keyword can be used to iterate over a slice, returning the index and the value at each iteration.