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)