16. What does this fmt.Println with append print?
func main() {
s := []int{1, 2, 3}
s = append(s, 4)
fmt.Println(len(s), cap(s))
fmt.Println(s)
}
Output:
4 6
[1 2 3 4]
append(s, 4) grows the slice. len becomes 4. cap grows to 6 because the backing array (capacity 3) was full and Go allocates a new one with extra room (2x growth here). The printed cap is implementation-defined, but len/contents are guaranteed.
17. What does this fmt.Println with slice aliasing print?
func main() {
a := []int{1, 2, 3, 4}
b := a[1:3]
b[0] = 99
fmt.Println(a)
fmt.Println(b)
}
Output:
[1 99 3 4]
[99 3]
b := a[1:3] shares a’s backing array. Writing b[0] = 99 writes through to a[1], so a becomes [1 99 3 4]. Slices are references into a shared backing array — this aliasing surprises newcomers.
18. What does this fmt.Println with a copy of a slice print?
func main() {
a := []int{1, 2, 3}
b := a
b[0] = 99
fmt.Println(a)
fmt.Println(b)
}
Output:
[99 2 3]
[99 2 3]
b := a copies the slice header (pointer, len, cap), not the elements. Both point to the same backing array, so b[0] = 99 is visible through a. To copy the elements, use copy(b, a) or append([]int{}, a...).
19. What does this fmt.Println with copy print?
func main() {
a := []int{1, 2, 3}
b := make([]int, len(a))
copy(b, a)
b[0] = 99
fmt.Println(a)
fmt.Println(b)
}
Output:
[1 2 3]
[99 2 3]
copy(b, a) copies the elements into b’s fresh backing array. Now the two slices are independent: writing b[0] = 99 leaves a untouched → [1 2 3].
20. What does this fmt.Println with multiple returns print?
func divmod(a, b int) (int, int) {
return a / b, a % b
}
func main() {
q, r := divmod(10, 3)
fmt.Println(q, r)
}
Output:
3 1
divmod(10, 3) returns (10/3, 10%3) = (3, 1). Multiple return values are destructured into q and r with :=. This is Go’s standard way of returning related values.
21. What does this fmt.Println with a variadic function print?
func sum(nums ...int) int {
total := 0
for _, n := range nums {
total += n
}
return total
}
func main() {
fmt.Println(sum(1, 2, 3))
fmt.Println(sum())
}
Output:
6
0
sum(1, 2, 3) collects the variadic args into []int{1, 2, 3} → 6. sum() passes an empty slice → the loop doesn’t run, total stays 0. Variadic parameters are slices inside the function.
22. What does this fmt.Println with deferred calls print?
func main() {
defer fmt.Println("first")
defer fmt.Println("second")
fmt.Println("main")
}
Output:
main
second
first
defer runs when the function returns, and defers execute LIFO (last deferred, first executed). So "main" prints first, then "second", then "first". Deferred calls run after the surrounding function body completes.
23. What does this fmt.Println with a named return print?
func f() (n int) {
defer func() { n = 42 }()
n = 1
return n
}
func main() {
fmt.Println(f())
}
Output:
42
return n sets the named return value n = 1, then the deferred closure runs and overwrites it to 42 before the function actually returns. A deferred function can modify named return values — this is a classic Go interview trick.
24. What does this fmt.Println with a closure print?
func counter() func() int {
n := 0
return func() int {
n++
return n
}
}
func main() {
c := counter()
fmt.Println(c())
fmt.Println(c())
fmt.Println(c())
}
Output:
1
2
3
The returned closure captures n by reference. Each call increments the same n, so it prints 1, 2, 3. Closures keep their captured variables alive across calls.
25. What does this fmt.Println with a pointer print?
func main() {
x := 10
p := &x
*p = 20
fmt.Println(x)
fmt.Println(*p)
}
Output:
20
20
p := &x takes x’s address. *p = 20 writes through the pointer, changing x. Both x and *p now print 20. Go has pointers but not pointer arithmetic.
26. What does this fmt.Println with a value receiver print?
type Counter struct{ n int }
func (c Counter) inc() int {
c.n++
return c.n
}
func main() {
c := Counter{}
fmt.Println(c.inc())
fmt.Println(c.inc())
}
Output:
1
1
inc has a value receiver, so it operates on a copy of c. Each call increments the copy and returns 1; c.n stays 0. To persist the increment, the method needs a pointer receiver (func (c *Counter) inc()).
27. What does this fmt.Println with a map iteration print?
func main() {
m := map[string]int{"a": 1, "b": 2}
delete(m, "a")
fmt.Println(len(m))
fmt.Println(m["a"])
}
Output:
1
0
delete(m, "a") removes the key → len is 1. Reading a deleted key returns the zero value 0. (Avoid ranging over maps for output prediction — iteration order is randomized in Go.)
28. What does this fmt.Println with type conversion print?
func main() {
var i int = 65
var f float64 = float64(i)
var s string = string(i)
fmt.Println(f)
fmt.Println(s)
}
Output:
65
A
float64(i) converts 65 to 65.0. string(i) converts an integer to the rune it represents — 65 is 'A'. Converting an int to a string does not do numeric-to-text formatting (use fmt.Sprint for that).
29. What does this fmt.Println with a channel print?
func main() {
ch := make(chan int, 1)
ch <- 5
fmt.Println(<-ch)
fmt.Println(len(ch))
}
Output:
5
0
ch <- 5 sends into the buffered channel, then <-ch receives it back out → prints 5. After the receive the buffer is empty, so len(ch) is 0. A buffered channel reports its current queue depth.
30. What does this fmt.Println with an interface value print?
func main() {
var x interface{} = 42
var y interface{} = 42
var z interface{} = "42"
fmt.Println(x == y)
fmt.Println(x == z)
}
Output:
true
false
Two empty interfaces holding the same dynamic type and value (int(42)) compare equal → true. x holds an int, z holds a string, so 42 == "42" is false — different dynamic types. Interface equality compares both the dynamic type and value.
Premium Content
Unlock Output Questions - Part 2 and all premium lessons with a subscription.
From ₹199.99/year — See plans