Monday, March 22, 2010

A pop quiz

Partway through the term the professor decides to give the students a pop quiz.

“The quiz is contained in the file quiz.scm which you can find in the usual location. For those of you that prefer hardcopy, I have a printout for you.

“I know that some of you” (and here the professor glared at Student A) “seem to have issues with the way I present code, so you should assume that this code is to be entered into an unmodified R5RS Scheme interpreter.

“You have two hours.

;; Question 1:
;; What does (fib 4) evaluate to?

;; Question 2:
;; What is domain and range of the EVEN? procedure?

;; Question 3:
;; How many top-level forms are in this file?

;; Question 4:
;; Make a list of the free variables in this file.

(define (even? x)
  (or (zero? x)
      (odd? (- x 1))))

(define (odd? x)
  (and (not (zero? x))
       (even? (- x 1))))

(define (fib n)
  (if (< n 2)
      (+ (fib (- n 1)) 
         (fib (- n 2)))))

;; Sanity check
(= (- (fib 100) (fib 99)) (fib 98))

;; Question 5:
;; Are either of the following two forms an infinite loop?
;; Make an informal argument to support your answer.

(define (loop1 n)
  (if (positive? n)
      (loop1 (- n 1))
      (loop1 n)))

(define (loop2 n)
  (if (negative? n)
      (loop2 (+ n 1))
Meta-questions for readers of this blog:
  1. Is this a reasonable quiz? If it is too difficult for first-year students, is there a year at which it would be appropriate or even too simple?
  2. Is two hours enough time to answer the questions?
  3. Are there any loopholes that will allow Student A to object?