Untitled Flashcards Set

Midterm 2 Practice Questions (from your classmates)
Question 1
Where is the error in this code that evaluates if the given number is odd?
1. def is_number_odd(n):
2. print(n)
3. if n % 2 1:
4. return True
5. print("Number is odd!")
6. else:
7. return False
8. print("Number is even!")
9. is_number_odd(7)
A. The error is the print statement being given after the return statement.
B. The error is in the wrong division operator being used. It should be true division
(/) operator instead.
C. Variable N has a print statement before being defined in the function.
D. n % 2 1 would mean the result of the division would equal 1, so the inputted
number has to be two. There should instead be a remainder variable to evaluate
for diVerent numbers.
E. The else statement should be written as an elif statement.
A. Choice 1 is correct because when a return statement is given, the function will exit
whatever loop it is in (if statements, for loops, while loops) if the return statement is
nested in the loop's indentation. As the return statement is inside the else/if
indentation, the function will exit the else/if loop immediately, not printing the
proceeding strings that are inside the else/if statement.
B. Choice 2 is incorrect because the true division operator would mean that the
given number is being divided by 2 entirely, returning a floating point instead of
the integer needed to determine if the number is even or odd.
C. Choice 3 is incorrect because while there isn't a local value associated with
variable n, since the variable is inside the function definition, the value is
determined by the function call, defining it with a number called (7 in this
instance).
D. Choice 4 is incorrect because the modulus operator used is the operator to
evaluate for remainders of a given equation. Although there would need to be a
remainder variable if the true division operator were used, the modulus operator
would still need to be used to calculate for remainder.
E. Choice 5 is incorrect because elif statements are only required when there are
multiple parameters that the variable must return true for at least one of them,
where as if there is only one parameter being evaluated for that returns true or

false given two possible outcomes, the elif statement would be unnecessary as it
would require there be another parameter as elif statements require more than
two outcomes.
Question 2
There is an error in the following code. How would the error be corrected?
def find_lower(word):
x = []
for i in len(word):
if word.islower():
x.append(i)
return x
A. Change line 4 to read "if word[i].islower():"
B. Change the function to instead print to the console.
C. Change the x variable to an empty string.
D. Change line 5 to x.insert(i)
E. There is nothing wrong with this code.
Choice A is the correct answer because it specifies where in the list the function needs to
check for a lowercase letter. As is, the function is checking if the entire string entered for
the word variable is all lowercase.
Choice B is incorrect because that would not solve the error the code has. It would just
make it so the list is printed to the console - but the list would be empty.
Choice C is incorrect because strings are not mutable. Therefore, the append() function
won't work unless it's assigned to a new variable other than x.
Choice D is incorrect because the insert() function will insert the number at the beginning
of the list (because no other character of the list is defined) rather than adding it to the end
of the list.
Choice E is incorrect because there is an error in the function on line 4.
Question 3
What would be the result of this code?
def change_str(input_str):
change_str = input_str.strip()
vowels = ['a', 'e', 'i', 'o', 'u']
for vowel in vowels:
change_str = change_str.replace(vowel, '')
return change_str
test_str = "Hello World"
result = change_str(test_str)

print(result)
A. Hello World
B. Hll Wrld
C. Hello Wrld
D. Error - code won't run
E. Hllo World
A. Incorrect - shows the string without any modifications
B. Correct - vowels were removed
C. Incorrect - didn't remove all vowels
D. Incorrect - code will run
E. Incorrect - didn't remove 'o' vowel"
Question 4
Consider the following Python code:
def my_function(x):
if x < 10:
return x 2
elif x < 20:
return x + 5
else:
return x - 3
What will be the output of my_function(15)?
A. 30
B. 20
C. 25
D. 10
E. 12
*Answer:** B. 20
**Explanation:**
A. Choice 1 (30) is incorrect because the function does not return x 2 when x is
between 10 and 20. For x = 15, the function will return x + 5, not x 2.
B. Choice 2 (20) is correct because the condition x < 20 is true for x = 15, so the
function will return x + 5, which is 15 + 5 = 20.
C. Choice 3 (25) is incorrect because the function does not return x + 5 for x = 15. It is
only executed if x < 20 but is greater than or equal to 10.
D. Choice 4 (10) is incorrect because the condition x < 10 is false for x = 15, so the
function will not return x 2.
E. *Choice 5 (12)** is incorrect because there is no condition in the function that would return
`12` for x = 15.
Question 5

