Consider the following Go code snippet. What will it print?
package main import "fmt" func main() { result := 3 + 4*2 fmt.Println(result) }
Remember that multiplication (*) has higher precedence than addition (+).
Multiplication happens before addition, so 4*2 = 8, then 3 + 8 = 11.
What will this Go program print?
package main import "fmt" func main() { result := (3 + 4) * 2 fmt.Println(result) }
Parentheses change the order of operations.
Parentheses force addition first: (3 + 4) = 7, then multiply by 2 = 14.
What will this Go program print?
package main import "fmt" func main() { a := 5 b := 10 c := 15 result := a < b && b > c || c == 15 fmt.Println(result) }
Logical AND (&&) has higher precedence than logical OR (||).
Evaluate a < b && b > c first: 5 < 10 is true, 10 > 15 is false, so true && false = false.
Then false || c == 15: c == 15 is true, so false || true = true.
What will this Go program print?
package main import "fmt" func main() { x := 6 // binary 110 y := 3 // binary 011 result := x & y + 2 fmt.Println(result) }
Bitwise AND (&) has higher precedence than addition (+).
First compute x & y: 6 & 3 = 2 (binary 110 & 011 = 010). Then add 2: 2 + 2 = 4.
What will this Go program print?
package main import "fmt" func main() { a := 10 b := 5 a += b * 2 fmt.Println(a) }
Multiplication happens before the addition assignment.
b * 2 is evaluated first: 5 * 2 = 10. Then a += 10 means a = a + 10 = 10 + 10 = 20.