;;Practice exam: CMPU 101 - 01 Exam 1 Practice ; ; Problem 1: ; ; Name three types of Racket entities that always evaluate to ; themselves and give examples of each of the types. ; ; ; ; Problem 2: ; ; Write a predicate function called SMALL-VALUE? that takes in a ; data item (any valid Racket entity) as input and that returns ; #t if the input is a Boolean value or a number between 0 and ; 20 (including 0 and 20). For full credit, your solution ; should not use an if or a cond. If you have trouble writing ; a solution with no decision statement, try writing the ; function with a decision statement first and it might give ; you some insight into writing it without if or cond. ; ; > (small-value? 7) ; #t ; ; > (small-value? #f) ; #t ; ; > (small-value? 'foo) ; #f ; ; ; Problem 3: ; ; Design a Scheme function that converts Euros to US ; Dollars. Since exchange rates change daily, you should ; make your code as readable and maintainable as possible ; by using a constant definition for the exchange rate. ; ; Be sure to follow the Design Recipe to get full credit for ; your solution. ; ; ; ; Problem 4(a): ; ; Evaluate the following programs (a and b) step by step (as ; if you were the Stepper), underlining the portion of the ; expression in each step to be evaluated in the next step, ; and separating each step with an arrow (->). Circle any ; parts of the functions that are not evaluated when the given ; function call is made. ; ; Function definition: ; ; (define my-abs ; (lambda (x) ; (cond ; [(> x 0) x] ; [(< x 0) (* -1 x)] ; [else 0]))) ; ; Function call: ; (my-abs (- 4 10)) ; ; (b): ; ; Function definition: ; ; (define score ; (lambda (x) ; (cond ; [(>= x 90) "A"] ; [(>= x 80) "B"] ; [(>= x 70) "C"] ; [(>= x 60) "D"] ; [else "F"]))) ; ; Function call: ; (score (- 94 20)) ; ; ; Problem 5: ; ; The expression (+ 8 2) evaluates to the value 10. It is a ; compound expression made up of three primitive values. For ; this problem, write five other Racket expressions whose ; values are also the number 10: ; ; * A single primitive value. ; ; * Another compound expression made up of three primitive ; values. ; ; * A compound expression made up of four primitive values. ; ; * A compound expression made up of one primitive value ; and two compound subexpressions. ; ; * Any other kind of expression. ; ; ; Problem 6: ; ; Describe what, if anything is wrong with the following ; function definitions: ; ; (a) (define area ; (lambda (sqr) ; (sqr sqr))) ; ; Would this definition cause an error? Why or why not? ; ; (b) (define func1 ; (lambda (f (+ 3 x) y) ; (* x y))) ; ; Would this definition cause an error? Why or why not? ; ; (c) (define func2 ; (lambda (z x y z) ; (* x y))) ; ; Would this definition cause an error? Why or why not? ; ; (d) (define func3 ; (lambda (z w y x) ; (add1 x))) ; ; Would this definition cause an error? Why or why not? ; ; ; PROBLEM 7: ; ; Develop a function called smallest-of-3 that consumes three ; numbers and returns the smallest of them. ; ; Note: You are not allowed to use the built-in min function in ; your solution, use decision statements instead. ; ; ; PROBLEM 8: ; ; Develop a function called smart-add that consumes 2 arguments ; and adds them. The trick is that the arguments may be either ; numbers (like 18) or strings of digits (like "18"). Your ; function should be able to handle both types for either input ; argument. ; ; HINT: There are 2 parameters in this function, each of which ; could be either of 2 types, so you'll need at least 4 examples. ; ; ; PROBLEM 9 (VERSION 1): ; ; Develop a function called roman-value that consumes 1 quoted ; symbol as an argument and produces a number that is the decimal ; version of the given Roman numeral. You should make sure your ; function works for roman numerals up to 1,000 ('i, 'v, 'x, 'l, ; 'c, 'd, and 'm). If any symbol other than one of the 7 Roman ; numerals is entered, you should return the quoted symboln'huh?. ; ; ; PROBLEM 9 (VERSION 2): ; ; Rewrite the following function without using the if special ; form and without including the literal boolean values #t and ; #f in the definition: ; ; (define in-range? ; (lambda (x) ; (if (> x -3) ; (if (<= x 4) ; (if (> x 2) ; #t ; #f)) ; (if (< x -5) ; #f #t)))) ; ; ; ; PROBLEM 10: ; ; Use the method shown in class to convert the following infix ; expressions to prefix notation. Do not simplify or remove ; operators. ; ; (a) (x - 32) * (5/9) ; ; ; (b) (x + 3y) * (x - y) ; ; ; (c) 4 + 3 * 7 - 5 / (3 + 4) + 6 ; ; ; ; PROBLEM 11: ; ; Show the global environment entries created by the following 5 ; definitions when they are executed in DrRacket: ; ; (define U 35) ; (define V 17) ; (define W (* U V)) ; (define X (+ V W)) ; (define f (lambda (a b) (+ a (* b 2)))) ; ; ; Show the result of typing the name of each entry in the ; Interactions window. ; ; ; ; PROBLEM 12: ; ; Given particular character sequences (words),indicate ; whether the word is a special form, a built-in function, ; or neither. ; ; If you answer special form, indicate how the form is ; evaluated and give an example of a valid use of the form, ; including the return value (or void if a side-effect ; is the only thing returned). ; ; If you answer built-in function, give an example of invoking ; the function on valid arguments and indicate the output for ; your example. ; ; If you think the given word is an undefined name, type ; "undefined". ; ; (a) typo ; ; ; (b) cond ; ; ; (c) string-append ; ; ; (d) my-func ; ; ; (e) begin ; ;