What is the expected output of this code?
x = 'Hello CS! This is X!'
def function(x):
x = x[::-1]
r = []
i = 0
while i < len(x):
print(x[i])
r.append(x[i])
i += 1
return "".join(r[::-1])
print(function(x))
A: !
X
s
i
s
i
h
T
!
S
C
o
l
l
e
H
Hello CS! This is X!
B !
X
s
i
s
i
h
T

!
S
C
o
l
l
e
H
!X si sihT !SC olleH
C: Nothing will display
D:
!
X
s
i
s
i
h
T
!
S
C
o
l
l
e
H
['H', 'e', 'l', 'l', 'o', ' ', 'C', 'S', '!', ' ', 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'X', '!']
A is correct because we are printing x in reverse, turning it into a list, reversing this list, then
joining it back into a string.
B is incorrect because r is not reversed, so it is just printing x in reverse.
C is incorrect because the function is fruitful and will return a value.
D is incorrect because it is not joining the list back into a string.
Question 6
Which of the following statements is true about mutability and immutability?

A. both lists and strings are Mutable
B. lists are Immutable, strings are mutable
C. Strings are immutable, lists are mutable
D. both lists and strings are immutable
E. none of the above
Answer: C. strings are immutable, lists are mutable
A is incorrect because only lists are mutable
B is incorrect because lists are mutable
C is correct because strings are immutable and lists are mutable
D is incorrect because strings are immutable and lists are mutable
E is incorrect because strings are immutable and lists are mutable
Question 7
What will be the output if we execute the following code? Please select a multiple choice
option (A-E) below?
The code:
for i in range (20):
print(i, end = " ")
i =+200
print(i, end = " ")
MCQ Options:
A. 0 200 1 201 2 202 3 203 4 204 5 205 6 206 7 207 8 208 9 209 10 210 11 211 12 212 13 213
14 214 15 215 16 216 17 217 18 218 19 219
B. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
C. 0 200 0 200 0 200 0 200 0 200 0 200 0 200 0 200 0 200 0 200
D. 0 200 1 201 2 202 3 203 4 204 5 205 6 206 7 207 8 208 9 209 10 210 11 211 12 212 13 213
14 214 15 215 16 216 17 217 18 218 19 219 20 220
E. Error: invalid syntax
Correct Answer: A: 0 200 1 201 2 202 3 203 4 204 5 205 6 206 7 207 8 208 9 209 10 210 11 211
12 212 13 213 14 214 15 215 16 216 17 217 18 218 19 219
Explanations:
A. This answer is correct because the code runs for loop where i starts at 0 and then goes up to
19 (because of the range(20)); in these loops you get 1 away from the actual number (or in other
words minus 1). For each iteration in this loop, it prints out the current value of i that is going
through in each iteration. i =+200 basically resets i to + 200 in each of the iteration which causes

i =+200 too go to 200 after printing its value and then after printing each i (0, ..., 12, 19) it prints
i + 200 which results in this sequence above (0 200.....19 219).
B. This answer is incorrect because this Option only shows the numbers from 0 to 19 without
increment by 200 (i + 200). Therefore, for that reason, this answer must be incorrect.
C. This answer is incorrect because this code does print any of the i's which this code is clearly
supposed to print by observing what is in the code above, it instead only prints the i + 200 even
though it is supposed to both the i and i + 200.
D. This answer is incorrect because of the extra number at the end 20: 220. In order for the loop
to do this the code would have to be specified "for in range (21)" not "for in range(20)" as
presently written in the code above.
E. There is no syntax error in this code. Therefore, this option is incorrect."
Question 8
Fix this while loop such that it prints the count up to ten.
def main():
count = 0
while count >= 10:
print("count = " + str(count))
count += 1
Choice 1: Change the greater than (>) for a less than (<)
Choice 2: Add a return function with the variable count at the end inside the loop.
Choice 3: The count has to be changed to eleven such that ten will be printed as well.
Choice 4: Remove the str() around the count variable.
Choice 5: Change the += to a -= so it counts properly.
Answer is choice 1
Choice 1: This makes it so it enters the while loop.
Choice 2: is wrong because if a return is added to the end of the loop it will only run once.
Choice 3: Is wrong because it already prints the ten because it counts up to and including the
number.
Choice 4: Is wrong because this would result in a concatenation error.
Choice 5: Is wrong because this would count down, but because it's at zero it will cause a stack
overflow error."
Question 9
What will be the result of executing the following code?
e = “cS122”
e = “C” + e[2:]
e
a) ‘CS122’
b) TypeError: ‘str’ object does not support item assignment

