3 / 23 // 2"a" * 3bool(0)a = [10, 20, 30, 40, 50], write the value of:
a[1:4]sum(a) / len(a)a[::-1]d:
f(4) return?
[7, 3, 9, 2, 8, 1]. Show the list after EACH iteration of the outer loop.57 in the sorted list:
lo, hi, and mid at each step. Conclude by stating the index returned.palindrome(s) that returns True if a string is the same forwards and backwards (case-sensitive). You may not use any loops or string-reversal built-ins. Then trace palindrome("level").Library with attributes and methods to manage a small library. Include AT LEAST: an __init__, an add_book(title) method, a borrow(title) method (returns True on success or False if not on shelf), a return_book(title) method, and a docstring describing the class. Use sensible data structures (e.g., a dict mapping title to count, or two sets for "on shelf" and "borrowed"). Comment thoroughly.orders.csv contains lines of the form customer,item,price, e.g.:
[{"name":..., "gpa":...}, ...]. Write a function top_n(students, n) that returns the names of the n students with the HIGHEST GPAs, in descending order. Then briefly justify your choice of algorithm and state its complexity.Course with attributes name (str) and marks (list of floats).add_mark(m) that appends a mark in the range 0–100 (raise ValueError otherwise).average() returning the mean.highest() returning the maximum mark (or None if no marks yet).rank(courses) that takes a list of Course objects and returns them sorted by average descending.Q1. Loop runs i=0..3. Iter 0: y=5,x=4. Iter 1: y=9,x=3. Iter 2: y=12,x=2. Iter 3: y=14,x=1. Output: 1 14.
Q2. (1) 1.5 (float). (2) 1 (int). (3) "aaa" (str). (4) False (bool).
Q3. (1) [20, 30, 40]. (2) 30.0. (3) [50, 40, 30, 20, 10].
Q4. {'M': 1, 'I': 4, 'S': 4, 'P': 2}.
Q5. f(4) = f(3)+f(2) = (f(2)+f(1)) + (f(1)+f(0)) = ((f(1)+f(0))+1) + (1+0) = ((1+0)+1)+1 = 3. (This is Fibonacci F(4) where F(0)=0, F(1)=1.)
Q6. Inside add, total = x creates a local total shadowing the global. Output: 5 then 0.
Q7. O(n), O(log n), O(n²), O(n log n), O(n).
Q8. A regression test re-runs old test cases after a code change to confirm previously-correct behaviour still works. Important because fixes can introduce new bugs in unrelated places.
Q9. Requirements gathering → Design → Implementation → Testing → Deployment → Maintenance.
Q10. [7,3,9,2,8,1] → [3,7,9,2,8,1] → [3,7,9,2,8,1] → [2,3,7,9,8,1] → [2,3,7,8,9,1] → [1,2,3,7,8,9].
Q11. Step 1: lo=0, hi=10, mid=5 (35 < 57; lo=6). Step 2: lo=6, hi=10, mid=8 (57 = 57). Return index 8.
Q12.
Q13. Bugs: (1) range(nums) should be nums (iterating list, not int). (2) = should be ==. (3) total + n doesn't store anything — should be total += n. (4) Counting bug: count = 1 initially makes the average wrong; should be 0 (and protect division-by-zero).
Q14. Sample:
Q15. Merge sort: O(n log n) best/avg/worst, O(n) extra space, stable. Quicksort: O(n log n) avg, O(n²) worst (bad pivot), O(log n) recursion stack, generally not stable, but in-place. Prefer merge sort when stability matters or worst-case guarantees are needed (e.g., real-time). Prefer quicksort when memory is tight and average performance dominates (most general-purpose libraries).
Q16. Free response. Award marks for: (a) clear position; (b) named real-world harm (e.g., bias in COMPAS, autonomous-vehicle deaths, generative-AI defamation); (c) named stakeholder (data subject, regulator, end-user); (d) industry comparison (e.g., automotive recall law, FDA pharmaceutical liability).
Q17. Sample issues: (1) Function name x is uninformative → rename filter_above. (2) Parameter L is uninformative → rename lst or numbers. (3) Inconsistent / unusual indentation (1 then 2 spaces) → PEP 8: 4 spaces. (4) range(0, len(L)) can be for x in lst: — iterate values directly. (5) Missing docstring. (6) Single-line if/append is hard to read. (7) No tests/comments.
Q18. Sample:
Q19.
Q20.
Q21. Sample:
| Level | Description | % |
|---|---|---|
| 4 | Thorough, insightful, demonstrates a high degree of effectiveness | 80–100% |
| 3 | Considerable effectiveness — provincial standard | 70–79% |
| 2 | Some effectiveness | 60–69% |
| 1 | Limited effectiveness | 50–59% |
| R | Insufficient — has not demonstrated the required knowledge or skills | <50% |