fina_study_guide_answers
fina_study_guide_answers
Course 179869
2) x = 75
a) Y = x / 2
b) Z = x // 2
No they are not the same, the ‘/’ operator performs a float division (ie can return a float-point
number), therefore Y is 37.5. The ‘//’ performs integer division, returns the floor value
discarding the fractional part and only returns whole integers therefore Z is 37
2) macarons = 12
The output will be: “almost enough for me” this is because of the <= ( less than or equal to 12)
Study Guide Final
Course 179869
answer = 2
for outer in range(1, 8, 2):
for inner in range(1, outer):
answer += outer
print(answer)
1. answer is initialized to 2.
2. The outer loop iterates over the range from 1 to 7 (exclusive), with a step size of 2. So, outer
takes values 1, 3, 5.
3. For each value of outer, the inner loop iterates over the range from 1 to outer (exclusive).
4. Inside the inner loop, answer is incremented by the value of outer for each iteration.
5. After all iterations, the value of answer is printed.
For outer = 1, the inner loop doesn't execute because range(1, 1) is an empty range.
For outer = 3, the inner loop executes once, adding 3 to answer.
For outer = 5, the inner loop executes three times, adding 5 to answer each time.
2+3+5+5+5= 20
2) Which of the following could replace ??? so program outputs “yay success”
******REVISED ANSWERS FROM STUDY GUIDE, answer choices should be tuples, lists
for critter in ???:
if len(critter) > 10:
print(“yay success”)
break
# Option A (Tuple)
("hippopotamus",)
# Option B (List)
["dog"]
# Option C (Tuple)
("alligator",)
# Option D (List)
["rhinoceros"]
Study Guide Final
Course 179869
3) For this while loop, which of the following values of x would cause loop to run indefinitely?
while y != 3:
y += 1
a. y = 10 this would start at 10 and keep growing by one because it will never != 3
b. y = 0 this would increment and eventually reach 3 which will cause loop to terminate
c. y = -3 this would increment and also eventually reach 3
Module 4 Practice Questions
1. Lists are ordered collection of items, they are mutable and you can change their
elements after they have been created. Lists are defined using brackets []
2. Tuples are ordered collection of items, but they are immutable, cannot be changed.
Tuples are defined using parenthesis.
3. Sets a unordered collection of unique items, sets do not allow duplicate elements.
2) Given a dictionary:
a. grocery_bag:
b. grocery_bag["candy"]: produces an error candy is not a key
c. grocery_bag["dairy"] = "cheese"
d. del grocery_bag["juice"]
b) Which statements correctly insert a new key-value pair into the dictionary?
a. grocery_bag["candy"] = "snickers"
b. grocery_bag.update({"candy": "snickers"})
c. grocery_bag.insert("snacks", "popcorn") there is no insert in dictionary.
Study Guide Final
Course 179869
def iterator(max_val):
???
for item in iterator(value):
print(item)
For which of the following lines of code could replace ??? so that the program prints all odd numbers from 0
(inclusive) to value (exclusive)? Select all that apply.
a. return range(max_val)
b. return range(1, max_val, 2)
c. return max_val % 2 != 0
d. return max_val % 2
Output is [‘brocoli’, ‘potatoes’, ‘steak’, ‘candy’] the function does not modify the original list
Output is [‘brocoli’, ‘potatoes’, ‘steak’, ‘pretzel’] the function does modify the list.
Study Guide Final
Course 179869
1) True/False
In a recursive function, the base case(s) are crucial to prevent infinite recursion.
True, recursive function the base case is crucial to prevent infinite recursion
2) True/False
A recursive function calling itself with the same function argument(s) it was called with will always
result in infinite recursion.
False, a recursive function calling itself with the same function argument(s) it was called with
will not always result in infinite recursion.
def beep(string):
print("BEEP")
length = len(string)
if length > 1:
beep(string[length//2])
beep(string)
class Car:
def _ _init_ _(self, car_color):
self.car_color = car_color
my_car = Car(CarColor("blue"))
your_car = Car(CarColor("blue"))
Which of the following statements are true? Select all that apply.
a) The == operator would find my_car and your_car to be equal.
b) The == operator would not find my_car and your_car to be equal.
c) The is operator would find my_car and your_car to be equal.
d) The is operator would not find my_car and your_car to be equal.
Study Guide Final
Course 179869
1) True/False
1. An except: clause (with no specified exception type) will catch a ValueError TRUE
2. An except: clause (with no specified exception type) will catch only exceptions derived
from the BaseException class. FALSE
3. An except BaseException: clause will catch a SystemExit TRUE
4. An except: clause (with no specified exception type) will catch a SystemExit TRUE
2) What exceptions will be thrown by this program?
some_dict = {}
for idx in range(len(some_list)):
some_dict[idx] = some_list[idx]
a. ValueError
b. TypeError
c. IndexError
d. Key Error
some_dict = {}
for idx in range(len(some_list)):
some_dict[idx] = some_list[idx]
a. ValueError
b. TypeError
Study Guide Final
Course 179869
c. IndexError
d. KeyError
in_file.close()
out_file.close()
2) Which of the following statements about writing to a file in Python are true? Select all that apply.
1. The .flush() method on a file object ensures that all data is immediately written to the file.
2. If you open a file in read mode ("r"), you cannot modify its contents using the write mode
("w").
3. It is not necessary to explicitly call the .close() method on a file object after performing file
operations.
Study Guide Final
Course 179869
1) Which of the following functions from the random module in Python can be used to generate a
random integer within a specified range?
a. random.choice()
b. random.randint()
c. random.sample()
d. random.shuffle()
import random
my_list = list()
for i in range(12):
my_list.append(random.randint(0, 3))
print(random.choice(my_list))
What are some of the potential numbers that will be printed when the program is called?