c) ‘C122’
d) ‘cS122’
e) None
Correct Answer: c) ‘C122’
Explanation:
a) ‘CS122’
This answer is incorrect because the list indices start from 0, meaning that “e[2:]” refers to the
third character in the string, “1”. This answer option references list indices that we learned in
week 2.
b) TypeError: ‘str’ object does not support item assignment
This answer is incorrect because although strings are immutable, we are reassigning e, thus
saving e to a new memory file.
c) ‘C122’
This answer is correct because we are reassigning e to a new memory address, with a new e[0] of
“C”, and adding the existing characters of the string starting at e[2], which is “1”. The resulting
new string will be ‘C122’.
d) ‘cS122’
This answer is incorrect because e is being reassigned to have new characters.
e) None
This answer is incorrect because calling e will return the string.
Question 10
Given the following Python code snippet:
def mystery_function(n):
result = 0
for i in range(1, n+1):
if i % 2 == 0:
result += i
return result
What is the output of the function mystery_function(6)?
A. 12
B. 15
C. 18
D. 20
E. 21
Correct Answer is: A. 12
A. Choice 1 is correct because the function adds up all even numbers from 1 to 6. The even
numbers in this range are 2, 4, and 6, and their sum is 12.

B. Choice 2 is incorrect because 15 is not the correct sum of even numbers between 1 and 6. The
even numbers are 2, 4, and 6, and their sum is 12.
C. Choice 3 is incorrect because 18 is not the correct sum of even numbers between 1 and 6. The
sum of even numbers 2, 4, and 6 is 12.
D. Choice 4 is incorrect because 20 is not the correct sum of even numbers between 1 and 6. The
even numbers between 1 and 6 are 2, 4, and 6, which add up to 12, not 20.
E. Choice 5 is incorrect because 21 is not the correct sum of even numbers between 1 and 6. The
even numbers 2, 4, and 6 add up to 12, not 21.
Question 11
What will the following code print?
grocery_list = ['apples', 'cereal', 'pasta']
print(grocery_list[0:5])
A. ['apples', 'cereal', 'pasta']
B. Code will print an index out of range error
C. ['apples']
D. Code won't print anything
E. ['pasta']
Answer: A. Choice 1
A. Choice 1 is correct answer because although there is no value at index 5, it will just print
every value in the list.
B. Choice 2 is incorrect because the 5 in this case will just have the code print every value even
if there aren't enough to reach index 5.
C. Choice 3 is incorrect because the code in the print command is asking for more values at
indexes greater than 0.
D. Choice 4 is incorrect because the code will continue to print values until it reaches the end of
the list.
E. Choice 5 is incorrect because the code will start printing at index 0 and so on.
Question 12
What will happen if I run the following code?
def songchoice (song, artist):
print ("My favorite song is" + song +" by "+artist)
songchoice (No Children)
A. The console will print "My favorite song is No Children by artist"
B. The console will not print anything.
C. The console will return an error.
D. The console will print "My favorite song is" + song+ "by" artist"
E. The console will return a value.

Correct answer:
C. The console will return an Error.
The first option is incorrect. When a function requires multiple parameters, it requires both
inputs. Even though one parameter is satisfied, the console will not print "artist" as a default.
The second option is incorrect. When using the print function, a message or other input is printed
to the console. Because the syntax for the print function is correct, the program will attempt to
print to console.
The third option is correct. The console will return an error, as it does not have both arguments
necessary to fulfill the function. It will tell you that there is a missing argument for "artist."
The fourth option is incorrect. Because the formatting for the print statement is correct AND we
are passing an argument through the function, the console will not print the embedded message.
The fifth option is incorrect. Print is not a fruitful function, so print does not return a value.
Instead, it is a type of function that prints to the console.
Question 13 (I adapted this question)
What will be the result if we execute the following code?
1 def change_string(w):
2 w = w + " world"
3 print(w)
4 my_string = "hello"
5 change_string(my_string)
6 print(my_string)
A. ‘hello’
‘hello world’
B. ‘hello world’
‘hello’
C. ‘hello world’
D. ‘hello’
E. There will be an error printed on the console.
Answer: B
A. Line 3 will print first and then line 6. Because strings are immutable you need to write a
fruitful function that return the new string value otherwise, my_string and w are at two
different addresses.

