diff --git a/11-if/questions/1-comparison-operators.md b/11-if/questions/1-comparison-operators.md new file mode 100644 index 0000000..0b47cd2 --- /dev/null +++ b/11-if/questions/1-comparison-operators.md @@ -0,0 +1,135 @@ +## Which one below is not one of the equality operators of Go? +1. `==` +2. `!=` +3. `>` *CORRECT* + +> **3:** That's the greater operator. It checks whether a ordered value is greater than the other or not. + + +## Which one below is not one of the comparison operators of Go? +1. `>` +2. `<=` +3. `==` *CORRECT* +4. `<` + +> **3:** That's the equal operator. In an expression, it checks whether a value (operand) is equal to another value (operand). + + +## Which one of these types is returned by the comparison operators? +1. int +2. byte +3. bool *CORRECT* +4. float64 + +> **3:** That's right. All the comparison operators return an untyped bool value (true or false). + + +## Which one of these cannot be used as an operand to ordering operators (`>`, `<`, `>=`, `<=`)? +1. int value +2. byte value +3. string value +4. bool value *CORRECT* +5. all of them + +> **1-2:** This is an ordered value, it can be used. +> +> **3:** String is an ordered value because it's a series of numbers. So, it can be used as an operand. +> +> **4:** That's right. A bool value is not an ordered value, so it cannot be used with ordering operators. + + +## Which one of these cannot be used as an operand to equality operators (`==`, `!=`)? +1. int value +2. byte value +3. string value +4. bool value +5. They all can be used *CORRECT* + +> **5:** That's right. Every **comparable value** can be used as an operand to equality operators. + + +## What does this code print? +```go +fmt.Println("go" != "go!") +fmt.Println("go" == "go!") +``` + +1. true true +2. true false *CORRECT* +3. false true +4. false false +5. error + +> **3-4:** Watch out for the exclamation mark at the end of the second string value. + + +## What does this code print? +```go +fmt.Println(1 == true) +``` + +1. true +2. 1 +3. false +4. 2 +5. error *CORRECT* + +> **5:** That's right. A numeric constant cannot be compared to a bool value. + + +## What does this code print? +```go +fmt.Println(2.9 > 2.9) +fmt.Println(2.9 <= 2.9) +``` + +1. true true +2. true false +3. false true *CORRECT* +4. false false +5. error + + +## What does this code print? +```go +fmt.Println(1 >= true) +fmt.Println(0 <= false) +``` + +1. true true +2. true false +3. false true +4. false false +5. error *CORRECT* + +> **5:** That's right. Bool values are not ordered values, so they cannot be compared using the comparison operators. + + +## How to fix this program without losing precision? +```go +package main +import "fmt" + +func main() { + weight, factor := 500, 1.5 + weight *= factor + + fmt.Println(weight) +} +``` + +1. It cannot be fixed +2. `weight *= float64(factor)` +3. `weight *= int(factor)` +4. `weight = float64(weight) * factor` +5. `weight = int(float64(weight) * factor)` *CORRECT* + +> **1:** It can be fixed. +> +> **2:** Type mismatch: weight is int. +> +> **3:** Lost precision: factor will be 1. +> +> **4:** Type mismatch: weight is int (cannot assign back). +> +> **5:** That's right. The result would be 750. \ No newline at end of file diff --git a/11-if/questions/2-logical-operators.md b/11-if/questions/2-logical-operators.md new file mode 100644 index 0000000..a997500 --- /dev/null +++ b/11-if/questions/2-logical-operators.md @@ -0,0 +1,120 @@ +## Which one below is not one of the logical operators of Go? +1. `||` +2. `!=` *CORRECT* +3. `!` +4. `&&` + +> **2:** That's the "not equal" operator. It's a comparison operator, not a logical operator. + + +## Which one of these types is returned by a logical operator? +1. int +2. byte +3. bool *CORRECT* +4. float64 + +> **3:** That's right. All the logical operators return an untyped bool value (true or false). + + +## Which one of these can be used as an operand to a logical operator? +1. int +2. byte +3. bool *CORRECT* +4. float64 + +> **3:** That's right. All the logical operators expect a bool value (or a bool expression that yields a bool value). + + +## What does this program print? +```go +package main +import "fmt" + +func main() { + var ( + on = true + off = !on + ) + + fmt.Println(!on && !off) + fmt.Println(!on || !off) +} +``` + +1. true true +2. true false +3. false true *CORRECT* +4. false false +5. error + +> **3:** `!on` is false. `!off` is true. So, `!on && !off` is false. And, `!on || !off` is true. + + +## What does this program print? +```go +package main +import "fmt" + +func main() { + on := 1 + fmt.Println(on == true) +} +``` + +1. true +2. false +3. error *CORRECT* + +> **3:** `on` is int, while `true` is a bool. So, there's a type mismatch error here. Go is not like other C based languages where `1` equals to `true`. + + +## What does this code print? +```go +// Note: "a" comes before "b" +a := "a" > "b" +b := "b" <= "c" +fmt.Println(a || b) +``` + +1. "a" +2. "b" +3. true *CORRECT* +4. false +5. error + +> **1-2:** Logical operators return a bool value only. + +> **3:** Order is like so: "a", "b", "c". So, `"a" > "b"` is false. `"b" <= "c"` is true. So, `a || b` is true. + +> **5:** There isn't an error. Strings are actually numbers, so, they're ordered and can be compared using the ordering operators. + + +## What does the following program print? +```go +// Let's say that there are two functions like this: +// +// `a()` which returns `true` and prints `"A"`. +// `b()` which returns `false` and prints `"B"`. +// +// Remember: Logical operators short-circuit. + +_ = b() && a() +_ = a() || b() +``` + +1. "BAAB" +2. "BA" +3. "ABBA" +4. "AB" + +> **1, 3:** Remember: Logical operators short-circuit. + +> **2:** That's right. +> +> In: `b() && a()`, `b()` returns false, so, logical AND operator short-circuits and doesn't call `a()`; so it prints: `"B"`. +> +> Then, in: `a() || b()`, `a()` returns true, so, logical OR operator short circuits and doesn't call `b()`; so it prints `"A"`. + +> **4:** Think again. + +Example program is [here](https://play.golang.org/p/JqEFVh5kOCE). \ No newline at end of file diff --git a/11-if/questions/3-if.md b/11-if/questions/3-if.md new file mode 100644 index 0000000..5b124ba --- /dev/null +++ b/11-if/questions/3-if.md @@ -0,0 +1,7 @@ +## What does control flow mean? +1. Changing the top-to-bottom execution of a program +2. Changing the left-to-right execution of a program +3. Controlling which statements are executed *CORRECT* + +> 1. You can't change that. +> 2. You can't change that. diff --git a/11-if/questions/questions.md b/11-if/questions/questions.md deleted file mode 100644 index cac63ae..0000000 --- a/11-if/questions/questions.md +++ /dev/null @@ -1,3 +0,0 @@ -## ? -* text *CORRECT* -* text diff --git a/13-loops/exercises/more-exercises.md b/13-loops/exercises/more-exercises.md index f2ba408..925fd5c 100644 --- a/13-loops/exercises/more-exercises.md +++ b/13-loops/exercises/more-exercises.md @@ -1,2 +1,2 @@ -**You can find more exercises here:** +**You can also find more exercises here:** * https://www.rosettacode.org/wiki/Category:Iteration \ No newline at end of file