homework.wenqian.dev
← Back to index
Exam 12026-07-02

Python Foundations Practical Exam

60 minutes · 60 points · tracing, functions, lists, input validation, debugging, and small programs

Instructions

Scope: This exam covers the materials before the class tutorial: Python functions, loops, lists, indexes, copying, terminal game logic, and debugging. Do not use class, object, self, attributes, or methods.

Time

60 min

Total

60 pts

Language

Python

No built-ins

no sum/max/sorted

Teacher note: This exam is designed so a student can earn points by reading code carefully, not only by writing code from scratch.

Section A — Code Tracing

3 questions, 4 points each. Write the exact output and one short reason.

Q1

First maximum, not last maximum

4 pts
python
scores = [72, 91, 88, 91, 65]best = scores[0]best_index = 0for i in range(len(scores)):    if scores[i] > best:        best = scores[i]        best_index = iprint(best)print(best_index)
Q2

Swap plus history snapshot

4 pts
python
board = ["G", "R", "B", "Y"]history = []history.append(board.copy())first = 0second = 2temp = board[first]board[first] = board[second]board[second] = tempprint(board)print(history[0])
Q3

Function return values

4 pts
python
def bonus(score):    if score >= 90:        return score + 5    return scoretotal = 0for score in [88, 90, 72]:    total += bonus(score)print(total)

Section B — Fill And Complete

4 questions, 14 points total. Fill the blanks so the helper functions are correct.

Q4

Valid list indexes

3 pts

Complete both helpers. A valid index must be from 0 to the last index.

python
def valid_index(index, board):    return index >= 0 and index < ____def last_index(board):    return ____(board) - 1
Q5

Swap two elements safely

3 pts

Complete the swap. The function should change board directly and does not need to return anything.

python
def swap_blocks(board, first, second):    temp = board[first]    board[first] = board[second]    board[second] = ____
Q6

Count matching positions

4 pts

Return how many indexes have the same value in board and target.

python
def count_matches(board, target):    count = 0    for i in range(____):        if board[i] == target[i]:            ____    return count
Q7

Parse a swap command

4 pts

For valid input, return a pair of indexes and an empty error message. For invalid input, return None and a useful message.

python
def parse_move(command, board):    parts = command.split()    if len(parts) != 2:        return None, "Enter two indexes."    if not parts[0].isdigit() or not parts[1].isdigit():        return None, "Indexes must be numbers."    first = int(parts[0])    second = int(parts[1])    if first == second:        return None, "Choose two different indexes."    if first < 0 or first >= len(board) or second < 0 or second >= ____:        return None, "Index out of range."    return ____, ""

Section C — Choices

5 questions, 12 points total. Q8-Q10 are single choice. Q11-Q12 are multiple choice.

Q8

Single choice: copy or alias?

2 pts

Which line creates a separate list snapshot?

  • A. old = board
  • B. old = board.copy()
  • C. old = len(board)
  • D. old = board == target
Q9

Single choice: helper function style

2 pts

Which helper should return a value instead of printing directly?

  • A. print_panel(lines)
  • B. render_screen(level, board, moves)
  • C. is_solved(board, target)
  • D. input("Command: ")
Q10

Single choice: exact output

2 pts
python
a = [3, 1, 4]a[1] = a[0] + a[2]print(a)
  • A. [3, 1, 4]
  • B. [4, 1, 4]
  • C. [3, 7, 4]
  • D. 7
Q11

Multiple choice: invalid commands

3 pts

For board = ["R", "G", "B"], which commands should parse_move reject?

  • A. "0 2"
  • B. "2 2"
  • C. "0 3"
  • D. "left 2"
Q12

Multiple choice: good program design

3 pts

Which choices make a beginner program easier to test and debug?

  • A. Split repeated logic into small helper functions.
  • B. Write sample inputs and expected outputs before coding.
  • C. Change a list while testing a possible move, even if the real board should stay unchanged.
  • D. Use clear names such as board, target, index, and total.

Section D — Debugging

3 questions, 4 points each. For each bug: identify the bad line, explain the problem, and write the fix.

Q13

Average score bug

4 pts
python
def average_score(scores):    total = 0    for i in range(len(scores)):        total = scores[i]    return total / len(scores)print(average_score([10, 20, 30]))
Q14

Preview should not change the real board

4 pts
python
def swap_blocks(board, first, second):    temp = board[first]    board[first] = board[second]    board[second] = tempdef preview_swap(board, first, second):    test_board = board    swap_blocks(test_board, first, second)    return test_boardreal_board = ["R", "G", "B"]preview = preview_swap(real_board, 0, 2)print(preview)print(real_board)
Q15

Boundary check bug

4 pts
python
def get_value(a, index):    if index > len(a):        return "bad index"    return a[index]print(get_value([4, 8, 15], 3))

Section E — Short Answer And Coding

2 questions, 10 points total. Use clear steps and simple Python.

Q16

Explain a smart hint

4 pts

In 4-6 sentences, explain how a function can find a useful swap without damaging the real board.

python
board  = ["B", "R", "G", "Y"]target = ["R", "B", "G", "Y"]# A good hint should suggest swapping index 0 and index 1.# Explain how a function can discover that without changing board.
Q17

Write apply_swaps

6 pts

Complete the function. It must return a new list after applying all swaps. The original board must not change.

python
def apply_swaps(board, swaps):    # Input:    #   board - a list of colors, for example ["R", "G", "B"]    #   swaps - a list of pairs, for example [(0, 2), (1, 2)]    # Output:    #   Return a NEW list after applying every swap.    #   Do not change the original board.    passprint(apply_swaps(["R", "G", "B"], [(0, 2)]))# expected: ["B", "G", "R"]print(apply_swaps(["Y", "R", "B", "G"], [(0, 3), (1, 2)]))# expected: ["G", "B", "R", "Y"]