B. The flow of execution dictates printing line 3 first.
C. There are two print statements being executed at line 3 and line 6.
D. There are two print statements being executed at line 3 and line 6.
E. There is no error in the code.
Question 14
What would be the result of this code?
def modify_list(lst):
lst.append(5)
list = [1, 2, 3]
modify_list(list)
print(list)
A.) [1, 2, 3, 4, 5]
B.) [1, 2, 3]
C.) Error - Can't modify list
D.) [1, 2, 3, 5]
E.) Error - lst isn't defined
A.) Incorrect - The function is only appending one value: 5
B.) Incorrect - Lists are mutable so appended 4
C.) Incorrect - lists are mutable so the list can be modified
D.) Correct - Appended 5 to the original list
E.) Incorect - lst is the function parameter
Question 15
What will be the result of this function:
st = 'Hello world!'
st [::-2]
A. It will result in an error.
B. It will print 'Hello world!'
C. It will print "!dlrow olleH'
D. It will print '!lo le'
E. It will print 'el ol!'
Answer: D. It will print '!lo le'.
A. Choice A is incorrect because the brackets are within range and will still print.
B. Choice B is incorrect because the addition of the '-2' step initiates the program to print
every other character backwards.
C. Choice C is incorrect because the '2' step skips every other letter.

D. Choice D is correct because it prints every other character backwards, starting with the
last character. The negative sign initiates the program to print backwards and the '2'
initiates the program to print every other character.
E. Choice E is incorrect because the negative sign initiates the program to print backwards
starting with the last character. This choice prints every other character starting from the
first character.
Question 16
A fruitful function returns a value. Examine the fruitful function below:
def calculate_area(length, width):
area = length * width
return area
What will be the outcome of the code below?
result = calculate_area(5, 3)
print(result)
Choose the best option:
A. 15
B. None
C. An error because the function does not take arguments
D. Area (as a string)
E. An error because it function does not return anything
Answer: A. Choice 1
A. Choice 1 is the correct answer because the function called "calculate_area" takes two
arguments, this is the length and the width. It then multiplies the two arguments to create a
product that it returns at the end of the function. The code runs with the arguments 5 and 3
which look like "calculate_area(5, 3)." When multiplied, this creates a result of 15 which is
printed, so the output is 15.
B. Choice 2 is incorrect because this function is a fruitful function as stated above and it
has a return statement meaning it will return a calculated value. Therefore the function can
not return "None."
C. Choice 3 is incorrect because the function does take two arguments, being the length
and the width. These functions are executed correctly throughout the function.
D. Choice 4 is incorrect because area is not a string in this function. Area is the calculated
number in the function that is returned. Therefore it can not return "area."
E. Choice 5 is incorrect because it is a fruitful function with a return statement. Therefore it
no error will occur do to the functions inability to return a value.

Question 17
What will be printed to the console?
for i in range(5, 9):
print(i)
A. 5, 6, 7, 8, 9
B. 6, 7, 8, 9
C. 5, 9
D. 5, 6, 7, 8
E. 8, 7, 6, 5
Answer: D. Choice 4
A. Choice 1 is incorrect because the index does not contain the last number in the range,
so 9 wouldn't be printed.
B. Choice 2 is incorrect because the index contains the first (beginning) number but not the
last.
C. Choice 3 is incorrect because the printing of index includes the values from the
beginning to the integer before the last range value, so it wouldn't just print the end
members of the range.
D. Choice 4 is correct answer because the values printed begin with 5 and end with the last
integer before 9.
E. Choice 5 is incorrect because this is the opposite order of the values printed. If we had
asked
for i in range(8, 4, -1):
print(i), then this is the answer we would have received.
Question 18
Identify the error in this function that is converting a decimal to base. Only one
answer.
1 def decimal_to_base(decimal, base):
2 if decimal == 0:
3 return "0"
4 result = ""
5 while decimal > 0:
6 result = str(decimal % base) + result
7 decimal = decimal // base
8 return result
Multiple choice:

