0
0
Goprogramming~20 mins

Operator precedence in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Operator Precedence 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 involving mixed operators?

Consider the following Go code snippet. What will it print?

Go
package main
import "fmt"
func main() {
    result := 3 + 4*2
    fmt.Println(result)
}
A14
B11
C10
D7
Attempts:
2 left
๐Ÿ’ก Hint

Remember that multiplication (*) has higher precedence than addition (+).

โ“ Predict Output
intermediate
2:00remaining
What is the output of this Go code with parentheses changing precedence?

What will this Go program print?

Go
package main
import "fmt"
func main() {
    result := (3 + 4) * 2
    fmt.Println(result)
}
A14
B7
C10
D11
Attempts:
2 left
๐Ÿ’ก Hint

Parentheses change the order of operations.

โ“ Predict Output
advanced
2:00remaining
What is the output of this Go code with mixed logical and comparison operators?

What will this Go program print?

Go
package main
import "fmt"
func main() {
    a := 5
    b := 10
    c := 15
    result := a < b && b > c || c == 15
    fmt.Println(result)
}
Atrue
Bfalse
Ccompile error
Dpanic
Attempts:
2 left
๐Ÿ’ก Hint

Logical AND (&&) has higher precedence than logical OR (||).

โ“ Predict Output
advanced
2:00remaining
What is the output of this Go code with bitwise and arithmetic operators?

What will this Go program print?

Go
package main
import "fmt"
func main() {
    x := 6  // binary 110
    y := 3  // binary 011
    result := x & y + 2
    fmt.Println(result)
}
A2
B6
C4
D0
Attempts:
2 left
๐Ÿ’ก Hint

Bitwise AND (&) has higher precedence than addition (+).

โ“ Predict Output
expert
2:00remaining
What is the output of this Go code with mixed assignment and arithmetic operators?

What will this Go program print?

Go
package main
import "fmt"
func main() {
    a := 10
    b := 5
    a += b * 2
    fmt.Println(a)
}
A25
B30
C15
D20
Attempts:
2 left
๐Ÿ’ก Hint

Multiplication happens before the addition assignment.