--- title: Drill 2 Solutions, Fall 2020 tags: Drills-F20, 2020 --- # Drill 2 Solutions, Fall 2020 ## Question 1 Which of the following evaluates to `true`? Note that `<>` means "not equals". [ ] `true and false` [ ] `true or false` [ ] `true == true` [ ] `false <> false` ::: spoiler Explanation `true and false` evaluates to `false` because *all* of the elements in the boolean expression need to evaluate to `true` in order for `and` to return `true`, and the expression `false` does not. `true or false` evaluates to `true` because *any* of the elements in the boolean expression can evaluate to `true` in order for `or` to return `true`, and the expression `true` does. `true == true` evaluates to `true` because both the expression on the left-hand side of the `==` and the expression on the right-hand side of the `==` are the same. `false <> false` evaluates to `false` because the expression on the left-hand side of the `<>` and the expression on the right-hand side of the `<>` are not different from each other. ::: ## Question 2 Consider the following function definition: ``` fun contains-z(word :: String) -> Boolean: string-contains(word, "z") end ``` What does the term function *body* refer to in this definition? ( ) word ( ) string-contains(word, "z") ( ) string-contains ( ) Everything between fun and end ( ) Everything including fun and end ::: spoiler Explanation string-contains(word, "z") The body of a function is all of the code that *isn't* in the header. In Pyret, this is everything starting from when we indent our code. Explanations for incorrect answers: `word`: This is the *input* or *argument* or *parameter* to the function, and it is defined in the header and used in the body. `string-contains`: This is a built-in `String` function which is *part* of the function body, but by itself it is not the whole body. Everything between `fun` and `end`: This contains the whole body, but the definition after `fun` (`contains-z(word :: String) -> Boolean:`) is called the header and includes type annotations. This is not part of the body. Everything including `fun` and `end`: This is the entire function, of which the body is only the indented part. ::: ## Question 3 Is this code an expression? ``` if x == 10: "ten" else: "some other number" end ``` ( ) Yes ( ) No ::: spoiler Explanation Yes `if` expressions are a type of expression because they can evaluate to a value. Here, that value is either the String `"ten"` or the String `"some other number"`. ::: ## Question 4 What inputs will return "password!" from this function? ``` fun nested(a :: Number, b :: Number) -> String: if a > b: if ((a - b) <= 5) or ((a - b) > 10): "not the password" else: "password!" end else: "not the password" end end ``` ( ) a = 0, b = 0 ( ) a = 0, b = 5 ( ) a = 10, b = 5 ( ) a = 10, b = 0 :::spoiler a = 10, b = 0 When `a` is 0 and `b` is 0, then `a > b` evaluates to `false` and `nested` returns `"not the password"`. When `a` is 0 and `b` is 5, then `a > b` evaluates to `false` and `nested` returns `"not the password"`. When `a` is 10 and `b` is 5, then `a > b` evaluates to `true`. `(a - b) <= 5` evaluates to `true`, so the `or` expression automatically evaluates to `true` (it doesn't even need to check if `(a - b) > 10` because an `or` expression only needs one component to evaluate to `true` for it to be `true`. Therefore, `nested` returns `"not the password"`. When `a` is 10 and `b` is 0, then `a > b` evaluates to `true`. `(a - b) <= 5` evaluates to `false` and `(a - b) > 10` evaluates to `false`, so `((a - b) <= 5) or ((a - b) > 10)` evaluates to `false` and `nested` returns `password!`. ::: ## Question 5 The definitions window contains this piece of code: ``` fun f(x :: Number) -> String: if x > 0: "Positive" else if x == 0: 0 else: "Negative" end end ``` When will this code fail? ( ) Upon clicking the "Run" button. ( ) Upon running `f(3)`. ( ) Upon running `f(-3)`. ( ) Upon running `f(0)`. ( ) It won't fail. :::spoiler Explanation Upon running `f(0)`. When you click "Run", the program checks for *syntax* errors, meaning anything that looks out of place. It does not try to evaluate the function, though, meaning it would miss any issues like this until you ran it with a certain input. When you run `f(3)`, `x > 0` evaluates to `true` and `Positive` is returned, so no error occurs. When you run `f(-3)`, `x > 0` evaluates to `false`, so the program moves onto check if `x == 0`, which is also false, so it moves on to the `else` and returns `Negative`. No error occurs. When you run `f(0)`, `x > 0` evaluates to `false`, so the program moves onto check if `x == 0`, which is `true`. The function tries to return `0`, but this is not of the type `String`, and the type annotation tells us this function is supposed to return a `String`. The code will fail. ::: ## Question 6 Assume you had the following contents in the definitions window: --- ``` include image size = 25 triangle(size, "solid", "yellow") ``` -------------------------------------- Use this setup to answer the following questions. The questions will ask you how Pyret would respond to several expressions typed in the interactions window. How would Pyret respond if you entered the following at the interactions prompt BEFORE hitting Run for the first time? ``` >>> size ``` ( ) nothing would happen ( ) Pyret would produce an error ( ) A circle ( ) A triangle ( ) 25 ( ) 35 ( ) 50 :::spoiler Explanation Pyret would produce an error Until you press Run, this name is not defined ::: ## Question 7 How would Pyret respond if you entered the following at the interactions prompt BEFORE hitting Run for the first time? ``` >>> size = 25 ``` ( ) nothing would happen ( ) Pyret would produce an error ( ) A circle ( ) A triangle ( ) 25 ( ) 35 ( ) 50 :::spoiler Explanation nothing would happen Pyret will produce a new prompt without producing any answer. There will be no error ::: ## Question 8 How would Pyret respond if you entered the following at the interactions prompt AFTER hitting Run for the first time? ``` >>> size + 10 ``` ( ) nothing would happen ( ) Pyret would produce an error ( ) A circle ( ) A triangle ( ) 25 ( ) 35 ( ) 50 :::spoiler Explanation 35 `size` is defined, so Pyret adds its value (25) to 10. ::: ## Question 9 How would Pyret respond if you entered the following at the interactions prompt AFTER hitting Run for the first time? ``` >>> size = 25 ``` ( ) nothing would happen ( ) Pyret would produce an error ( ) A circle ( ) A triangle ( ) 25 ( ) 35 ( ) 50 :::spoiler Explanation Pyret would produce an error `size` is already defined, so Pyret will complain that you've tried to redefine it ::: ## Question 10 How would Pyret respond if you entered the following at the interactions prompt AFTER hitting Run for the first time? ``` >>> triangle(size, "solid", "yellow") ``` ( ) nothing would happen ( ) Pyret would produce an error ( ) A circle ( ) A triangle ( ) 25 ( ) 35 ( ) 50 :::spoiler Explanation A triangle Running the same expression again in the interactions window is fine. The restriction is against changing the value associated with a name. ::: ## Question 11 How would Pyret respond if you entered the following at the interactions prompt AFTER hitting Run for the first time? ``` >>> yellow = triangle(size, "solid", "yellow") ``` ( ) nothing would happen ( ) Pyret would produce an error ( ) A circle ( ) A triangle ( ) 25 ( ) 35 ( ) 50 :::spoiler Explanation nothing would happen There is nothing wrong with using yellow to name a value. ::: ## Question 12 How would Pyret respond if you entered the following at the interactions prompt AFTER hitting Run for the first time? ``` >>> new-size = 10 >>> num-max(size, new-size) ``` ( ) nothing would happen ( ) Pyret would produce an error ( ) A circle ( ) A triangle ( ) 25 ( ) 35 ( ) 50 :::spoiler Explanation 25 You can add new definitions at the interactions prompt, then use existing and new defined names in expressions :::