Quiz and exam info will appear on this page.
1 - Quiz 2 sample problems
You are getting the first edition of all these pages. Please let me know if you find an error!
In class Oct 8
Study key terms from slides and labs.
Study Knowledge Checks from labs.
Emphasis on:
- testing
- I will not ask you to calculate coverage by hand.
- control flow graphs (CFGs)
- coding conventions and code documentation.
- I do not expect you to memorize PEP8 rules or docstring formats.
- testing
Repeat questions from previous quiz are on the table. No CLI.
You may use your own written notes during the quiz.
Multiple choice, fill-in-the-blank, long answer.
Sample CFG and Testing problems
You will be asked to draw CFGs and write test cases that cover all program paths.
Disclaimer: You will have other knowledge check questions beyond these types of questions.
Sample 1
|
|
- Draw the CFG for this code using the conventions from class.
- List the unique program paths.
- Add
assert
statements to the following test case that exercise all unique program paths.def test_is_prime(): # Your code here.
Solution
CFG for is_prime()
unique program paths
The unique edges in the path are highlighted. You do not need to highlight the unique edges on the quiz.
- (1, 2, 3)
- (1, 2, 4, 7)
- (1, 2, 4, 5, 6)
- (1, 2, 4, 5, 4, 7) or (1, 2, 4, 5, 4, 5, 6)
test case
def test_is_prime():
# Some paths can be exercised with multiple input values.
# The goal is to exercise all program paths.
assert is_prime(1) == True # tests path (1, 2, 3)
assert is_prime(2) == True # path (1, 2, 4, 7)
assert is_prime(4) == False # path (1, 2, 4, 5, 6)
assert is_prime(5) == True # path (1, 2, 4, 5, 4, 7)
Sample 2
|
|
- Draw the CFG for this code using the conventions from class.
- List the unique program paths.
- Add
assert
statements to the following test case that exercise all unique program paths.def test_generate_fibonacci(): # Your code here.
Solution
CFG for generate_fibonacci()
unique program paths
The unique edges in the path are highlighted. You do not need to highlight the unique edges on the quiz.
- (14, 15, 16)
- (14, 15, 18-21, 23, 29)
- (14, 15, 18-21, 23, 24-27, 23, 29)
test case
def test_generate_fibonnaci():
# Some paths can be exercised with multiple input values.
# The goal is to exercise all program paths.
assert generate_fibonnaci(0) == "Error: Number of terms must be a positive integer" # tests path (14, 15, 16)
assert generate_fibonnaci(1) == [1] # path (14, 15, 18-21, 23, 29)
assert generate_fibonnaci(6) == [1, 1, 2, 3, 5, 8] # path (14, 15, 18-21, 23, 24-27, 23, 29)
Sample 3
|
|
- Draw the CFG for this code using the conventions from class.
- List the unique program paths.
- Add
assert
statements to the following test case that exercise all unique program paths.def test_factorial(): # Your code here.
Solution
CFG for factorial()
unique program paths
The unique edges in the path are highlighted. You do not need to highlight the unique edges on the quiz.
- (36, 38, 39)
- (36, 38, 42, 43, 45)
- (36, 38, 42, 43, 44, 43, 45)
test case
def test_factorial():
# Some paths can be exercised with multiple input values.
# The goal is to exercise all program paths.
assert factorial("Alice") == "Error: Number of terms must be a positive integer" # tests path(36, 38, 39)
assert factorial(-1) == "Error: Number of terms must be a positive integer" # also tests path(36, 38, 39)
assert factorial(0) == 1 # tests path(36, 38, 42, 43, 45)
assert factorial(5) == 120 # tests path(36, 38, 42, 43, 44, 43, 45)
2 - Quiz 3 - Design Rules and Version Control
In class November 19
- Study key terms from slides and labs.
- Study your notes, the examples, and the slides (Weeks 7-9 on Canvas) about the Low-Level Design Rules. You will be asked to critique code for design rule violations.
- Study Knowledge Checks from all the Version Control labs.
- Sample questions for examining a Git Repo’s state or critiquing code for design rule violations are provided below.
- You may use your own written notes during the quiz.
- Multiple choice, fill-in-the-blank, long answer.
Sample Git Repo state questions
Git analysis - sample 1
Consider the following depictions of a Git repository’s state:
Repo state
Conceptual branch history
- What is the current active branch?
- How many versions will running the command
git log
show? - How many versions will running the command
git log
show after runninggit checkout main
? - Suppose the following changes and commands are made in order:
git checkout main
- Add some lines to
app.py
. git add .
git commit -m "added new functions"
Update both the Repo state diagram and the Conceptual branch history diagram to reflect the changes.
Solution for Git sample 1
- The current active branch is
feature-1
, as indicated by theHEAD
git log
runs in the active branch by default, andfeature-1
has 3 versions.git log
in themain
branch will shown only the first version.
Git analysis - sample 2
Consider the repo state above as the starting point for each question. Briefly explain the impact of running the following commands in terms of the versions, head, and the workspace files.
git reset .
git reset --hard HEAD
.git reset --hard HEAD~1
.
Solution for Git sample 2
- The changes to
main.py
andhello.py
will be unstaged (removed from the Index), but will still be present in the workspace. - We discard any changes since the most recent version:
- All changes will be unstaged.
main.py
will be replaced with its version fromb424cc
.hello.py
will be as-is in the workspace because it is untracked, meaning it has not yet been added to version control. You can tell thathello.py
is untracked because it does not exist in any version. Runninggit status
would tell you that it is untracked.- The
HEAD
will continue to point to versionb424cc
.
- All tracked files in the workspace and the local repository are reset to version
8356ea
. Essentially, we are resetting to the point before the most recent version.README.md
will be removed from the workspace.main.py
will be replaced with its version from8356ea
.- Version
b424cc
will be deleted from the Local Repository. hello.py
will be as-is in the workspace because it is untracked.- The
HEAD
will now point to version8356ea
.
Sample low-level design rule questions
Design critique - sample 1
Consider this simple Python program:
|
|
Identify which of the 6 low-level design rules this program violates and why. Suggest how these violations might be addressed.
Solution for Sample 1
You could potentially argue for other design rule violations, or a violation in rule of the ones below. In my mind, the rules below are clearly violated.
- Violation of “Separate input/output logic from business logic”:
- Issue: The function
process_order
mixes calculating the total cost with printing output directly to the console. - Solution: Separate the calculation logic into a distinct function and handle input/output operations elsewhere.
- Issue: The function
- Violation of “Avoid magic literals”:
- Issue: The prices 1.0, 0.5, and 2.0 are hardcoded directly into the function, making them “magic” numbers without context.
- Solution: Use named constants for these values. They could be placed into a dictionary, for example.
- Violation of “Handle errors at the lowest sensible level, and re-raise/re-throw them otherwise”:
- Issue: The function checks for positive quantities but does not raise a specific error.
- Solution: Use exception handling to raise a specific error when quantity is non-positive.
Design critique - sample 2
Consider this Python code:
|
|
Solution for sample 2
You could potentially argue for other design rule violations, or a violation in rule of the ones below. In my mind, the rules below are clearly violated.
- Violation of “Single Responsibility Rule”:
- Issue: The OrderProcessor class handles multiple responsibilities: it maintains the list of orders, processes them, and prints order summaries.
- Solution: Split responsibilities into dedicated classes, such as
OrderManager
for managing orders,OrderProcessor
for processing, etc.
- Violation of “DRY (Don’t Repeat Yourself)”:
- Issue: The calculation
total = order["quantity"] * order["price"]
is repeated across multiple methods (process_orders
,print_summary
,find_order
, andupdate_order
). - Solution: Create a helper method to calculate the total for a given order to avoid repeating this logic.
- Issue: The calculation
- Violation of “Avoid Magic Literals”:
- Issue: The product-related attributes such as “product”, “quantity”, and “price” are string literals used throughout the class. If any of these strings are mistyped, or if there is a change in the attribute names, the program may fail without easy traceability.
- Solution: Use constants or, better yet, use a dedicated Order class with attributes for product, quantity, and price to encapsulate these values.
- Violation of “Handle errors at the lowest sensible level, and re-raise/re-throw them otherwise”:
- Issue: When removing or updating orders, the methods simply print messages if the order is not found rather than handling the error in a structured manner (e.g., raising an exception or returning an error code).
- Solution: Raise specific exceptions like OrderNotFoundError if an order is not found, and allow higher-level methods to decide how to handle them.
- Violation of “Raise specific errors and define custom errors if needed”:
- Issue: No custom error handling is used for operations that could logically fail (e.g., removing or finding non-existent orders). The use of print statements for error messages is not a robust approach.
- Solution: Define custom exceptions such as OrderNotFoundError or InvalidOrderError to handle various conditions gracefully.
3 - Final Exam
Where and When
- Where: Congdon Hall 2055
- When: Thursday, December 12, 3pm-6pm
- The exam is not designed to be 3 hours long, but you will have the entire 3 hours at your disposal.
- Other
- Please eat a snack before the exam. Eating in the lab is not permitted.
- Bring a water bottle.
- You may listen to music using headphones/earbuds at a volume that does not disturb others.
Exam Format and Rules
- A mix of multiple choice, fill-in-the-blank, and long answer questions.
- You will not be programming in Visual Studio Code, but you may be asked to write or edit code snippets by hand.
- You may use your own hand-written notes, class worksheets (but not Git or CLI cheatsheets), and scrap paper. No other resources are permitted.
- Honor Code violations on the final exam result in a course grade of F.
- Failure to submit the final exam results in a course grade of F.
Content
- Describing the phases of the Software Lifecycle and how they are organized into Software Process Models.
- Describing Operating Systems concepts.
- Writing a good Problem Statement when provided a high-level description of the program goals.
- Creating a control flow graph for a given function.
- Writing or analyzing unit tests for a given function, including:
assert
statements- computing line coverage by hand
- testing if exceptions are raised using
pytest
functions - Assignment 2 - Unit Testing
- Assignment 3, Part B and Part C
- Applying the six code-level design rules.
- Describing version control concepts and applying them to given scenarios:
- Creating a visual diagram like this one showing the state of Git repos (a) after running a set of Git commands, or (b) based on
git log
output. - Entering the appropriate
git
command or sequence of commands for common scenarios, such as creating a new version, staging changes, creating a new branch, merging, and interacting with the remote repository. - Explaining the proper process of merging branches, including in the presence of merge conflicts.
- Resolving merge conflicts in a given block of code while preserving functionality.
- Creating a visual diagram like this one showing the state of Git repos (a) after running a set of Git commands, or (b) based on
Review the three quizzes: