June 2019 Quiz Answers and Explanation

Here are all the quizzes posted on Twitter and Facebook in June 2019 and their explanation.

Quiz 1

package main

import (  
    "fmt"
)

func hello() []string {  
    return nil
}

func main() {  
    h := hello
    if h == nil {
        fmt.Println("nil")
    } else {
        fmt.Println("not nil")
    }
}

Run in playground

Options
nil  
not nil  
compilation error  
Answer
not nil  

We assign the function hello to the variable h and not the return value of hello() in line no. 12 and hence h is not nil and the program will print not nil.

Quiz 2

package main

import (  
    "fmt"
    "strconv"
)

func main() {  
    i := 2 
    s := "1000"
    if len(s) > 1 {
        i, _ := strconv.Atoi(s)
        i = i + 5
    }
    fmt.Println(i)
}

Run in playground

Options
2  
1005  
compilation error  
Answer
2  

The tricky part of the above quiz is line no. 12. i, _ := strconv.Atoi(s) creates a new variable i whose scope is only within the if statement. The i which is being printed in line no. 15 is actually the one defined in line no. 9 and not the one defined in line no. 12. Hence this program will print 2.

Quiz 3

package main

import (  
    "fmt"
)

func hello(num ...int) {  
    num[0] = 18
}

func main() {  
    i := []int{5, 6, 7}
    hello(i...)
    fmt.Println(i[0])
}

Run in playground

Options
18  
5  
Compilation error  
Answer
18  

The slice i is passed to the variadic function hello() function in line no. 13. To know why this happens, please read the section Passing a slice to a variadic function in https://golangbot.com/variadic-functions/.

If you read the section Passing a slice to a function in https://golangbot.com/arrays-and-slices/, you can understand that changes made to a slice inside a function are visible to the caller. Hence line no. 14 will print 18.

Quiz 4

package main

import (  
    "fmt"
)

func main() {  
    a := [2]int{5, 6}
    b := [2]int{5, 6}
    if a == b {
        fmt.Println("equal")
    } else {
        fmt.Println("not equal")
    }
}

Run in playground

Options
compilation error  
equal  
not equal  
Answer
equal  

Arrays are value types in Go and can be compared. Two array values are equal if their corresponding elements are equal. In our case, a and b are equal and hence this program prints equal.

Quiz 5

package main

import "fmt"

type rect struct {  
    len, wid int
}

func (r rect) area() {  
    fmt.Println(r.len * r.wid)
}

func main() {  
    r := &rect{len: 5, wid: 6}
    r.area()
}

Run in playground

Options
compilation error  
30  
Answer
30  

This program will compile perfectly and print 30.

In line no. 14 of the program above, we assign the address of rect to r. You might be wondering why the program worked when we didn't use (*r).area() in line no. 15. Since area() has a value receiver, Go is intelligent enough to interpret r.area() as (*r).area() and hence this program works :).



Quiz 6

package main

import (  
    "fmt"
)

func main() {  
    a := [5]int{1, 2, 3, 4, 5}
    t := a[3:4:4]
    fmt.Println(t[0])
}

Run in playground

Options
3  
4  
compilation error  
Answer
4  

The expression

a[low : high : max]  

constructs a slice of the same type, and with the same length and elements as the simple slice expression a[low : high]. Additionally, it controls the resulting slice's capacity by setting it to max - low. Hence, the slice t in line no.9 has one element 4 and is of capacity 1.

Quiz 7

package main

import (  
    "fmt"
)

type person struct {  
    name string
}

func main() {  
    var m map[person]int
    p := person{"mike"}
    fmt.Println(m[p])
}

Run in playground

Options
compilation error  
0  
1  
Answer
0  

When we try to print an element that does not exist in a map, the zero value of the element is printed. In our case m is a map of type map[person]int. Since p doesn't exist in the map, the zero value of int i.e 0 is printed.

Quiz 8

package main

import (  
    "fmt"
)

func main() {  
    i := 65
    fmt.Println(string(i))
}

Run in playground

Options
A  
65  
compilation error  
Answer
A  

The unicode value of A is 65. Hence when i is type casted to string in line no. 9, A is printed.

Quiz 9

package main

import (  
    "fmt"
)

func main() {  
    a := 5
    b := 8.1
    fmt.Println(a + b)
}

Run in playground

Options
13.1  
13  
compilation error  
Answer
compilation error  

a is of type int and b is of type float64. We are trying to add a int and float64 in line no. 10. This is not allowed and hence the program will fail to compile with error ./prog.go:10:16: invalid operation: a + b (mismatched types int and float64)



Quiz 10

package main

import (  
    "fmt"
)

func main() {  
    var i interface{}
    if i == nil {
        fmt.Println("nil")
        return
    }
    fmt.Println("not nil")
}

Run in playground

Options
nil  
not nil  
compilation error  
Answer
nil  

An empty interface has both its underlying value and concrete type as nil. Hence i is nil.

Quiz 11

package main

import (  
    "fmt"
)

func hello(i int) {  
    fmt.Println(i)
}
func main() {  
    i := 5
    defer hello(i)
    i = i + 10
}

Run in playground

Options
5  
15  
Answer
5  

The arguments of a deferred function are evaluated when the defer statement is executed and not when the actual function call is done. Hence when the defer statement is encountered in line no. 12, the value of i is 5. So this program will print 5.

Quiz 12

package main

import (  
    "fmt"
)

func main() {  
    fmt.Printf("%%")
}

Run in playground

Options
0.0  
compilation error  
%
Answer
%

The format specifier %% prints a literal % sign. Hence the program prints %.

Quiz 13

package main

import (  
    "fmt"
)

func main() {  
    s := make(map[string]int)
    delete(s, "h")
    fmt.Println(s["h"])
}

Run in playground

Options
runtime panic  
0  
compilation error  
Answer
0  

The delete function in line no. 9 doesn't return anything and will do nothing if the specified key doesn't exist. Since the key h doesn't exist, the delete function will not do anything. In line no. 10, we are trying to print s["h"]. Since the map s doesn't have the key h, it will return the default value of int. Hence 0 will be printed.

Quiz 14

package main

import (  
    "fmt"
)

func main() {  
    i := -5
    j := +5
    fmt.Printf("%+d %+d", i, j)
}

Run in playground

Options
-5 +5
+5 +5
0 0  
Answer
-5 +5

The + flag in the format specifier %+d is used to always print a sign for numeric values. Hence this program outputs -5 +5.

Like my tutorials? Please show your support by donating. Your donations will help me create more awesome tutorials.

Naveen Ramanathan

Naveen Ramanathan has more than 11 years of programming experience in backend and mobile app development. If you would like to hire him, please mail to naveen[at]golangbot[dot]com