📜 ⬆️ ⬇️

Avito Quiz: Golang

Hello! Today we continue the series of mini-quizzes in our blog. This issue (the previous one here ) will be devoted to the Go language - we will assume that this is a warm-up on the eve of GopherCon Russia 2018 (by the way, we will have a stand at this conference and we are planning some interesting activities).


Under the cut - seven questions and a pair of Easter eggs. Answers to questions will post an update to the post on Monday, 26.02. If you decide - put the answers under the spoiler, so as not to spoil the other fans.


Enjoy!
UPD: post updated, now the answers are inside!


1. What will this code output?


package main import ( "fmt" ) func add(arr []int, v int) { arr = append(arr, v) } func main() { arr := make([]int, 0, 100000) fmt.Printf("%v %p\n", arr, &arr) add(arr, 10) fmt.Printf("%v %p\n", arr, &arr) } 

Variants of answers:



Correct answer

Both times empty array. Addresses match.


2. What will this code output?


 package main import "fmt" func main() { numbers := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} s1 := numbers[:5:8] fmt.Println(s1) s2 := numbers[::4] fmt.Println(s2) } 

Variants of answers:



Correct answer

Will not compile


3. Suppose you have the following hierarchy:


 src/ package_1/ p1_a.go p1_b.go package_2/ p2_a.go package_3/ p3_sh.go p3_bash.go p3_zsh.go 

Each top level module imports a low level module i. Package_1 imports package_2, and that in turn package_3. In each file the init function is declared. content:


 func init () { fmt.Println(< , : p1_a.go>) } 

What will be displayed on the screen?


Variants of answers:



Correct answer
      ,          (package_3)    (package_1) 

4. What are the constants equal to:


 const ( _ = iota Avito OLX LetGo Craiglist = iota eBay ) 

Variants of answers:



Correct answer

1,2,3,4,5


5. Experienced programmer Rob decided to add a module for parsing data to the standard library of his language. One of the parser modules will be a finite state machine, having entered the stream, Rob sketched the following code, what happens at the time of the first compilation?


 package main type state func(x int) state func start(x int) state { if x == 0 { return middle } else { return end } } func middle(_ int) state { return end } func end(_ int) state { return start } func main() { state := start(0) state = state(1) state = state(2) } 

Variants of answers:



Correct answer

Nothing will be output to stdout, but the process will complete successfully.


6. What will be the result of code execution?


 package main import ( "fmt" ) func test() (x int) { defer func () { x++ } () x = 1 return x } func main() { fmt.Println(test()) } 

Variants of answers:



Correct answer

2


7. Young Chinese programmer Jian Yang decided to master Go for his new geo-startup. Starting with the usual Hello World, he decided to slightly modify it in order to master the construction of the range ... What would be the result of the code?


 package main import ( "fmt" ) func main() { var rc, bc int str := "Hello 世界" for _ = range str { rc++ } for _ = range []byte(str){ bc++ } fmt.Println(rc, bc) } 

Variants of answers:



Correct answer

8 12




Have a nice day and wait for your answers in the comments. The next release of Avito Quiz is planned for PHP.


')

Source: https://habr.com/ru/post/349088/


All Articles