BYU logo Computer Science

Practice Final

Try taking this practice test without looking at the answers. These questions are similar to what will be on the final.

Lists and Dictionaries

Question 1

What does the following code print?

def guestbook(guests):
    my_guestbook = {}
    for name, date in guests:
        if name not in my_guestbook:
            my_guestbook[name] = []
        my_guestbook[name].append(date)

    return my_guestbook


people = [("Elise", 2008),
          ("Elise", 2018),
          ("Aaron", 2018),
          ("Elise", 2018),
          ("Sheila", 2020),
          ("Elise", 2021),
          ("Sheila", 2021)]

result = guestbook(people)
print(result)

a.

[('Elise', 2008), ('Aaron', 2018), ('Sheila', 2020)]

b.

{'Elise': [2008, 2018, 2018, 2021], 'Aaron': [2018], 'Sheila': [2020, 2021]}

c.

[(2008, 'Elise'), (2018, 'Aaron'), (2020, 'Sheila')]

d.

{'Elise': [2008], 'Aaron': [2018], 'Sheila': [2021]}

Question 2

def goose(kids, finder, likes):
    index = 2
    while True:
        if kids[index] == likes[finder]:
            print("Goose! ", end=" ")
            print(f"{finder} tagged {kids[index]}.")
            return
        else:
            print("Duck! ", end=" ")
            index = (index + 1) % len(kids)


kids_in_circle = ["Emily", "Sam", "Dylan", "Josh", "Lily"]
finder = "Lucy"
likes = {
    "Dylan": "Lucy",
    "Emily": "Sam",
    "Lucy": "Sam",
    "Sam": "Lily",
    "Lily": "Dylan"
}
goose(kids_in_circle, finder, likes)

a.

Duck!  Goose!  Lucy tagged Sam.

b.

Duck!  Duck!  Duck! IndexError: list index out of range

c.

Duck!  Duck!  Duck!  Duck!  Goose!  Lucy tagged Sam.

d.

Duck! Duck! Duck! Duck! Duck! Duck! Duck!... Infinite loop

Grids

Question 3

What does the following code print?

def is_piece(grid, row, column):
    num_rows = len(grid)
    num_columns = len(grid[0])

    # check if the row is off the grid
    if row < 0 or row >= num_rows:
        return False

    # check if the column is off the grid
    if column < 0 or column >= num_columns:
        return False

    if grid[row][column] != ".":
        return True

    return False

def pawnCanCapture(grid):
    num_rows = len(grid)
    num_cols = len(grid[0])
    found_pawns = []

    # In chess, a black pawn can capture diagonally down-right or down-left
    # Find pawns that could capture
    for r in range(num_rows):
        for c in range(num_cols):
            if grid[r][c] == "p" and (is_piece(grid, r + 1, c - 1) or is_piece(grid, r + 1, c + 1)):
                found_pawns.append((r, c))

    return found_pawns


board = [['p', '.', 'p', 'r'],
         ['.', 'q', 'p', '.'],
         ['.', '.', 'p', '.']]

pawns = pawnCanCapture(board)

print(pawns)

a.

[(0, 0), (0, 2)]

b.

[(0, 0), (0, 2), (1, 2)]

c.

[(0, 0), (0, 2), (1, 2), (2,2)]

d.

[(0, 0), (0, 2), (0, 3), (1, 1), (1, 2), (2,2)]

Lists

Question 4

What does the following code print?

def mysteryFunction2(nums):
    modified = False
    for index in range(len(nums)):
        if nums[index] % 3 == 0:
            nums[index] = nums[index] // 3
            modified = True
    return modified


def mysteryFunction1(nums):
    changed = True
    while changed:
        changed = mysteryFunction2(nums)
    return nums


numbers = [12, 5, 7, 3, 6, 8, 25, 45, 74, 81]
result = mysteryFunction1(numbers)
print(result)

a.

[12, 5, 7, 3, 6, 8, 25, 45, 74, 81]

b.

[0, 5, 7, 1, 0, 8, 25, 0, 74, 0]

c.

[4, 5, 7, 1, 2, 8, 25, 15, 74, 27]

d.

[4, 5, 7, 1, 2, 8, 25, 5, 74, 1]

Strings

Question 5

What does the following code print?

def add_stars(text):
    new_text = ""
    for char in text:
        if char.isdigit():
            new_text += "*" * int(char)
        else:
            new_text += char
    return new_text


sentence = "*Hi*! I'd like 2 pears and 3 apples."
result = add_stars(sentence)
print(result)

a.

*Hi*! I'd like ** pears and *** apples.

b.

**Hi**! I'd like * pears and * apples.

c.

**Hi**! I'd like 2* pears and 3* apples.

d.

*Hi*! I'd like **2** pears and ***3*** apples.

Question 6

What does the following code print?

def add_stars(text):
    new_text = ""
    for char in text:
        if char.isdigit():
            new_text += "*" *  int(char)
        else:
            new_text += char
    return new_text


def mystery_function(text):
    new_text = ""
    for char in text:
        if char != "*":
            new_text += char
    return new_text


sentence = "*Hi*! I'd like 2 pears and 3 apples."
result = add_stars(sentence)
mystery = mystery_function(result)
print(mystery)

a.

*******

b.

Hi! I'd like 2 pears and 3 apples.

c.

Hi! I'd like  pears and  apples.

d.

*****