Challenge - 5 Problems
Go Var Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of variable declaration with var and default value
What is the output of this Go program?
Go
package main import "fmt" func main() { var x int fmt.Println(x) }
Attempts:
2 left
๐ก Hint
Variables declared with var get a default zero value.
โ Incorrect
In Go, when you declare a variable with var but do not assign a value, it gets the zero value for its type. For int, the zero value is 0.
โ Predict Output
intermediate2:00remaining
Output of multiple variable declaration using var
What will this Go program print?
Go
package main import "fmt" func main() { var a, b, c = 1, "hello", true fmt.Println(a, b, c) }
Attempts:
2 left
๐ก Hint
var can declare multiple variables with initial values.
โ Incorrect
Variables a, b, c are declared with values 1, "hello", and true respectively, so printing them shows those values.
โ Predict Output
advanced2:00remaining
Output of var declaration with explicit type and no initial value
What is the output of this Go program?
Go
package main import "fmt" func main() { var flag bool fmt.Println(flag) }
Attempts:
2 left
๐ก Hint
Boolean variables default to false if not initialized.
โ Incorrect
The zero value for bool type in Go is false, so flag prints false.
โ Predict Output
advanced2:00remaining
Output of var declaration with short variable declaration inside function
What will this Go program print?
Go
package main import "fmt" func main() { var x int = 10 y := 20 fmt.Println(x + y) }
Attempts:
2 left
๐ก Hint
The + operator adds integers.
โ Incorrect
x is 10 and y is 20, so x + y equals 30.
๐ง Conceptual
expert2:00remaining
Error caused by var declaration with conflicting types
What error will this Go code produce?
Go
package main func main() { var x int = 5 var x string = "hello" }
Attempts:
2 left
๐ก Hint
You cannot declare two variables with the same name in the same scope.
โ Incorrect
The code tries to declare x twice in the same scope, which causes a redeclaration error.