0
0
Goprogramming~20 mins

Variable declaration using var in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Var Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2: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)
}
Aundefined
Bnil
C0
D1
Attempts:
2 left
๐Ÿ’ก Hint
Variables declared with var get a default zero value.
โ“ Predict Output
intermediate
2: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)
}
A1 0 true
B1 hello true
C0 hello false
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
var can declare multiple variables with initial values.
โ“ Predict Output
advanced
2: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)
}
Afalse
Btrue
Cnil
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Boolean variables default to false if not initialized.
โ“ Predict Output
advanced
2: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)
}
A30
B1020
CCompilation error
D0
Attempts:
2 left
๐Ÿ’ก Hint
The + operator adds integers.
๐Ÿง  Conceptual
expert
2: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"
}
Ano error, compiles fine
Btype mismatch error
Csyntax error: missing semicolon
Dcannot redeclare x
Attempts:
2 left
๐Ÿ’ก Hint
You cannot declare two variables with the same name in the same scope.