Docsity

CS 106A Final Examination, Exams of Nursing

Toggle sidebar
bg1
CS 106A Final Examination
Course: CS 106A — Programming Methodology
Exam Type: Comprehensive Final Examination
Total Points: 150
Suggested Time: 180 minutes
Language: Python 3
Coverage: Full course, weighted toward post-midterm material
The CS 106A final is comprehensive but emphasizes later-course topics: file reading and
splitting, dictionaries and counting algorithms, nested dictionaries, tuples,
comprehensions, lambda functions, custom sorting, and classes. Earlier topics—
including strings, lists, loops, functions, and 2-D lists—may still appear because later
material builds on them.
Table of Contents
1. Instructions
2. Problem 1 — Short Answer
3. Problem 2 — Lists and Tuples
4. Problem 3 — Strings and Dictionaries
5. Problem 4 — File Processing
6. Problem 5 — Nested Dictionaries
7. Problem 6 — Sorting and Lambda
8. Problem 7 — Classes
9. Reference Solutions
Instructions
Write complete, syntactically valid Python 3 code.
You may assume standard Python built-ins and common string, list, dictionary, and file
operations.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS 106A Final Examination and more Exams Nursing in PDF only on Docsity!

CS 106A Final Examination

Course: CS 106A — Programming Methodology

Exam Type: Comprehensive Final Examination

Total Points: 150

Suggested Time: 180 minutes

Language: Python 3

Coverage: Full course, weighted toward post-midterm material

The CS 106A final is comprehensive but emphasizes later-course topics: file reading and

splitting, dictionaries and counting algorithms, nested dictionaries, tuples,

comprehensions, lambda functions, custom sorting, and classes. Earlier topics—

including strings, lists, loops, functions, and 2-D lists—may still appear because later

material builds on them.

Table of Contents

1. Instructions

2. Problem 1 — Short Answer

3. Problem 2 — Lists and Tuples

4. Problem 3 — Strings and Dictionaries

5. Problem 4 — File Processing

6. Problem 5 — Nested Dictionaries

7. Problem 6 — Sorting and Lambda

8. Problem 7 — Classes

9. Reference Solutions

Instructions

 Write complete, syntactically valid Python 3 code.

 You may assume standard Python built-ins and common string, list, dictionary, and file

operations.

 Unless otherwise stated, functions should return the requested result rather than print

it.

 Do not modify an input data structure unless the problem explicitly asks for in-place

modification.

 You may define helper functions.

 Point values indicate approximate suggested time.

Problem 1 — Short Answer

20 points

Part A — 5 points

What is the output of the following code?

python d = {"a": 1, "b": 2, "c": 3} print (len(d), d["b"]) Part B — 5 points

What is the output of the following code?

python s = "apple,banana,cherry" parts = s.split(",") print (parts[1], len(parts)) Part C — 5 points

What is the output of the following code?

python pairs = [(3, 1), (1, 5), (2, 2)] pairs.sort(key= lambda pair: pair[1]) print (pairs) Part D — 5 points

What is the output of the following code?

python nums = [1, 2, 3, 4] result = [n * n for n in nums if n % 2 == 0] print (result)

{"A": 2, "a": 1, "B": 1, "b": 1} Problem 4 — File Processing

20 points

Write a function:

python def word_counts(filename):

The function reads a text file and returns a dictionary mapping each word to the number

of times it appears in the file.

Rules:

 Split each line into words using whitespace.

 Treat words as case-sensitive.

 Preserve duplicate words in the count.

 Do not print anything.

Example: if data.txt contains:

text apple banana apple cherry banana

then:

python word_counts("data.txt")

should return:

python {"apple": 2, "banana": 2, "cherry": 1} Problem 5 — Nested Dictionaries

25 points

Write a function:

python def add_score(scores, name, category, points):

The parameter scores is a dictionary mapping student names to dictionaries. Each

inner dictionary maps a category name, such as "homework" or "exam", to a list of

integer scores.

Your function should add points to the appropriate list inside the appropriate student’s

inner dictionary.

If the student or category does not yet exist, create it.

The function should modify scores in place and return scores.

Example:

python scores = { "Ada": {"homework": [90, 85]} } add_score(scores, "Ada", "exam", 78) add_score(scores, "Grace", "homework", 95)

After the calls, scores should be:

python { "Ada": {"homework": [90, 85], "exam": [78]}, "Grace": {"homework": [95]} } Problem 6 — Sorting and Lambda

20 points

Write a function:

python def sort_by_second(pairs):

The parameter pairs is a list of two-item tuples, where each tuple has the form:

python (name, score)

Return a new list containing the same tuples sorted from highest score to lowest score.

If two tuples have the same score, preserve their original relative order.

Example:

python

Part B text banana 3

split(",") divides the string at commas into three parts; index 1 is "banana".

Part C text [(3, 1), (2, 2), (1, 5)]

The lambda sorts tuples by their second element in increasing order.

Part D text [4, 16]

The comprehension keeps only even numbers, then squares them.

These question types reflect the CS 106A final’s emphasis on dictionaries, file/string

processing, tuples, comprehensions, lambda functions, and custom sorting.

Problem 2 — Lists and Tuples python def str_and_length(lst): result = [] for s in lst: result.append([s, len(s)]) return result

Why this works: The function builds a new outer list and appends a new two-item list

for each original string. It does not mutate lst. This mirrors the list-and-tuple

transformation format used in official CS 106A final practice materials.

Problem 3 — Character Counts python def char_counts(s): counts = {} for ch in s:

if ch not in counts: counts[ch] = 0 counts[ch] += 1 return counts

Why this works: This is the standard dictionary-count algorithm: initialize missing

keys to zero, then increment the count for every character. Case sensitivity is preserved

because Python dictionary keys distinguish "A" from "a". Dictionary counting is

explicitly identified as a major post-midterm CS 106A topic.

Problem 4 — File Word Counts python def word_counts(filename): counts = {} with open(filename, "r") as f: for line in f: words = line.split() for word in words: if word not in counts: counts[word] = 0 counts[word] += 1 return counts

Why this works: The with open(...) pattern safely opens and closes the file.

Iterating over the file processes one line at a time, split() breaks each line into

whitespace-separated words, and the dictionary-count pattern accumulates frequencies.

File reading, splitting, and dictionary counting are central final-exam topics.

Problem 5 — Nested Dictionaries python def add_score(scores, name, category, points): if name not in scores: scores[name] = {} if category not in scores[name]: scores[name][category] = [] scores[name][category].append(points) return scores