From bd538864f3ff7db5705be85e21f4ca18fb10beed Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Mon, 22 Oct 2018 19:15:32 +0300 Subject: [PATCH] add: questions for the loops section --- 13-loops/questions/01-loops.md | 215 ++++++++++++++++++++ 13-loops/questions/02-randomization.md | 90 ++++++++ 13-loops/questions/03-labeled-statements.md | 172 ++++++++++++++++ 13-loops/questions/questions.md | 3 - 4 files changed, 477 insertions(+), 3 deletions(-) create mode 100644 13-loops/questions/01-loops.md create mode 100644 13-loops/questions/02-randomization.md create mode 100644 13-loops/questions/03-labeled-statements.md delete mode 100644 13-loops/questions/questions.md diff --git a/13-loops/questions/01-loops.md b/13-loops/questions/01-loops.md new file mode 100644 index 0000000..57897a0 --- /dev/null +++ b/13-loops/questions/01-loops.md @@ -0,0 +1,215 @@ +## Which one of these is a valid loop statement in Go? +1. while +2. forever +3. until +4. for *CORRECT* + +> **4:** Correct. There is only one loop statement in Go. + + +## What does this code print? +```go +for i := 3; i > 0; i-- { + fmt.Println(i) +} +``` +1. 3 2 1 *CORRECT* +2. 1 2 3 +3. 0 1 2 +4. 2 1 0 + + +## What does this code print? +```go +for i := 3; i > 0; { + i-- + fmt.Println(i) +} +``` +1. 3 2 1 +2. 1 2 3 +3. 0 1 2 +4. 2 1 0 *CORRECT* + + +## What does this code print? +```go +for i := 3; ; { + if i <= 0 { + break + } + + i-- + fmt.Println(i) +} +``` +1. 3 2 1 +2. 1 2 3 +3. 0 1 2 +4. 2 1 0 *CORRECT* + + +## What does this code print? +```go +for i := 2; i <= 9; i++ { + if i % 3 != 0 { + continue + } + + fmt.Println(i) +} +``` +1. 3 6 9 *CORRECT* +2. 9 6 3 +3. 2 3 6 9 +4. 2 3 4 5 6 7 8 9 + + +## How can you simplify this code? +```go +for ; true ; { + // ... +} +``` +1. ```go + for true { + } + ``` +2. ```go + for true; { + } + ``` +3. ```go + for { + } + ``` + *CORRECT* +4. ```go + for ; true { + } + ``` + + +## What does this code print? +Let's say that you run the program like this: +```bash +go run main.go go is awesome +``` + +```go +for i, v := range os.Args { + fmt.Println(i+1, v) +} +``` +1. ``` + 1 go + 2 is + 3 awesome + ``` + *CORRECT* +2. ``` + go + is + awesome + ``` +3. ``` + 0 go + 1 is + 2 awesome + ``` +4. ``` + 1 + 2 + 3 + ``` + + +## What does this code print? +Let's say that you run the program like this: +```bash +go run main.go go is awesome +``` + +```go +for i := range os.Args { + fmt.Println(i+1) +} +``` +1. ``` + 1 go + 2 is + 3 awesome + ``` +2. ``` + go + is + awesome + ``` +3. ``` + 0 go + 1 is + 2 awesome + ``` +4. ``` + 1 + 2 + 3 + ``` + *CORRECT* + + +## What does this code print? +Let's say that you run the program like this: +```bash +go run main.go go is awesome +``` + +```go +for _, v := range os.Args { + fmt.Println(v) +} +``` +1. ``` + 1 go + 2 is + 3 awesome + ``` +2. ``` + go + is + awesome + ``` + *CORRECT* +3. ``` + 0 go + 1 is + 2 awesome + ``` +4. ``` + 1 + 2 + 3 + ``` + + +## What does this code print? +Let's say that you run the program like this: +```bash +go run main.go go is awesome +``` + +```go +var i int + +for range os.Args { + i++ +} + +fmt.Println(i) +``` +1. go is awesome +2. 1 2 3 +3. 2 +4. 3 *CORRECT* + +> **4:** As you can see, you can also use a for range statement for counting things. \ No newline at end of file diff --git a/13-loops/questions/02-randomization.md b/13-loops/questions/02-randomization.md new file mode 100644 index 0000000..f8c56b8 --- /dev/null +++ b/13-loops/questions/02-randomization.md @@ -0,0 +1,90 @@ +## What's pseudorandom number generation? +1. Numbers appear to be randomly generated but in reality they are not *CORRECT* +2. Generating random numbers according to the physical laws +3. Generating pseudo even and odd numbers + +> **1:** Computers are deterministic machines. They can't generate truly random numbers (unlike actual physical processes). + + +## What's a seed number? +1. Exchanging of random numbers between two computers +2. It's used to getting a random number between 0 and the seed number +3. It's used initialize a pseduorandom number generator *CORRECT* + + +## Which package is used to generate pseudorandom numbers in Go? +1. pseudorand +2. rand *CORRECT* +3. random +4. randomizer + + +## What does [0, 5) mean? +1. A range of numbers between 0 and 5 (excluding 5) *CORRECT* +2. A range of numbers between 0 and 5 (including 5) +3. Just 0 and 5 +4. Just 0 and 4 + +> **1:** Right. The square-brace means: "inclusion". The parenthesis means: "exclusion". So, [0, 5] means: 0, 1, 2, 3, 4. It's called the "mathematical interval notation". + + +## Why this function call would not work? +```go +rand.Intn(0) +``` +1. First you should seed it +2. It expects two arguments +3. Intn works within a range of [0, 0). So, it doesn't make sense to include 0 and not include 0 at the same time. *CORRECT* + +> **1:** That's not the cause of this error. You don't always have to seed it. +> **2:** No, it does not. + + +## What does this program print? +Note that, each seed number below returns pseudorandom numbers as these: + +``` +Seed: 0 + 3 3 6 8 4 1 9 3 6 6 + +Seed: 1 + 1 1 9 3 2 4 7 6 6 6 + +Seed: 2 + 10 1 2 2 0 6 4 1 0 5 +``` + +Here's the program: + +```go +package main + +import ( + "fmt" + "math/rand" +) + +func main() { + for i := 0; i < 3; i++ { + rand.Seed(int64(i)) + fmt.Print(rand.Intn(11), " ") + fmt.Print(rand.Intn(11), " ") + } +} +``` +1. 3 1 10 3 1 1 +2. 3 6 1 6 10 5 +3. 1 10 1 1 3 3 +4. 3 3 1 1 10 1 *CORRECT* + +> **4:** The numbers are determined depending on the seed number. So, this loop, seeds the pseudorandom generator with 0, 1, and 2 respectively. +> +> And, after each seed, it calls Intn twice to generate two random numbers. +> +> So, if you look at the result, 3 3 is the first two numbers of Seed: 0. 1 1 for Seed: 1. And, 10 1 for Seed: 2. + + +## What you should do if you want the pseudorandom generator to produce random numbers each time you run your program? +1. You need to seed it like this: rand.Seed(rand.Random) +2. You need to seed it like this: rand.Seed(time.Now().UnixNano()) *CORRECT* +3. You need to seed it like this: rand.Seed(time.Now()) \ No newline at end of file diff --git a/13-loops/questions/03-labeled-statements.md b/13-loops/questions/03-labeled-statements.md new file mode 100644 index 0000000..f4a127a --- /dev/null +++ b/13-loops/questions/03-labeled-statements.md @@ -0,0 +1,172 @@ +## Which scope a label belongs to? +1. To the scope of the statement that it is in +2. To the body of the function that it is in *CORRECT* +3. To the package scope that it is in + +> **1:** Labels do not belong to statement scopes unlike other names like variable or constant names. +> +> **2:** They can be used throughout the function, even before their declaration inside the function. This also what makes goto statement jump to any label within a function. + + +## Which statement that the word label labels? +```go +for range words { +words: + for range letters { + // ... + } +} +``` +1. The first loop +2. The second, nested loop *CORRECT* +3. All the loops + +> **2:** A label can only label one statement at a time. + + +## Will this loop terminate after the break? +```go +package main + +func main() { +main: + for { + switch "A" { + case "A": + break // <- here! + case "B": + continue main + } + } +} +``` +1. No, the break will only terminate the switch but the loop will continue *CORRECT* +2. Yes, the break will terminate the loop +3. Yes, the break will terminate the switch + +> **1:** Yep. This is an unlabeled break. So, it breaks the closest statement, which in here, it's that switch statement. And, since it only breaks the switch, the loop will keep continue. +> +> **3:** Yep. However, why would that kill the loop as well? + + +## Will this loop ever terminate? +```go +package main + +func main() { + flag := "A" + +main: + for { + switch flag { + case "A": + flag = "B" + break + case "B": + break main + } + } +} + +``` +1. No, this loop will loop to infinity +2. Yes, the first break will terminate the loop +3. Yes, the second break will terminate the loop *CORRECT* + +> **2:** No it does not but it helps. +> +> **3:** Yep. Do you know why? Because, at first, first case will match, and it will set the flag to "B". So, in the next loop step, the 2nd case will be hit, then, it will break from the loop, because the loop is labeled with the main label. + + +## What the first break below does? + +Note that, in this program, there's an infinite loop. + +```go +package main + +func main() { + for { + switcher: + switch 1 { + case 1: + switch 2 { + case 2: + break switcher + } + } + break + } +} +``` +1. It breaks from the 2nd switch causing the program will loop indefinitely +2. It breaks from the 2nd switch and then the 2nd break will terminate the loop +3. It breaks from the 1st switch and then the 2nd break will terminate the loop *CORRECT* + +> **1:** There's another break after the switch, so the loop will end immediately. +> +> **2:** It doesn't break the 2nd switch. The label labels the 1st switch. + + +## What's wrong with this program? + +```go +package main + +func main() { + for { + switcher: + switch { + case true: + switch { + case false: + continue switcher + } + } + } +} +``` +1. continue statement can only continue a loop *CORRECT* +2. continue statement cannot be used within a switch statement +3. It will loop to infinity + +> **1:** Yes! Here, the switcher label labels the first switch statement. So, it's a switch label. And, then it tries to jump to that label using a continue. However, a continue statement can only be used to jump to a loop label. +> +> **2:** It can be used to continue for the next loop step. +> +> **3:** Yes, but that's not the real problem. + + +## Which one of these programs will terminate? + +I mean: When you run it, which one will quit. Some of the codes here will indefinitely run. + +1. ```go + func main() { + start: goto exit + exit : fmt.Println("exiting") + goto start + } + ``` +2. ```go + func main() { + exit: fmt.Println("exiting") + goto exit + } + ``` + +3. ```go + func main() { + goto exit + start : goto getout + exit : goto start + getout: fmt.Println("exiting") + } + ``` + *CORRECT* + +> **1:** In the start label: "goto exit" sends the execution to the exit label. In the exit label: "goto start" sends the execution back to the start label. So, it's an infinite loop. The program will never terminate. +> +> **2:** In the exit label: "goto exit" sends the execution back to the exit label. So, it's an infinite loop. The program will never terminate. +> +> **3:** "goto exit" sends the execution to the exit label. In the exit label: "goto start" sends the execution to the start label. In the start label: "goto getout" sends the execution to the getout label. And, since the getout label is the last statement of the main function, the program will terminate there. \ No newline at end of file diff --git a/13-loops/questions/questions.md b/13-loops/questions/questions.md deleted file mode 100644 index cac63ae..0000000 --- a/13-loops/questions/questions.md +++ /dev/null @@ -1,3 +0,0 @@ -## ? -* text *CORRECT* -* text