A). Nothing, everything looks good.
B). Line 7; switch = to %
C). Line 1; change def to import
D). Line 3; return "1"
E). Line 5; change > to <
EXPLANATIONS:
A). Nothing is wrong with this function because I had gotten it from a past project and it ran
perfectly
B). Changing the = to % would cause the math to error and make the rest of the function
not work.
C). The definition to import changes everything and nothing works.
D). Returning 1 would make the math not work and ruin the rest of the function, giving the
base number back will be incorrect.
E). Changing the greater than to less than will make the math not work because the
decimal will not be less than 0.
Question 19 (I adaped this question)
What will be the output of the following code:
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
list3 = list1 + list2[1:3]
list3[3] = 10
list4 = list3[7]
list4.append(11)
A. [1, 2, 10, 4, 5, 6, 7, 11]
B. [1, 2, 10, 4, 6, 7, 8, 11]
C. IndexError: list index out of range
D. [1, 2, 10, 4, 6, 7, 11]
E. [1, 2, 10, 4, 6, 11]
Answer: C. There will be an index out-of-range error.
A. [1, 2, 10, 4, 5, 6, 7, 11] is incorrect because the third line states that it only takes the
second and the third items from list2. This could be confusing because the index of a list
starts at 0 while some people may think it starts at 1 and that's why 5 would be included in
the final answer.
B. [1, 2, 10, 4, 6, 7, 8, 11] is incorrect because the third line states that it only takes the
second and the third items from list2. This could be confusing because the code, list2[1:3],

takes only the first index and does not include the 2rd index, some people may think it is
included leading them to have 8 in the final answer.
C. List index starts from 0 and there is no element at index 7 for list3.
D. [1, 2, 10, 4, 6, 7, 11] would be the correct answer if list4 was printed, however, it was
not.
E. [1, 2, 10, 4, 6, 11] is incorrect because the list is too short. Line 5 takes the first 6 items in
list3, therefor, E is incorrect because it only has the first 4 items of list3.
Question 20
What will be the result if we execute the following code?
x = 3
y = 7
while y >= x + 1:
print(x, y, end = " ")
x += 1
Answer Choices
A. Choice 1: 3 7 4 7 5 7 6 7 will be printed to the console. (Correct Answer)
B. Choice 2:
3 7
4 7
5 7
6 7
Will be printed to the console.
C. Choice 3: Nothing will be printed to the console.
D. Choice 4: 3 7 4 7 5 7 6 7 7 7 will be printed to the console.
E. Choice 5: The loop will run endlessly.
Explanations:
A. Choice 1 is the correct answer because the while loop will run 5 times, the first 4 printing
'x y' with x increasing by 1 each time. On the 5th run the loop will end as x will equal 7 and 7
is not >= 8 (compared to 8 because 7 + 1 = 8).
B. Choice 2 is incorrect because end = " " parameter is included meaning that the program
will not print on a new line.
C. Choice 3 is incorrect because the while loop includes a print statement that will be
executed.

D. Choice 4 is incorrect because the while loop cannot run if y = x, meaning that '7 7'
cannot be printed. This would be correct if the '+ 1' adjacent to x was not included.
E. Choice 5 is incorrect because the loop will end as x will eventually become greater than
y due to x being incremented by 1.
Question 21
Consider the following code, what will the code print:
def is_even(n):
return n % 2 == 0
def main():
number = 12
if is_even(number):
print("The number is even.")
else:
print("The number is odd.")
main()
A) Syntax error
B) The number is odd
C) The number is even
D) The function is_even does not return any value
E) The function is_even returns the wrong data type
Answer: C the number is even
A. Syntax error is incorrect because there is not error or bug in this code.
B. The number is odd is incorrect because the number is set to 12, the function is_even(12)
will return True
C. The number is even is the correct answer because the function is_even(12) will return
True.
D. The function is_even does not return any value is incorrect is_even returns a boolean
value depending on weather n is odd or even
E. The function is_even returns the wrong data type is incorrect because is_even returns a
boolean value which is appropriate for the if statement.
Question 22
Which of the following statements is true about immutable strings?
A. Strings can be changed in place after they are created
B. A new string must be created to modify an existing string
C. Strings are mutable and can be modified without creating a new object
D. Strings immutability prevents any form of modification
E. Strings can be modified by using their index positions

ANSWER: B
A. is incorrect because strings in Python cannot be changed in place.
B. correct because when you modify a string a new string object is created instead of
changing the string
C. incorrect because it states that strings are mutable.
D. incorrect because string immutability does not prevent any modification.
E. incorrect because strings cannot be modified directly using their index positions. It will
result in an error.