Integer division with negatives — what does this code print?
func main() {
fmt.Println(7 / 2)
fmt.Println(-7 / 2)
}- A.4 -3
- B.3 -4
- C.3 -3
- D.3.5 -3.5
Answer: C
Prints 3 then -3. Division between two ints is integer division; Go truncates the fractional part TOWARD ZERO (truncate toward zero), so 7/2 = 3 and -7/2 = -3 — unlike Python, which rounds down (-4). To get a float result, at least one operand must be a float. (FREE)
References