Challenge - 5 Problems
Go Constants Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of constant iota usage
What is the output of this Go program?
Go
package main import "fmt" const ( a = iota b c = 5 d e = iota ) func main() { fmt.Println(a, b, c, d, e) }
Attempts:
2 left
๐ก Hint
Remember that iota increments on each line, but explicit assignments reset it.
โ Incorrect
iota starts at 0 for 'a', increments by 1 for 'b'. 'c' is explicitly 5, so 'd' repeats 5. 'e' resumes iota counting from the line number, which is 4.
โ Predict Output
intermediate2:00remaining
Constant expression evaluation
What will this Go program print?
Go
package main import "fmt" const ( x = 3 y = x * 2 z = y + 4 ) func main() { fmt.Println(x, y, z) }
Attempts:
2 left
๐ก Hint
Constants can be expressions using other constants.
โ Incorrect
x is 3, y is 3*2=6, z is 6+4=10.
โ Predict Output
advanced2:00remaining
Constant block with mixed types
What is the output of this Go program?
Go
package main import "fmt" const ( a = 1 b c = "hello" d e = 3.14 f ) func main() { fmt.Printf("%v %v %v %v %v %v", a, b, c, d, e, f) }
Attempts:
2 left
๐ก Hint
Constants repeat the previous value if no new value is given.
โ Incorrect
b repeats a's value 1, d repeats c's value "hello", f repeats e's value 3.14.
โ Predict Output
advanced2:00remaining
Using iota with bit shifting
What does this Go program print?
Go
package main import "fmt" const ( _ = 1 << (iota * 2) A B C ) func main() { fmt.Println(A, B, C) }
Attempts:
2 left
๐ก Hint
iota starts at 0, but the first constant is ignored with underscore.
โ Incorrect
The first line sets _ = 1 << (0*2) = 1, ignored. Then A = 1 << (1*2) = 4, B = 1 << (2*2) = 16, C = 1 << (3*2) = 64.
๐ง Conceptual
expert2:00remaining
Why are constants preferred over variables for fixed values?
Which is the best reason to use constants instead of variables for fixed values in Go?
Attempts:
2 left
๐ก Hint
Think about safety and clarity in code.
โ Incorrect
Constants cannot be changed after compilation, which helps avoid bugs and makes code easier to understand.