Go Variables MCQ

1. What is the correct syntax for declaring a variable in Go?

a) var myVar int
b) int myVar
c) myVar := int
d) declare myVar as int

Answer:

a) var myVar int

Explanation:

In Go, a variable is declared using the 'var' keyword followed by the variable name and the type. The syntax is 'var variableName variableType'.

2. Which of the following is used for type inference in Go?

a) :=
b) ==
c) =
d) ::

Answer:

a) :=

Explanation:

The ':=' syntax is used for type inference in Go, where the compiler determines the type of the variable based on the initial value assigned to it.

3. How do you declare a block of variables in Go?

a) var { a int, b string }
b) var ( a int; b string )
c) var ( a int; b string; )
d) var [ a int, b string ]

Answer:

c) var ( a int; b string; )

Explanation:

In Go, a block of variables is declared using the 'var' keyword followed by parentheses. Each variable declaration is separated by a semicolon.

4. What is the zero value of a string variable in Go?

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

Answer:

b) ""

Explanation:

The zero value of a string in Go is an empty string, denoted by "".

5. Can you redeclare a variable in the same scope in Go?

a) Yes, always
b) No, never
c) Yes, but only if it is of a different type
d) Yes, but only if it is not yet assigned a value

Answer:

b) No, never

Explanation:

In Go, you cannot redeclare a variable in the same scope. Once a variable is declared, its name cannot be reused in the same scope.

6. Which of the following is NOT a valid way to declare and initialize a variable in Go?

a) var a int = 10
b) b := 20
c) var c = 30
d) d int = 40

Answer:

d) d int = 40

Explanation:

The correct syntax to declare and initialize a variable in Go does not include the type before the variable name when using the '=' assignment. 'd int = 40' is incorrect.

7. How do you declare a package level variable in Go?

a) package var myVar int
b) var myVar int
c) global myVar int
d) public var myVar int

Answer:

b) var myVar int

Explanation:

Package level variables in Go are declared outside of any function using the 'var' keyword. They are accessible from any function within the package.

8. What is the default value of a boolean variable in Go?

a) true
b) false
c) 0
d) null

Answer:

b) false

Explanation:

The zero value of a boolean in Go is 'false'.

9. Which of the following declares a pointer variable in Go?

a) var ptr *int
b) var *ptr int
c) int *ptr
d) ptr *int

Answer:

a) var ptr *int

Explanation:

In Go, a pointer variable is declared using the 'var' keyword, followed by the variable name, a star (*), and the type of the value it points to.

10. How do you initialize a variable with a constant value in Go?

a) var myVar int = 5
b) myVar := 5
c) const myVar = 5
d) myVar = 5

Answer:

c) const myVar = 5

Explanation:

In Go, constants are declared using the 'const' keyword. The value of a constant should be known at compile-time and cannot be changed.

Leave a Comment

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

Scroll to Top