Midterm Study Questions
Midterm Study Questions
a. output
b. memory
c. calculation
d. assignment
a. code
b. prompt
c. interpreter
d. expression
a. *
b. C
c. //
d. #
a. noprint
b. symbol
c. space-line
d. whitespace
a. input = user_name()
b. user_name = input()
d. user_name = "input()"
a. int()
b. integer()
c. string_to_int()
d. convert(string, int)
a. prompt
b. prefix
c. variable
d. assignment
9) Which statement has a syntax error? Assume variables x, y, z, and age have already
been defined.
a. y = y
b. x + y = z
c. age = '32'
d. print('Name, age')
a. runtime
b. syntax
c. logic
d. infinity
11) Which type of error does not cause the program to crash?
a. logic error
b. value error
c. indentation error
d. assignment error
a. Linux and Mac computers usually do not come with Python installed.
d. Developers are not usually required to pay a fee to write a Python program.
a. switches
b. bits
c. voltage
d. electricity
a. bits
b. switches
c. memories
d. instructions
15) Which type of program converts a high-level language program into machine
instructions?
a. App
b. Compiler
c. Processor
d. Assembler
16) A _____ is a computer component that stores files and other data.
a. disk
b. monitor
c. keyboard
d. processor
18) The _____ is a process that manages programs and interfaces with peripherals.
a. clock
b. BIOS
c. integrated circuit
d. operating system
a. app
b. compiler
c. processor
d. interpreter
a. 1940
b. 1960
c. 1980
d. 2010
21) The Python language is developed by a public community of users, therefore Python
is a(n) _____ language.
a. buggy
b. open-source
c. open-space
d. beginner
22) A _____ is a named item used to hold a value.
a. constant
b. number
c. statement
d. variable
23) Which of the following statements has a syntax error? Assume age and years are
variables that have already been defined.
a. age = years - 2
b. age + 2 = years
c. age = 17 - 2
d. age = -15
a. @
b. $
c. &
d. _ (underscore)
25) A language is called _____ when upper case letters in identifiers are considered
different from lower case letters.
a. unambiguous
b. case sensitive
c. case strict
d. camel case
26) A _____ is a word that is part of the Python language and can't be used as a
variable name.
a. keyword
b. special token
c. syntax symbol
d. stylized word
a. max_age
b. 32area
c. transfer$
d. True
28) _____ is the process where objects that are no longer needed are deleted.
a. Identity recycling
b. Memory clearing
c. Garbage collection
d. Object recycling
29) Objects like integers and strings that can't be modified are called _____ .
a. immutable
b. mutable
c. frozen
d. set
30) The built-in Python function that gives an object's identity is:
a. memory()
b. id()
c. type()
d. identity()
x - y * -z / 3
a. (x - y) * ((-z) / 3))
b. x - ((y * (-z)) / 3)
c. x - (y * ((-z) / 3))
d. (x - (y * (-z))) / 3
a. double
b. compound
c. increment
d. multiple assignment
x -= 2 + y
a. x = 2 + y - x
b. x = -(2 + y)
c. x = x - (2 + y)
d. x = x - 2 + y
34) Which floating-point literal correctly represents the scientific notation value: 2.3 x
10^7?
a. 2.3e7
b. 2.3*10e7
c. 2.3e10^7
d. 2.3xe7
35) Which statement makes the code in the math module available?
a. use math
b. allow math
c. import math
d. include math
36) What is the value of the __name__ built-in variable in a module that is executed as a
script by the programmer?
a. __main__
b. __direct__
c. __module__
d. __executed__
a. -4...4
b. -4...0
c. 0...3
d. -4...4
38) Dice have 6 sides, with values 1, 2, 3, 4, 5, and 6. Which expression randomly rolls
one dice, directly yielding one of those values?
a. random.randrange(0, 6)
b. random.randrange(1, 6)
c. random.randint(0, 6)
d. random.randint(1, 6)
empty_string = ''
print(len(empty_string))
a. "empty"
b. 1
c. 0
d. "0"
40) If text_line = 'one fish two fish', what is the value of text_line[6]?
a. ' '
b. 'h'
c. 'i'
d. 's'
41) Which of the following statements produces an error? Assume string_1 = 'abc' and
string_2 = '123'.
a. string_2 = string_1
c. print(string_1 + string_2)
d. string_1[1] = 'B'
42) Which of the following assignment statements creates a list with 4 integer elements?
b. my_list = [4]
c. my_list = ['1', '2', '3', '4']
d. my_list = integer(4)
a. 7
b. 10
c. 17
d. 18
a. len(my_list)
b. size(my_list)
c. my_list.count()
d. my_list.size()
46) The variable emails_dict is assigned with a dictionary that associates student ids
with email addresses. Which statement prints the email address associated with the
student id "C2104"?
a. print(value of emails_dict("C2104"))
b. print(key of emails_dict("C2104"))
c. print(emails_dict["C2104"])
d. print(emails_dict["[email protected]"])
a. value
b. pair
c. list
d. key
48) Which statement changes the value associated with key "Lemon" to 0.75 in the
dictionary fruits_dict?
a. fruits_dict[0.75] = "Lemon"
b. fruits_dict["Lemon"] = 0.75
c. fruits_dict[Lemon] = 0.75
d. dict("Lemon") = fruits_dict[0.75]
a. cars_dict["1G1JB6EH1E4159506"] = None
b. cars_dict{"1G1JB6EH1E4159506"}.del()
c. delete(cars_dict["1G1JB6EH1E4159506"])
d. del cars_dict["1G1JB6EH1E4159506"]
50) With the logic block shown below, what is output when grade is assigned with the
value 75?
If grade < 50
Put "F" to output
Else If grade < 60
Put "D" to output
Else
Put "Invalid grade" to output
a. A
b. B
c. C
d. Invalid grade
51) What is the value of test_val after the following code is executed?
a = 12
test_val = 6
if a * 2 == test_val:
a = a + 7
else:
test_val = 2 * a
test_val = a + 1
a. 7
b. 13
c. 24
d. 25
c. Any x from 21 to 40
d. Any x from 10 to 40
a. x == y
b. y <= x
c. y >= x
d. y != 2 * x
a. if x = y:
b. if x < y:
c. if x <= y:
d. if x != y:
55) Given x = 1, y = 2, and z = 3, how is the expression evaluated? In the choices, items
in parentheses are evaluated first.
(x == 5) or (y == 2) and (z == 5)
c. (False OR True) AND False --> True AND False --> False
d. (False OR True) AND False --> True AND False --> True
x = input()
if x == 'a':
print('first')
print('second')
57) Excess indentation must be removed from which lines to make the code correct?
1. print('start')
2. if x > 10:
3. print('large')
4. else:
5. print('small')
6. print('done')
a. 1, 6
b. 1, 2, 3
c. 2, 3, 4
d. 2, 4, 5
58) How many times will the body of the loop execute?
a. 3
b. 4
c. 5
d. 7
a. 1
b. 3
c. 4
d. 5
x = int(input())
while x >= 0:
# Do something
x = int(input())
print('Goodbye')
a. -1
b. 0
c. 1
d. No such value
count = 0
while count < 3:
print('loop')
count = count + 1
print(f'Final value of count: {count}')
62) Which is an essential feature of a while loop having the following form?
while loop_expression:
loop_body
63) Fill in the blank so that the loop displays all odd numbers from 1 to 100.
i = 1
while i <= 100:
print(i)
i = _____
a. 1
b. i + 1
c. 2
d. i + 2
x = 0
i = 5
while i > 1:
x = x + i
i = i - 1
a. 0
b. 12
c. 14
d. 15
a. Xu:33
Bob:24
Jill:18
b. Bob:24
Jill:18
Xu:33
d. Xu:24
Bob:18
Jill:33
66) Fill in the blank so that the output is a count of how many negative values are in
temperatures?
temperatures = [-2, 8, 4, -7, 18, 3, -1]
count = 0
for t in temperatures:
if _____:
count = count + 1
print(f'Total negative temperatures: {count}')
a. t < 0
b. temperatures < 0
c. temperatures[t] < 0
d. t[temperatures] < 0
67) Which range() function call generates every even number between 20 and 30
(including both 20 and 30)?
a. range(20, 30, 2)
b. range(20, 31, 2)
c. range(30, 20, 2)
68) Which choice fills in the blank so that the output prints one line for each item in
sports_list, as in: 1. Hockey?
a. range(len(sports_list))
b. range(len(sports_list-1)
c. range(1, len(sports_list))
d. range(1, len(sports_list)-1)
69) Which of the following loops is best implemented with a for loop?
c. Starting from a user-entered integer, increment the value until the value is a prime
number.
d. Reading values from a temperature sensor until it gives a value greater than 100
degrees.
70) Which of the following loops is best implemented with a while loop?
b. Counting how many keys in a dictionary start with the letter 'A'.
d. Looping through the characters in a string, and displaying 'yes' if it contains a vowel.
for j in range(2):
for k in range(4):
if (k == 2):
break
print(f'{j}{k}', end=' ')
a. 00 01 02
b. 00 01 02 03
c. 00 01 10 11
d. 00 01 02 10 11 12
z = 0
a = 5
while a > 0:
a = a - 1
if a == 2:
continue
z = z + a
a. 7
b. 8
c. 9
d. 10
73) The code that includes the keyword "def" is called a _____.
a. function call
b. function definition
c. function reference
d. function constructor
74) After a function's last statement is executed, the program returns to the next line
after the _____.
a. import statement
b. function definition
c. function call
75) In the following code, the variable size is the function's _____.
def calc_square_area(size):
area = size * size
return area
a. parameter
b. argument
c. property
d. value
76) In the following code, the variable val is the function call's _____.
def calc_square_area(size):
area = size * size
return area
a. parameter
b. argument
c. property
d. value
a. 9 3
b. 10 4
c. 145 112
d. 4, 5, 1, 2
a. add(2 4 6)
b. add(2 + 4 + 6)
c. add(2; 4; 6)
d. add(2, 4, 6)
a. A function must have exactly one return statement, or no return statement at all.
c. A function can only return strings and numbers, not lists or dictionaries.
d. A function can have any number of return statements, or no return statement at all.
y = print_sum(4, 5)
a. 4 5
b. 9
c. 45
d. None
81) Which line in the function print_greeting() must be changed if the user wishes to print
the greeting three times with three different names?
def print_greeting(name):
print('Welcome message:')
print(f'Greetings {name}')
a. def print_greeting()
b. print('Welcome message:')
c. print('Greetings', name)
d. None. To print the greeting with three different names, the main program must call
print_greeting() three times with three different arguments.
82) Which term describes how Python assigns the type of a variable?
a. dynamic typing
b. static typing
c. quick typing
d. random typing
84) Given the following function. To change the function to return the product instead of
the sum, how many lines of code need to be changed?
print(calculate(3, 4))
print(calculate(5, 2))
print(calculate(6, 7))
a. 1
b. 2
c. 3
d. 4
85) How does the given function improve the code versus if no function was present?
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32.0) * 5.0 / 9.0
fahrenheit = float(input())
c1 = fahrenheit_to_celsius(fahrenheit);
c2 = fahrenheit_to_celsius(32.0);
c3 = fahrenheit_to_celsius(72.0);
c. The use of the function reduces the number of variables in the code
def print_water_temp_for_coffee(temp):
if temp < 195:
print('Too cold.')
print_water_temp_for_coffee(205)
print_water_temp_for_coffee(190)
a. Too cold.
b. Perfect temperature.
c. Perfect temperature.
Too cold.
87) For the given program, how many print statements will execute?
def print_shipping_charge(item_weight):
if (item_weight > 0.0) and (item_weight <= 10.0):
print(item_weight * 0.75)
elif (item_weight > 10.0) and (item_weight <= 15.0):
print(item_weight * 0.85)
elif (item_weight > 15.0) and (item_weight <= 20.0):
print(item_weight * 0.95)
print_shipping_charge(18)
print_shipping_charge(6)
print_shipping_charge(25)
a. 1
b. 2
c. 3
d. 9
a. Line 1
b. Line 2
c. Line 3
cone_vol = cone_volume(2, 4)
print(cone_vol)
def compute_square(r):
return r * r
def cone_volume(r, h):
return (0.33) * 3.14 * compute_square(r) * h
def is_even(num):
if num % 2 == 0:
even = True
else:
even = False
is_even(7)
print(even)
a. False
b. True
a. Onegia
b. One gi
c. One gia
d. 'Onegi
92) Consider the stringnew_string = 'Code development in Python'. Which
statement will return 'velop' as a substring?
a. new_string[-18:12]
b. new_string[7:11]
c. new_string[8:12]
d. new_string[-19:12]
new_string = 'Python'
print(new_string[0:-1:2])
a. Pto
b. Pytho
c. on
d. yhn
a. new_string[15:35]
b. new_string[14:31]
c. new_string[-18:]
d. new_string[-18:1]
a. new_string = my_string.title()
new_string = new_string.replace('thi', '3')
b. new_string = my_string[:3].title()
new_string = new_string.replace('thi', '3')
c. new_string = my_string.capitalize()
new_string = new_string.replace('third', '3')
d. new_string = my_string.capitalize()
new_string = new_string.replace('thi', '3')
my_string = 'Greetings!'
print(my_string == 'greetings!')
print('tin' in my_string)
print('Hello' > my_string)
a. False
True
True
b. True
True
True
c. True
True
False
d. False
True
False
97) Which of the following methods will change the string 'Python Programming' into
'PYTHON PROGRAMMING'?
a. title()
b. isupper()
c. upper()
d. capitalize()
98) Consider the listmy_list = ['www', 'python', 'org']. Choose the option
that returns 'www.python.org' as the value to my_string.
a. my_string = (.).join(my_list)
b. my_string = ','.join(my_list)
d. my_string = ('.').join(my_list)
99) Complete the following code to get 'Email me at [email protected] or call at 333 222
1111' as output.
b. Roses.are.red.Violets.are.blue
c. Roses;are;red;Violets;are;blue