Practice Exam
1. What does (car '(1 2 3)) return in Racket?
A. '(1 2 3)
B. '(2 3)
C. 1
D. 3
Correct Answer: C. 1
Rationale: car returns the first element of a non-empty list.
2. What does (cdr '(1 2 3)) return?
A. '(1)
B. '(2 3)
C. 1
D. 3
Correct Answer: B. '(2 3)
Rationale: cdr returns the list without its first element.
3. What does (cons '(1) '(2)) return?
A. '(1 (2))
B. '((1 2))
C. '(1 2)
D. '((1) 2)
Correct Answer: D. '((1) 2)
Rationale: cons adds its first argument as a single new element at the front of the
second list, so the list (1) becomes the first element.
,4. What is the result of (map (lambda (x) (* x x)) '(1 2 3))?
A. '(1 4 9)
B. '(2 4 6)
C. '(1 2 3)
D. 14
Correct Answer: A. '(1 4 9)
Rationale: map applies the function to each element and returns a new list of the
results.
5. What does (filter odd? '(1 2 3 4 5)) return?
A. '(1 2 3 4 5)
B. 9
C. '(2 4)
D. '(1 3 5)
Correct Answer: D. '(1 3 5)
Rationale: filter keeps only the elements for which the predicate returns true.
6. What does (foldl cons '() '(1 2 3)) return?
A. '(1 2 3)
B. '()
C. '(3 2 1)
D. '(1 (2 (3)))
Correct Answer: C. '(3 2 1)
Rationale: foldl processes left to right: (cons 1 '()) gives (1), then (cons 2 '(1)) gives
(2 1), then (3 2 1).
7. What value does (let* ([x 2] [y (+ x 1)]) y) return?
A. 3
B. 2
C. 1
D. An error because x is undefined in y
Correct Answer: A. 3
Rationale: let* binds sequentially, so y can use the value of x defined just before it.
, 8. Given (define (f n) (if (= n 0) 0 (+ n (f (- n 1))))), what is (f
4)?
A. 0
B. 10
C. 24
D. 4
Correct Answer: B. 10
Rationale: f(4) = 4 + 3 + 2 + 1 + 0 = 10.
9. Which statement best describes a tail-recursive function?
A. It uses a loop internally
B. The recursive call is the last operation performed, so no work remains after it
returns
C. It calls itself twice
D. It never has a base case
Correct Answer: B. The recursive call is the last operation performed, so no
work remains after it returns
Rationale: Tail calls allow the implementation to reuse the stack frame, avoiding
stack growth.
10. What does ((lambda (x y) (+ x y)) 3 4) evaluate to?
A. A function
B. An error
C. '(3 4)
D. 7
Correct Answer: D. 7
Rationale: The anonymous function is applied immediately to 3 and 4, returning 7.