Go Constants MCQ

1. How do you declare a constant in Go?

a) const myConst = 10
b) var myConst = 10
c) myConst := 10
d) #define myConst 10

Answer:

a) const myConst = 10

Explanation:

Constants in Go are declared using the 'const' keyword followed by the constant name and its value.

2. Can a constant in Go be declared without an initial value?

a) Yes, it can be initialized later.
b) No, it must be initialized during declaration.
c) Yes, but only for string types.
d) No, unless it is a global constant.

Answer:

b) No, it must be initialized during declaration.

Explanation:

Constants in Go must be initialized at the time of their declaration and cannot be assigned a value later.

3. What is the type of an untyped constant in Go?

a) The type is inferred from the context.
b) It defaults to int.
c) It is always float64.
d) Constants are always typed in Go.

Answer:

a) The type is inferred from the context.

Explanation:

Untyped constants in Go don't have a fixed type. Their type is inferred based on the context in which they are used.

4. Which of these is a valid way to declare a group of constants in Go?

a) const ( a = 1, b = 2, c = 3 )
b) const a, b, c = 1, 2, 3
c) const ( a = 1 b = 2 c = 3 )
d) const ( a = 1; b = 2; c = 3 )

Answer:

c) const ( a = 1 b = 2 c = 3 )

Explanation:

In Go, a group of constants can be declared inside parentheses with each constant declaration separated by a newline.

5. Which of the following is true about constant blocks in Go?

a) Constants in a block must all be of the same type.
b) Constants in a block can have different types.
c) Each constant in a block must be explicitly initialized.
d) Blocks can only define numeric constants.

Answer:

b) Constants in a block can have different types.

Explanation:

In Go, constants in a block can be of different types, and it's not mandatory for each constant to be explicitly initialized if it follows an iota.

6. What does 'iota' represent in Go?

a) A constant type
b) An auto-incremented constant generator
c) A built-in function for constants
d) A special operator for constants

Answer:

b) An auto-incremented constant generator

Explanation:

'iota' is a predeclared identifier in Go representing successive untyped integer constants, used to create enumerated constants.

7. What is the zero value of an untyped boolean constant in Go?

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

Answer:

b) false

Explanation:

The zero value of an untyped boolean constant in Go is 'false'.

8. How does Go handle typed and untyped constants in an operation?

a) Typed constants cannot be mixed with untyped constants.
b) Typed and untyped constants are always compatible.
c) An untyped constant is converted to the type of the typed constant.
d) The operation is invalid in Go.

Answer:

c) An untyped constant is converted to the type of the typed constant.

Explanation:

In operations involving both typed and untyped constants, the untyped constant is converted to the type of the typed constant.

9. Are constant expressions in Go evaluated at compile-time or runtime?

a) Compile-time
b) Runtime
c) Both
d) Neither

Answer:

a) Compile-time

Explanation:

Constant expressions in Go are evaluated at compile time.

10. Can you use arithmetic operations on constants of different types in Go?

a) Yes, always.
b) No, it results in a compile-time error.
c) Yes, but only if they are untyped.
d) Yes, but only if they are explicitly converted.

Answer:

c) Yes, but only if they are untyped.

Explanation:

Arithmetic operations can be performed on untyped constants of different types, as Go will infer the type based on the context. However, typed constants must be explicitly converted to a common type.

Leave a Comment

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

Scroll to Top