Practice Quiz 2 Solutions
Practice Quiz 2 Solutions
Answer: Number
2. ‘[]
3. [+ 1 “2”]
Answer: Exception
Explanation: you can’t add a number to a string.
5. [+ 1 2
[if false “2” 3]]
Answer: number
6. [define [my-form]
[define f [make Form]]
form]
[my-form]
Note: assume the user has already executed [using System.Windows.Forms], so that the
Form class is available.
Answer: Exception
Explanation: there’s no variable defined called form.
7. [define x 0]
[define [mystery]
[with x = 5
[x ← “test”]]
x]
[mystery]
Answer: number
Explanation: there are two variables called x here, the global variable x, which is a
number, and the local variable x, which starts as a number and is changed to a
string. The assignment statement, which changes x to a number occurs within the
scope of the local variable, so the local variable is changed, leaving the global
variable unchanged. However, the x which appears at the end of mystery is
outside the with, and so outside the scope of the local variable x. It therefore
refers to the global variable, which is still a number.
9. [define [my-form]
[define result 0]
[define f [make Form]]
[define button1 [make Button
Click: [ignore ignore →
[result ← “Button1”]]]]
[define button2 [make Button
Top: 40
Click: [ignore ignore →
[result ← false]]]]
[f.Controls.Add button1]
[f.Controls.Add button2]
[Application.Run f]
result]
[my-form]
Note: Assume this procedure runs properly and that the user presses button1 followed by
the close box. Why type of value does it return?
Answer: String
Explanation: when the user presses button1, the Click method for button1 is run,
which changes result to a string. After the form is closed, Application.Run returns
and the procedure returns the value of result, which is now a string.
1. [fold +
[map sublis → [fold + sublis]
[list [list 1 2 3] [list 4 5 6] [list 7 8 9]]]]
Desired output: The sum of all the numbers in all the sublists, i.e. 6+15+24=45.
Error: ArgumentTypeException in call to fold (list argument was a procedure rather than a
list).
Explanation: by omitting the brackets around the procedure, it fooled the system
into thinking the map expression was one big procedure with map and sublis as its
inputs
Desired output: a list of all the files in directory whose pathnames contain name.
Note: assume string-contains? returns true if and only if the string file contains
the string name. Also, remember that the Add method adds an element to the
end of an ArrayList.
Error: None. It runs fine, but it doesn’t return any value at all.
Answer: add result to the end.
Explanation: the code is fine. It properly builds the result list up, it just doesn’t return
its value.
Desired output
Answer: add [list <- [rest list]] to the end of the while loop
Explanation: the while loop never changes list and so it can never
become empty.
Glossary
Predicates
[and booleans …]
[or booleans …]
Returns true if all/any of the booleans are true.
[not boolean]
Returns true if argument is false, or false if argument is true.
[= object1 object2]
Returns true if object1 and object2 are the same.
Arithmetic
[+ numbers …]
[+ vectors …]
[+ colors …]
Returns the sum of the given numbers, colors, or vectors. Numbers, colors, and vectors cannot
be mixed.
[− number number]
[− vector vector]
[− color color]
Returns the different of two numbers, vectors, or colors.
[− number]
[− vector]
Returns the number/vector times -1.
[× number number/color/vector]
[× number/color/vector number]
[ ∕ number/color/vector number]
Returns the specified multiple (or quotient) of the specified number, color, or vector, and the
specified number.
[abs number]
Returns the absolute value of number, i.e. the number with the sign erased.
Colors
[color number number number]
Returns a color with the specified amounts of red, green, and blue, respectively. If any number is
less that 0, it is treated as 0. If any number is greater than 255, it is treated as 255.
[color name]
Returns the color with the specified name. The name should be a string, such as “red” or “green”.
Strings are typed in double quotes: “”.
[red color], [green color], [blue color] (or: color.R, color.G, color.B, respectively)
Returns the amount of red, green, or blue light in the specified color as a number from 0-255.
Pictures
All the following procedures return pictures. Boxes, ellipses, groups, etc. are particular kinds of
pictures. However, bitmaps can not be given as inputs to the procedures in this section.
[group pictures …]
Returns a picture comprised of all the pictures passed as arguments. The pictures can be boxes,
ellipses, lines, groups, or any other object returned by procedures in this section. When called
with no arguments, group returns a blank picture.
count-1. The resulting picture contains all the pictures returned by all the calls to picture-
procedure. Thus picture-procedure must take a number as input and return a picture as its result.
Lists
[empty? list]
Returns true if list has no elements.
[list elements …]
Returns a list with all the specified elements, in order. If called with no arguments, it returns the
empty list.
[rest list]
Returns all but the first item of the list