0
0
Goprogramming~20 mins

Type inference in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Go Type Inference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Go code using type inference?
Consider the following Go program. What will it print when run?
Go
package main
import "fmt"
func main() {
    x := 10
    y := 3.5
    z := x + int(y)
    fmt.Println(z)
}
AError: cannot add int and float64
B13.5
C10
D13
Attempts:
2 left
๐Ÿ’ก Hint
Remember Go requires explicit conversion between int and float64 types.
โ“ Predict Output
intermediate
1:30remaining
What is the type of variable 'a' inferred by Go?
Look at this Go code snippet. What is the inferred type of variable 'a'?
Go
package main
func main() {
    a := 'G'
}
Arune
Bstring
Cint
Dbyte
Attempts:
2 left
๐Ÿ’ก Hint
Single quotes in Go denote a rune literal.
๐Ÿ”ง Debug
advanced
2:00remaining
Why does this Go code cause a compilation error?
Examine the code below. Why does it fail to compile?
Go
package main
func main() {
    var x = 5
    x = "hello"
}
AVariable x is not declared
BMissing semicolon after variable declaration
CCannot assign string to variable x of inferred type int
DCannot assign int to string variable
Attempts:
2 left
๐Ÿ’ก Hint
Check the type inferred for x and what value is assigned later.
โ“ Predict Output
advanced
2:00remaining
What is the output of this Go code using type inference with constants?
What will this Go program print?
Go
package main
import "fmt"
func main() {
    const pi = 3.14
    radius := 5
    area := pi * float64(radius) * float64(radius)
    fmt.Printf("%.2f", area)
}
A78.50
B78
CError: cannot convert radius to float64
D15.7
Attempts:
2 left
๐Ÿ’ก Hint
Look at how radius is converted before multiplication.
๐Ÿง  Conceptual
expert
1:30remaining
Which statement about Go's type inference is true?
Select the correct statement about how Go infers types for variables declared with ':='.
AGo allows the variable type to change dynamically at runtime based on assigned values.
BGo infers the variable type from the right-hand side expression at compile time and it cannot change later.
CGo infers the variable type as interface{} by default when using ':='.
DGo requires explicit type annotations even when using ':='.
Attempts:
2 left
๐Ÿ’ก Hint
Think about static typing and how ':=' works in Go.