Go Data Types MCQ

1. What is the default type of an integer literal in Go if it does not fit into int32?

a) int32
b) int64
c) int
d) uint64

Answer:

b) int64

Explanation:

If an integer literal exceeds the size of int32, Go defaults to int64 for the literal's type.

2. Which of the following types represents a floating-point number in Go?

a) int
b) float32
c) string
d) boolean

Answer:

b) float32

Explanation:

In Go, float32 and float64 are the types used to represent floating-point numbers.

3. What is the zero value of a struct in Go?

a) null
b) 0
c) An empty struct
d) Each field set to its zero value

Answer:

d) Each field set to its zero value

Explanation:

The zero value of a struct in Go is a struct with all its fields set to their respective zero values.

4. In Go, which keyword is used to define a new struct?

a) class
b) struct
c) new
d) type

Answer:

b) struct

Explanation:

The 'struct' keyword is used to define a new struct type in Go.

5. What is the output type of the 'make' function when used with a slice in Go?

a) Array
b) Slice
c) Map
d) Pointer

Answer:

b) Slice

Explanation:

The 'make' function is used to create slices, maps, and channels in Go, and it returns a slice when used with a slice type.

6. Which data type is used to represent a sequence of characters in Go?

a) char
b) string
c) rune
d) byte

Answer:

b) string

Explanation:

In Go, the 'string' data type is used to represent a sequence of characters.

7. How do you declare an array of 5 integers in Go?

a) var arr [5]int
b) int arr[5]
c) var arr = [5]int
d) [5]int arr

Answer:

a) var arr [5]int

Explanation:

Arrays in Go are declared by specifying the type of elements and the number of elements in square brackets before the variable name.

8. Which of the following is NOT a valid map declaration in Go?

a) var myMap map[string]int
b) myMap := map[string]int{}
c) map[string]int myMap
d) var myMap = make(map[string]int)

Answer:

c) map[string]int myMap

Explanation:

In Go, the correct syntax to declare a map either uses the 'var' keyword followed by the map type, or the ':=' syntax with the 'make' function or a map literal.

9. What is the zero value for a pointer in Go?

a) 0
b) null
c) nil
d) undefined

Answer:

c) nil

Explanation:

The zero value for a pointer in Go is 'nil'.

10. Which keyword is used to define an interface in Go?

a) interface
b) struct
c) type
d) class

Answer:

a) interface

Explanation:

Interfaces in Go are defined using the 'interface' keyword.

11. Which data type in Go is used to represent a Unicode code point?

a) string
b) char
c) rune
d) byte

Answer:

c) rune

Explanation:

The 'rune' data type in Go represents a Unicode code point. It is an alias for int32.

12. What is the default value of a slice in Go?

a) nil
b) Empty array
c) Zero
d) An array with zero elements

Answer:

a) nil

Explanation:

The zero value of a slice in Go is 'nil'.

13. How do you specify a variable-length argument list in a Go function?

a) Using the 'varargs' keyword
b) By prefixing the type with '…'
c) By using an array
d) By using a slice

Answer:

b) By prefixing the type with '…'

Explanation:

In Go, a variable-length argument list is specified by prefixing the type of the argument with '…'.

14. What is the key difference between a slice and an array in Go?

a) Arrays can store elements of different types, but slices cannot.
b) Slices are fixed in size, whereas arrays are dynamic.
c) Arrays are fixed in size, whereas slices are dynamic.
d) Slices support direct data manipulation, arrays do not.

Answer:

c) Arrays are fixed in size, whereas slices are dynamic.

Explanation:

In Go, arrays have a fixed size, while slices are a flexible view into the elements of an array and can dynamically change size.

15. What does the 'range' keyword in Go return when iterating over a map?

a) Only the keys
b) Only the values
c) Both keys and values
d) Index and value

Answer:

c) Both keys and values

Explanation:

When iterating over a map with the 'range' keyword in Go, it returns both the key and the value for each entry in the map.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top