Sample CS 135 questions
  1. The Design Recipe and BSL Expressions · Complete the code · 2 marks

    The function below should swap the two halves of a string of even length. Fill in the two blanks with the same expression; do not change any other code.

    ;; String -> String
    ;; produce s with its first and second half swapped; s has even length
    (check-expect (swap-halves "abcd") "cdab")
    (check-expect (swap-halves "") "")
    
    (define (swap-halves s)
      (string-append (substring s ____________ (string-length s))
                     (substring s 0 ____________)))
    Show solution

    Both blanks hold (quotient (string-length s) 2), the index where the second half starts.

  2. Compound Data and Structures · Write code · 4 marks

    A hotel records a stay as either nothing or a structure.

    (define-struct booking (guest nights rate))
    ;; A Booking is one of:
    ;;  - false
    ;;  - (make-booking String Natural Number)
    ;; interp. no booking, or a guest's name, the number of nights, and the
    ;;         price per night in dollars

    Design bill, which produces the amount owed: 0 for no booking, otherwise the nights times the rate, less 10%10\% of that total if the stay is at least 7 nights. Give the signature, the purpose, at least three check-expects that cover every case and the boundary, and the function.

    Show solution
    ;; Booking -> Number
    ;; produce the amount owed: nights times rate, less 10% for 7+ nights
    (check-expect (bill false) 0)
    (check-expect (bill (make-booking "Al" 6 100)) 600)
    (check-expect (bill (make-booking "Bo" 7 100)) 630)
    
    (define (bill b)
      (cond [(false? b) 0]
            [(< (booking-nights b) 7)
             (* (booking-nights b) (booking-rate b))]
            [else
             (- (* (booking-nights b) (booking-rate b))
                (/ (* (booking-nights b) (booking-rate b)) 10))]))
  3. Natural Numbers and Lists of Structures · Write code · 4 marks

    Design the function double-prices, which produces a list of lines like the one it consumes, but with the price of every line doubled. Give the signature, the purpose, two check-expects and the function body.

    (define-struct line (item price qty))
    ;; A Line is (make-line String Number Natural)
    ;; A ListOfLine is one of:
    ;;  - empty
    ;;  - (cons Line ListOfLine)
    Show solution
    ;; ListOfLine -> ListOfLine
    ;; produce lol with the price of every line doubled
    (check-expect (double-prices empty) empty)
    (check-expect (double-prices (list (make-line "pen" 3 4)
                                       (make-line "ink" 5 2)))
                  (list (make-line "pen" 6 4) (make-line "ink" 10 2)))
    
    (define (double-prices lol)
      (cond [(empty? lol) empty]
            [else (cons (make-line (line-item (first lol))
                                   (* 2 (line-price (first lol)))
                                   (line-qty (first lol)))
                        (double-prices (rest lol)))]))
  4. Mutual Recursion and Trees · Multiple choice · 2 marks

    A BST is a BT in which, for every node, every key in its left subtree is smaller than its key and every key in its right subtree is larger. To keep the trees short, the values are all "" and these helpers build the nodes.

    (define (lf k) (make-node k "" false false))
    (define (n k l r) (make-node k "" l r))

    Which one of the following trees is a BST?

    1. (n 5 (n 2 (lf 1) (lf 6)) (lf 9))

    2. (n 5 (n 2 (lf 1) (lf 4)) (n 8 (lf 9) (lf 10)))

    3. (n 5 (n 2 (lf 1) (lf 4)) (n 8 (lf 6) (lf 9)))

    4. (n 5 (n 2 (lf 1) (lf 3)) (n 8 (lf 4) (lf 9)))

    Show solution

    Answer: (C)

    (C). In (A) the key 6 is in the left subtree of 5 and in (D) the key 4 is in the right subtree of 5, although each is correctly placed relative to its parent: the invariant concerns the whole subtree, not just the children. In (B) the key 9 is the left child of 8.

  5. Generative Recursion, Search and Graphs · Trace the code · 2 marks

    Each recursive call of gcd-of replaces the problem by a new one. Fill in the arguments of every call made while evaluating (gcd-of 84 36), and the final value.

    (define (gcd-of a b)
      (cond [(= b 0) a]
            [else (gcd-of b (remainder a b))]))
    callab
    18436
    2      
    3      

    The value of (gcd-of 84 36) is    .

    Show solution

    Answer: 36; 12; 12; 0; 12

    Call 2 is (gcd-of 36 12), since (remainder 84 36) is 12. Call 3 is (gcd-of 12 0), since (remainder 36 12) is 0. There b is 0, so the answer is a, which is 12.

About CS 135 practice exams

CS 135 practice exams

Designing Functional Programs · University of Waterloo (Waterloo, UWaterloo, UW) · also written CS135

Make a free, printable CS 135 practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 450 original questions and skips the ones you have already seen.

Looking for CS 135 past exams?

Start with the past exams and tests your instructor or the Waterloo library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CS 135 practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.

Topics covered

  • The Design Recipe and BSL Expressions · 50 questions
  • Atomic Data, Intervals and Itemizations · 50 questions
  • Compound Data and Structures · 50 questions
  • Lists and Self-Referential Data · 50 questions
  • Natural Numbers and Lists of Structures · 50 questions
  • Abstraction, Higher-Order Functions and Local · 50 questions
  • Mutual Recursion and Trees · 50 questions
  • Accumulators and Tail Recursion · 50 questions
  • Generative Recursion, Search and Graphs · 50 questions

More Waterloo courses

questionbank.dev is independent and not affiliated with University of Waterloo. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.