Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Output Questions - Part 1
GO

Output Questions - Part 1

Practice Go predict-the-output questions covering fmt, integer division, slices, constants, and arithmetic behavior.

1. What does this fmt.Println print?

func main() {
	fmt.Println(5 + 3)
	fmt.Println(5 / 2)
	fmt.Println(5.0 / 2)
	fmt.Println(7 % 3)
}

Output:

8
2
2.5
1

5 / 2 divides two ints, so it truncates to 2. 5.0 / 2 has a float64 operand, so it’s 2.5. % works on integers: 7 % 3 is 1. Go does not implicitly convert — int division and float division are different operations.

2. What does this fmt.Println with strings print?

func main() {
	fmt.Println("go" + "lang")
	fmt.Println(len("héllo"))
	fmt.Println("abc"[1])
}

Output:

golang
6
98

"go" + "lang" concatenates to "golang". len counts bytes, and é is 2 bytes in UTF-8, so "héllo" is 6 bytes. "abc"[1] indexes a byte — 'b' is 98.

3. What does this fmt.Println with booleans print?

func main() {
	fmt.Println(true && false)
	fmt.Println(true || false)
	fmt.Println(!true)
	fmt.Println(3 > 2 && 2 > 1)
}

Output:

false
true
false
true

&& is logical AND → true && false is false. || is logical OR → true. !true is false. 3 > 2 && 2 > 1 is true && truetrue.

4. What does this fmt.Println with variable shadowing print?

func main() {
	x := 10
	{
		x := 20
		fmt.Println(x)
	}
	fmt.Println(x)
}

Output:

20
10

The inner block declares a new x (via :=), shadowing the outer one, so it prints 20. After the block ends, the outer x is visible again → 10. Inner scopes can redeclare and shadow outer variables.

5. What does this fmt.Println with string concatenation print?

func main() {
	s := "count: " + fmt.Sprint(42)
	fmt.Println(s)
	fmt.Println(fmt.Sprintf("%d items", 3))
}

Output:

count: 42
3 items

fmt.Sprint(42) converts the number to a string for concatenation. fmt.Sprintf formats directly into a new string. Go’s + only works between two strings, so numbers must be formatted first.

6. What does this fmt.Println with runes print?

func main() {
	fmt.Println('A')
	fmt.Println('A' + 1)
	fmt.Println(string('A'))
	fmt.Println('A' == 65)
}

Output:

65
66
A
true

A rune literal ('A') is just an int32 alias, so it prints as 65. 'A' + 1 is 66. string('A') converts back to the character. 'A' == 65 is true — runes compare numerically.

7. What does this fmt.Println with a nil slice print?

func main() {
	var s []int
	fmt.Println(s == nil)
	fmt.Println(len(s))
	var m map[string]int
	fmt.Println(m == nil)
	fmt.Println(len(m))
}

Output:

true
0
true
0

A var s []int declares a nil slice; comparing to nil is true, and len is 0. Same for a nil map: == nil is true, len is 0. Nil slices and maps are usable for reads and len — only writes to a nil map panic.

8. What does this fmt.Println with a nil map write print?

func main() {
	var m map[string]int
	fmt.Println(m["missing"])
	fmt.Println(m["missing"] == 0)
	// m["k"] = 1  // would panic: assignment to entry in nil map
}

Output:

0
true

Reading a missing key from any map — nil or not — returns the zero value (0 for int). There’s no “key not found” error by default; you must use the comma-ok form to distinguish. The commented line shows that writing to a nil map panics.

9. What does this fmt.Println with int overflow print?

func main() {
	var u uint8 = 255
	fmt.Println(u + 1)
	var i int8 = 127
	fmt.Println(i + 1)
}

Output:

0
-128

uint8 wraps: 255 + 1 overflows to 0. int8 wraps from 127 to -128. Go silently wraps unsigned and signed integer overflow at compile/runtime — there’s no exception, it just wraps around.

10. What does this fmt.Println with short variable declaration print?

func main() {
	a, b := 1, 2
	a, b = b, a
	fmt.Println(a, b)
}

Output:

2 1

a, b = b, a is a parallel assignment — both values are read before either is written, so it swaps cleanly: a becomes 2, b becomes 1. No temp variable needed.

11. What does this fmt.Println with int-to-float print?

func main() {
	x := 7 / 2
	y := 7 / 2.0
	z := float64(7) / 2
	fmt.Println(x)
	fmt.Println(y)
	fmt.Println(z)
}

Output:

3
3.5
3.5

7 / 2 is integer division → 3. 7 / 2.0 — untyped constant 2.0 is float64, so the division is float → 3.5. float64(7) / 2 explicitly converts first → 3.5. Go keeps int and float division distinct.

12. What does this fmt.Println with string slicing print?

func main() {
	s := "hello"
	fmt.Println(s[1:3])
	fmt.Println(s[0:5])
	fmt.Println(s[:])
}

Output:

el
hello
hello

"hello"[1:3] takes bytes 1 through 2 (end-exclusive) → "el". s[0:5] is the whole string → "hello". s[:] is the whole string → "hello". String slicing produces a new string sharing the same backing data.

13. What does this fmt.Println with a byte slice print?

func main() {
	b := []byte("hi")
	b[0] = 'H'
	fmt.Println(string(b))
	fmt.Println(len(b))
}

Output:

Hi
2

[]byte("hi") is a mutable copy of the string’s bytes. Changing b[0] to 'H' affects the slice, not the original literal. string(b) converts back → "Hi". len(b) is 2 bytes.

14. What does this fmt.Println with constants print?

const (
	A = 1
	B = iota
	C
)

func main() {
	fmt.Println(A, B, C)
}

Output:

1 1 2

In a const block, A = 1 is explicit. B = iota gets the block index 1 (iota starts at 0; B is the second entry). C repeats the previous expression, iota, which is now 2. So the output is 1 1 2.

15. What does this fmt.Println with float precision print?

func main() {
	a := 0.1
	b := 0.2
	c := 0.3
	fmt.Println(a + b)
	fmt.Println(a+b == c)
}

Output:

0.30000000000000004
false

With variables, a + b is a runtime float64 addition. 0.1 and 0.2 aren’t exactly representable in binary, so the sum is 0.30000000000000004, and it’s not equal to the float64 nearest 0.3. (As untyped constants, 0.1 + 0.2 == 0.3 would be true — constants are computed exactly. That’s the trap.) Comparing floats with == is unreliable; use an epsilon instead.

My Private Notes

Notes are auto-saved locally to this device.