2.3 KiB
2.3 KiB
What's a magic value?
- A value which pops up from somewhere
- Merlin the Wizard's spell
- An unnamed constant value in the source code CORRECT
- A named constant
What's a named constant?
- A constant with a cool name
- A constant value declared with a name CORRECT
- A literal value converted to a name
How to declare a constant?
Const version int = 3
const version int := 3
const version int = 3
CORRECT
1: "C"onst should be just "c"onst.
Which code below is correct?
s := "pick me"; const length = len(s)
const message = "pick me!"; const length = len(message)
CORRECTconst length = utf8.RuneCountInString("pick me")
1:
s
not a constant.2:
len
function can be used as an initial value to a constant, when the argument tolen
is also a constant.3: You cannot call functions while initializing a constant.
Which explanation below is correct for the following code?
const speed = 100
porsche := speed * 3
- speed is typeless and porsche's type is int CORRECT
- speed's type is int and porsche's type is also int
- speed and porsche are typeless
2: speed has no type.
3: A variable cannot be typeless.
How to fix the following code?
const spell string
spell = "Abracadabra"
const spell = "Abracadabra"
CORRECTspell := "Abracadabra"
var spell = "Abracadabra"
1: A constant always have to be initialized to a value. And, sometimes the type declaration is not necessary.
2-3: That's a variable not a constant.
How to fix the following code?
const total int8 = 10
x := 5
fmt.Print(total * x)
// #1 - *CORRECT*
const total = 10
x := 5
fmt.Print(total * x)
// #2
const total int64 = 10
x := 5
fmt.Print(total * x)
// #3
const total int64 = 10
x := 5
fmt.Print(int64(total) * x)
1: Now, the total constant is typeless, so it can be used with the x variable.
2: There's still a type mismatch. x is int not int64.
3: total is already int64. No need to convert it again.
What are the values of the following constants?
const (
Yes = (iota * 5) + 2
No
Both
)
- Yes=0 No=1 Both=2
- Yes=2 No=3 Both=4
- Yes=7 No=12 Both=17
- Yes=2 No=7 Both=12 CORRECT
3: iota starts at 0, not 1.