← Back to the Introduction to Computer Science outline The Course Maker
← Introduction to Computer Science outline
Week 16 · Final practice exam

Final Practice Exam

Introduction to Computer Science Free sample edition
The student view of the 40-question practice exam: fresh variants of the course's quiz items, never the scored exam's. The answer key and the feedback ship only inside the course's .imscc.
  1. True or false: given the method header def fill(self, amount):, the call has to be written tank.fill(tank, 20), because the caller must supply a value for every parameter, self included.

    True or false.

    • True
    • False
  2. An illustrative bike-share tally adds up six days of rides. Which value appears on the first output line?

    rides = [14, 9, 22, 0, 17, 10]
    total = 0
    for count in rides:
        total = total + count
    print(total)
    print(total / len(rides))

    Select one.

    • 62
    • 12
    • 6
    • 72
  3. This version sets right to len(vals) rather than len(vals) - 1. What happens when it searches six illustrative locker numbers?

    def halving_search(vals, goal):
        left = 0
        right = len(vals)
        while left <= right:
            middle = left + (right - left) // 2
            if vals[middle] == goal:
                return middle
            elif vals[middle] < goal:
                left = middle + 1
            else:
                right = middle - 1
        return -1
    
    lockers = [5, 10, 15, 20, 25, 30]
    print(halving_search(lockers, 10))
    print(halving_search(lockers, 31))

    Select one.

    • It prints 1, then stops with IndexError
    • It prints 1, then −1
    • It prints 1, then 6
    • It stops with IndexError before printing anything
  4. A temperature converter (illustrative) wraps its work in a bare except. What does it print, or which exception type stops it?

    temps_f = [68, 77, 59]
    try:
        temps_c = []
        for deg in temps_f:
            temps_c.append((deg - 32) * 5 // 9)
        prnt(temps_c)
    except:
        print("no result")

    Select one.

    • no result
    • [20, 25, 15]
    • Nothing prints
    • NameError
  5. A binary search with while lo <= hi and one comparison per pass looks for 20 in [2, 6, 11, 17, 24, 29, 35, 42, 50, 58, 61]. Its fourth pass checks index 4, which holds 24. Since 24 is greater than 20, hi becomes 3 while lo is 4. What happens next?

    Select one.

    • The loop runs again with mid = 4, since lo and hi are adjacent
    • The function returns 4, because 24 is the closest value it saw
    • The loop never ends, because lo and hi keep trading places
    • The test lo <= hi is now False, so it returns (−1, 4)
  6. This traced recursive factorial prints a line each time a call begins. How many lines starting with enter does fact(3) produce?

    def fact(k, level=0):
        print("  " * level + "enter fact(" + str(k) + ")")
        if k == 0:
            print("  " * level + "give back 1")
            return 1
        result = k * fact(k - 1, level + 1)
        print("  " * level + "give back " + str(result))
        return result
    
    print(fact(3))

    Select one.

    • 4
    • 3
    • 6
    • 2
  7. Selection sort runs on five illustrative package weights. What is the first line this program prints?

    def pick_smallest_sort(values):
        values = values[:]
        for start in range(len(values)):
            low = start
            for k in range(start + 1, len(values)):
                if values[k] < values[low]:
                    low = k
            values[start], values[low] = values[low], values[start]
            print("round", start + 1, values)
        return values
    
    print(pick_smallest_sort([21, 9, 30, 5, 14]))

    Select one.

    • round 1 [9, 21, 30, 5, 14]
    • round 1 [5, 9, 30, 21, 14]
    • round 1 [5, 9, 14, 21, 30]
    • round 1 [5, 21, 9, 30, 14]
  8. A temperature log (illustrative) holds five daily highs. What does its second print line show?

    highs = [16, 23, 19, 26, 21]
    running = 0
    for h in highs:
        running = running + h
    print(running)
    print(running / len(highs))

    Select one.

    • 21.0
    • 21
    • 105.0
    • 5
  9. A supply list (illustrative) is typed with one comma and one space. What does this line print?

    print("pens,tape glue".split())

    Select one.

    • ['pens', 'tape', 'glue']
    • ['pens,tape', 'glue']
    • ['pens', 'tape glue']
    • ['pens,tape glue']
  10. An illustrative supply-closet dictionary records how many of each item remain. What is the result when this program asks for glue?

    stock = {"pens": 12, "tape": 3}
    print(stock["glue"])

    Select one.

    • None
    • KeyError
    • 0
    • 12
  11. This linear search looks for 25 in an illustrative list of ages that does not contain it. What is printed?

    def find_first(values, wanted):
        checks = 0
        for k in range(len(values)):
            checks = checks + 1
            if values[k] == wanted:
                return k, checks
        return -1, checks
    
    ages = [11, 16, 20, 27, 33, 38]
    print(find_first(ages, 25))

    Select one.

    • (−1, 6)
    • (−1, 0)
    • (None, 6)
    • (−1, 5)
  12. A survey form (illustrative) cleans up a typed reply. Select every line that appears in the output.

    reply = " Maybe"
    print(reply == "maybe")
    tidy = reply.lower().strip()
    print(tidy)
    print(tidy.upper())

    Select every option that applies.

    • maybe
    • True
    • False
    • Maybe
    • MAYBE
  13. An illustrative search loop looks for the smallest whole number whose square is larger than 90. What does it display?

    n = 1
    while True:
        if n * n > 90:
            print(n)
            break
        n = n + 1

    Select one.

    • 9
    • 100
    • 81
    • 10
  14. An illustrative savings jar starts with 20 dollars, and 15 dollars go in each week. What is the last line printed inside the loop?

    saved = 20
    for week in range(1, 6):
        saved = saved + 15
        print(week, saved)
    print(saved)

    Select one.

    • 6 110
    • 4 80
    • 5 110
    • 5 95
  15. An illustrative lap counter displays the lap number on each pass of this loop. How many lines appear before it ends?

    lap = 2
    while lap < 9:
        print(lap)
        lap += 1

    Select one.

    • 8
    • 7
    • 6
    • 9
  16. When this illustrative program runs, 15 appears and then a traceback follows. Which exception type is named at the bottom of that traceback?

    def triple(x):
        print(x * 3)
    
    total = triple(5)
    print(total + 2)

    Select one.

    • TypeError
    • NameError
    • ValueError
    • SyntaxError
  17. A cafe menu (illustrative) maps each item to its price. Predict the output, then run it to confirm.

    menu = {"tea": 3, "soup": 6}
    print(6 in menu)

    Select one.

    • False
    • True
    • None
    • KeyError
  18. Running this line shows False followed by True, since the number 0 counts as False and a string holding a single space is not empty.

    print(bool(0), bool(" "))

    True or false.

    • True
    • False
  19. An illustrative game asks for a lucky number, and the user types 7. The program keeps the typed value as a string. What appears on the screen?

    typed = "7"
    print(typed * 3)

    Select one.

    • 21
    • 777
    • 7
    • TypeError
  20. What output does this recursive launch countdown (illustrative) produce?

    def blastoff(n):
        if n == 0:
            print("Liftoff!")
            return
        print(n)
        blastoff(n - 1)
    
    blastoff(5)

    Select one.

    • Liftoff!, then 5, 4, 3, 2, 1, one per line
    • 5, 4, 3, 2, 1, one per line, and nothing else
    • 5, 4, 3, 2, 1, 0, then Liftoff!, one per line
    • 5, 4, 3, 2, 1, then Liftoff!, one per line
  21. An illustrative even-number printer was meant to show 2, 4, 6, 8 and 10, but it stops after 8. Which single change makes it show all five?

    n = 2
    while n < 10:
        print(n)
        n += 2

    Select one.

    • Swap the order of the two lines in the loop body.
    • Start n at 0 instead of 2.
    • Replace < with <= in the loop header.
    • Change n += 2 to n += 1.
  22. Two illustrative bike docks each get their own rental counter. What does this program print?

    class DockCounter:
        def __init__(self):
            self.rentals = 0
    
        def rent(self):
            self.rentals = self.rentals + 1
    
    north = DockCounter()
    south = DockCounter()
    south.rent()
    north.rent()
    south.rent()
    south.rent()
    print(north.rentals, south.rentals)

    Select one.

    • 1 3
    • 4 4
    • 3 1
    • 1 1
  23. A recursive product function (illustrative) multiplies down to its base case. What number does it print?

    def fact(k):
        if k == 0:
            return 1
        return k * fact(k - 1)
    
    print(fact(6))

    Select one.

    • 120
    • 0
    • 720
    • RecursionError
  24. A recursive version and a loop version of one sum (illustrative) are compared. Select every line the program prints.

    def total_rec(n):
        if n == 0:
            return 0
        return n + total_rec(n - 1)
    
    def total_loop(n):
        total = 0
        for k in range(1, n + 1):
            total = total + k
        return total
    
    print(total_rec(6))
    print(total_loop(6))
    print(total_rec(20))

    Select every option that applies.

    • 15
    • 21
    • 20
    • 210
    • 190
  25. A doorbell routine (illustrative) recurses to ring twice, and its result is printed. What is the last line of output?

    def ring(times):
        if times == 0:
            print("done")
            return
        print("ring")
        ring(times - 1)
    
    outcome = ring(2)
    print(outcome)

    Select one.

    • done
    • 2
    • 0
    • None
  26. An illustrative bake sale packs muffins into boxes of 12. Which number does the last line show?

    baked = 90
    sold_early = 15
    remaining = baked - sold_early
    print(remaining % 12)

    Select one.

    • 6
    • 3
    • 6.25
    • 0.25
  27. A slogan checker (illustrative) looks for two letters. What does it print?

    motto = "Code, test, repeat"
    print(motto.find("q"), motto.find("t"))

    Select one.

    • False 6
    • 0 6
    • None 6
    • −1 6
  28. An illustrative step-counter note is broken into words. Which list appears?

    entry = "walk 2: 6400 steps"
    pieces = entry.split()
    print(pieces)

    Select one.

    • ['walk', '2', '6400', 'steps']
    • ['walk 2: 6400 steps']
    • ['walk', ' 2:', ' 6400', ' steps']
    • ['walk', '2:', '6400', 'steps']
  29. A learner is reading a traceback for the first time. Which of these statements are accurate? Mark each accurate one.

    Select every option that applies.

    • A traceback's first line names the exception type, so read it from the top down
    • The exception type, such as NameError, sits on the last line of a traceback
    • When the parser rejects a program with a SyntaxError, none of its lines have run
    • A program that finishes with no traceback must be doing what it was meant to do
    • Output that appeared before a traceback shows the program had started running
  30. An illustrative swim timer keeps looping while laps < 12. When laps holds 12, the comparison laps < 12 gives True.

    True or false.

    • True
    • False
  31. A fruit stand's program (illustrative) adds three prices, one at a time. Which list of keys does it print?

    prices = {}
    prices["pear"] = 5
    prices["fig"] = 8
    prices["kiwi"] = 2
    print(list(prices))

    Select one.

    • ['fig', 'kiwi', 'pear']
    • ['kiwi', 'pear', 'fig']
    • ['pear', 'fig', 'kiwi']
    • ['kiwi', 'fig', 'pear']
  32. An illustrative bike-share dock compares its rider count with a limit. Which two lines does this program display?

    limit = 20
    riders = 20
    print(riders > limit)
    print(riders == 20)

    Select one.

    • False False
    • True False
    • False True
    • True True
  33. A chatbot says that print(10 - 4 - 3) shows 9, because Python handles the right-hand subtraction first. What should you do next?

    Select one.

    • Accept the answer, since the chatbot laid out each step of its reasoning for you
    • Type the line into a Python runner and check its output against the claim
    • Keep asking the chatbot the same question until two of its replies match exactly
    • Switch to another chatbot whose reply sounds more confident and more certain
  34. A lamp class (illustrative) is built, and then its method is called as shown. Which exception type ends the program?

    class Lamp:
        def __init__(self):
            self.on = False
    
        def switch(self):
            self.on = not self.on
    
    desk = Lamp()
    switch()

    Select one.

    • TypeError
    • NameError
    • AttributeError
    • It turns the desk lamp on and prints nothing
  35. An illustrative tally adds every whole number from 2 to 6 into an accumulator. Which total is displayed?

    total = 0
    n = 2
    while n <= 6:
        total += n
        n += 1
    print(total)

    Select one.

    • 20
    • 14
    • 21
    • 6
  36. An illustrative data-plan function returns what is left, and the call splits that amount into blocks of 60. What is printed?

    def remaining(total, used):
        return total - used
    
    print(remaining(500, 120) // 60)

    Select one.

    • 6
    • 380
    • 8
    • 7
  37. A water-tank class (illustrative) has a fill method. What does this program print?

    class Tank:
        def __init__(self):
            self.litres = 15
    
        def fill(self, amount):
            litres = self.litres + amount
    
    tank = Tank()
    tank.fill(20)
    print(tank.litres)

    Select one.

    • 15
    • 35
    • AttributeError
    • None
  38. An illustrative locker-code program joins two digits that are stored as text. What shows on the screen?

    print("6" + "1")

    Select one.

    • 7
    • 61
    • 6 1
    • "6" + "1"
  39. A cheer program (illustrative) swaps one word for another. What line does it print?

    chant = "go team go"
    print(chant.replace("go", "win"))

    Select one.

    • win team win
    • win team go
    • go team go
    • go team win
  40. An illustrative elevator program updates its floor and then displays it on every pass. What output does it produce?

    floor = 1
    while floor < 4:
        floor = floor + 1
        print(floor)

    Select one.

    • 2, then 3, then 4
    • 1, then 2, then 3
    • 2, then 3
    • 2, then 3, then 4, then 5

Built with The Course Maker · samples.thecoursemaker.com