Python Foundations Practical Exam
60 minutes · 60 points · tracing, functions, lists, input validation, debugging, and small programs
Instructions
Time
60 min
Total
60 pts
Language
Python
No built-ins
no sum/max/sorted
Section A — Code Tracing
3 questions, 4 points each. Write the exact output and one short reason.
First maximum, not last maximum
4 ptsscores = [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)Swap plus history snapshot
4 ptsboard = ["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])Function return values
4 ptsdef 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.
Valid list indexes
3 ptsComplete both helpers. A valid index must be from 0 to the last index.
def valid_index(index, board): return index >= 0 and index < ____def last_index(board): return ____(board) - 1Swap two elements safely
3 ptsComplete the swap. The function should change board directly and does not need to return anything.
def swap_blocks(board, first, second): temp = board[first] board[first] = board[second] board[second] = ____Count matching positions
4 ptsReturn how many indexes have the same value in board and target.
def count_matches(board, target): count = 0 for i in range(____): if board[i] == target[i]: ____ return countParse a swap command
4 ptsFor valid input, return a pair of indexes and an empty error message. For invalid input, return None and a useful message.
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.
Single choice: copy or alias?
2 ptsWhich line creates a separate list snapshot?
- A. old = board
- B. old = board.copy()
- C. old = len(board)
- D. old = board == target
Single choice: helper function style
2 ptsWhich 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: ")
Single choice: exact output
2 ptsa = [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
Multiple choice: invalid commands
3 ptsFor board = ["R", "G", "B"], which commands should parse_move reject?
- A. "0 2"
- B. "2 2"
- C. "0 3"
- D. "left 2"
Multiple choice: good program design
3 ptsWhich 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.
Average score bug
4 ptsdef average_score(scores): total = 0 for i in range(len(scores)): total = scores[i] return total / len(scores)print(average_score([10, 20, 30]))Preview should not change the real board
4 ptsdef 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)Boundary check bug
4 ptsdef 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.
Explain a smart hint
4 ptsIn 4-6 sentences, explain how a function can find a useful swap without damaging the real board.
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.Write apply_swaps
6 ptsComplete the function. It must return a new list after applying all swaps. The original board must not change.
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"]