0% found this document useful (0 votes)
10 views10 pages

fina_study_guide_answers

Uploaded by

583443
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

fina_study_guide_answers

Uploaded by

583443
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Study Guide Final

Course 179869

Module 1 Practice Questions:

1) Which variable declarations are valid? Correct are highlighted


a) none = None
b) OH_NO = “it’s the final”
c) 5_times = “five times” NOT correct, variables cannot start with a number
d) insertion = input(“Enter a word:”)

2) x = 75
a) Y = x / 2
b) Z = x // 2

Are Y and Z the same? (hint review float vs integer)

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

3) word = “carnivorous caterpillar” has multiple correct ways to do it.


a) How would you use indexing to get:
i) cat
(a) cat = word[:2] + word[-9]
ii) pillar
(a) pillar = word[-6:]
iii) car
(a) car = word[0:3]
iv) cart
(a) cart = word[0:3] + word[-9]
Study Guide Final
Course 179869

Module 2 Practice Questions:

1) Given: (10 - 3) > 6 or (False and True or False):


Which of the following Boolean expressions are equivalent?
a. True
b. False
c. not True or False
d. False or True ~ True or False which is what the expression is

10 – 3 = 7 which is bigger than 6 so that part is True

Second part False and True or False is False

The overall expression then becomes True or False which is TRUE

2) macarons = 12

if macarons > 13:


print(“enough macarons to feed me”)
if macarons < 10:
print("not enough for me to eat”)
if macarons <= 12:
print("almost enough for me”)

What would be the output of the program ?

The output will be: “almost enough for me” this is because of the <= ( less than or equal to 12)
Study Guide Final
Course 179869

Module 3 Practice Questions

1) What is the output of this program?

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.

So, the value of answer after all iterations is:

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) Explain differences between a list, a tuple, and a set

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:

grocery_bag = {“chocolate”: “pretzels”, “dairy”: “milk”, “juice”: “lemonade”}

a) Which statement would produce an error?

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

Module 5 Practice Questions:


1) Given program:

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

2) What is the output of this code?


def be_healthy(list):
for item in list:
if item == “candy”:
item = “pretzel”

my_lunch = [‘brocoli’, ‘potatoes’, ‘steak’, ‘candy’]


be_healthy(my_lunch)
print(my_lunch)

Output is [‘brocoli’, ‘potatoes’, ‘steak’, ‘candy’] the function does not modify the original list

3) What is the output of this code?


def be_healthy(list):
for i in range(len(list)):
if list[i] == "candy":
list[i] = "pretzel"

my_lunch = ['broccoli', 'potatoes', 'steak', 'candy']


be_healthy(my_lunch)
print(my_lunch)

Output is [‘brocoli’, ‘potatoes’, ‘steak’, ‘pretzel’] the function does modify the list.
Study Guide Final
Course 179869

Module 6 Practice Questions:

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.

3) you are given program below (string is some arbitrary string):

def beep(string):
print("BEEP")
length = len(string)
if length > 1:
beep(string[length//2])

beep(string)

Which of the following statements are false?

a. The program will never print BEEP more than once


b. The program will never print BEEP more than twice
c. The program will never print BEEP more than len(my_string)//2 times
d. The program will never print BEEP more than len(my_string) times
Study Guide Final
Course 179869

Module 7 Practice Questions:


1) Which of the following statements are true?
a. == checks for equality of identity.
b. is checks for equality of identity.
c. == checks for equality of value.
d. is checks for equality of value.

2) Given the following program:


class CarColor:
def _ _init_ _(self, color):
self.color = color

def _ _eq_ _(self, other):


return self.color == other.color

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

Module 8 Practice Questions

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]

value = int(input("Enter an index: "))


print(value // 2)
print(some_list[value])
print(some_dict[value])

a. ValueError
b. TypeError
c. IndexError
d. Key Error

3) What exceptions will be thrown by this program?

some_dict = {}
for idx in range(len(some_list)):
some_dict[idx] = some_list[idx]

value = int(input("Enter an index: "))


print(value // 2)
print(some_list[value])
print(some_dict[value])

a. ValueError
b. TypeError
Study Guide Final
Course 179869

c. IndexError
d. KeyError

Module 9 Practice questions:

1) You are given file input1.text its contents are:


apple
banana
orange

you have a program


in_file = open("input1.txt")
out_file = open("output1.txt", "w")

for line in in_file:


out_file.write(line.strip())

in_file.close()
out_file.close()

what will output1.txt be?

The output will be “applebananaorange” all in one line

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

Module 10 Practice Questions:

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()

2) you have a program below:

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?

Some potential numbers would be 0, 1, 2, 3

You might also like