0% found this document useful (0 votes)
4 views

Midterm Study Questions

The document contains a series of midterm exam study questions related to programming concepts, specifically focusing on Python language syntax, data types, and control structures. Each question presents multiple-choice answers, covering topics such as variable assignment, error types, loops, and data structures. The questions are designed to test knowledge and understanding of fundamental programming principles.

Uploaded by

yatharthis0031
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Midterm Study Questions

The document contains a series of midterm exam study questions related to programming concepts, specifically focusing on Python language syntax, data types, and control structures. Each question presents multiple-choice answers, covering topics such as variable assignment, error types, loops, and data structures. The questions are designed to test knowledge and understanding of fundamental programming principles.

Uploaded by

yatharthis0031
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Midterm Exam Study Questions

1) Basic instruction types are input, process, and _____.

a. output

b. memory

c. calculation

d. assignment

2) What is a common word for the textual representation of a program?

a. code

b. prompt

c. interpreter

d. expression

3) Which symbol is used in Python to create a comment?

a. *

b. C

c. //

d. #

4) Which statement outputs the text: "I won't quit!"?

a. print(I won't quit!)

b. print("I won't quit!")

c. print('I won't quit!')

d. print('I won't quit!', punctuation=True)


5) Space, tab, and newline are all called _____ characters.

a. noprint

b. symbol

c. space-line

d. whitespace

6) Which statement reads a user-entered string into variable user_name?

a. input = user_name()

b. user_name = input()

c. input() => user_name

d. user_name = "input()"

7) Which function converts a string to an integer?

a. int()

b. integer()

c. string_to_int()

d. convert(string, int)

8) In the statement:age = input('Enter your age: ')

, the string 'Enter your age: ' is called a(n) _____.

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

10) Dividing by zero is an example of which type of error?

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

12) Which statement about Python is true?

a. Linux and Mac computers usually do not come with Python installed.

b. There are no free web-based tools for learning Python.

c. Windows usually comes with Python installed.

d. Developers are not usually required to pay a fee to write a Python program.

13) 0s and 1s are known as ___ .

a. switches
b. bits

c. voltage

d. electricity

14) A processor is a circuit that executes a list of _____.

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

17) RAM is an abbreviation that stands for:

a. Real Analog Memory

b. Ranged Access Memory

c. Random Access Memory


d. Random Abbreviated Memory

18) The _____ is a process that manages programs and interfaces with peripherals.

a. clock

b. BIOS

c. integrated circuit

d. operating system

19) A(n) _____ is a program that executes a script.

a. app

b. compiler

c. processor

d. interpreter

20) The Python language began development in the late _____'s.

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

24) Which of the following symbols can be used as part of an identifier?

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

27) Which of the following identifiers is valid?

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

31) Which expression using parentheses is equivalent to the following expression:

x - y * -z / 3

a. (x - y) * ((-z) / 3))

b. x - ((y * (-z)) / 3)

c. x - (y * ((-z) / 3))

d. (x - (y * (-z))) / 3

32) The operator *= is called a(n) _____ operator.

a. double

b. compound

c. increment

d. multiple assignment

33) Which statement is equivalent to the following 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__

37) What are the possible values for random.randint(-4, 4)?

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)

39) What is displayed when the following code is executed?

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

b. string_1 = string_2 + "456"

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?

a. my_list = [7, 2, -8, 16]

b. my_list = [4]
c. my_list = ['1', '2', '3', '4']

d. my_list = integer(4)

43) Which of the following statements about my_list is false?

my_list = ['JFK', 'LAX', 'MIA']

a. The element at index 1 is 'JFK'

b. The list has a length of 3

c. The list elements are all strings

d. The index of the last item in the list is 2

44) What is the output?

my_list = [2, 8, 3, 1, 18, 5]


print(my_list[3] + my_list[1] * 2)

a. 7

b. 10

c. 17

d. 18

45) Which method call returns the number of elements in my_list?

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]"])

47) A _____ can be located in a dictionary and is associated with a value.

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]

49) Which statement removes entry "1G1JB6EH1E4159506" from the dictionary


cars_dict?

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 If grade < 75


Put "C" to output

Else If grade < 85


Put "B" to output

Else If grade <= 100


Put "A" 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

52) For what values of x will "Medium" be output?

If x > 40: Output "Large"


Else If x > 20: Output "Medium"
Else If x > 10: Output "Small"

a. Any x larger than 20

b. Any x smaller than 40

c. Any x from 21 to 40

d. Any x from 10 to 40

53) If x = 10 and y = 20, which expression is True?

a. x == y

b. y <= x

c. y >= x

d. y != 2 * x

54) Which has an error? Assume x = 10 and y = 20.

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)

a. False OR (True AND False) --> False OR False --> False


b. False OR (True AND False) --> False OR True --> True

c. (False OR True) AND False --> True AND False --> False

d. (False OR True) AND False --> True AND False --> True

56) Which is true of the badly formatted code?

x = input()
if x == 'a':
print('first')
print('second')

a. Both print() statements must be indented.

b. Neither print() statement has to be indented.

c. The first print() statement must be indented.

d. The second print() statement can't be indented.

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?

my_list = [6, 2, 8, -1, 12, 15, -7]


x = Get first my_list value
While x is not negative
put "Positive number!" to output
x = Get next my_list value

Put "Done" to output

a. 3

b. 4

c. 5

d. 7

59) What is the ending value of count?

my_list = [3, -4, 0, -1, 2, 1, 8]


n = 0
count = 0

While n < length of my_list


If my_list[n] > 0
count = count + 1
n = n + 1

a. 1

b. 3

c. 4

d. 5

60) Which input value causes "Goodbye" to be output next?

x = int(input())
while x >= 0:
# Do something
x = int(input())
print('Goodbye')

a. -1
b. 0

c. 1

d. No such value

61) What is the output?

count = 0
while count < 3:
print('loop')
count = count + 1
print(f'Final value of count: {count}')

a. Prints 'loop' once, then 'final value of count: 1'

b. Prints 'loop' three times, then 'final value of count: 3'

c. Prints 'loop' three times, then 'final value of count: 4'

d. Prints 'loop' forever (infinite loop)

62) Which is an essential feature of a while loop having the following form?

while loop_expression:
loop_body

a. The loop_expression should be affected by the loop_body

b. The loop_expression should not be affected by the loop_body

c. The loop_body should get user input

d. The loop_body should update at least two variables

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

64) What is the ending value of x?

x = 0
i = 5
while i > 1:
x = x + i
i = i - 1

a. 0

b. 12

c. 14

d. 15

65) What is the output?

names = ['Bob', 'Jill', 'Xu']


ages = [24, 18, 33]
for index in [2, 0, 1]:
print(f'{names[index]}:{ages[index]}')

a. Xu:33
Bob:24
Jill:18

b. Bob:24
Jill:18
Xu:33

c. Xu, Bob, Jill:33, 24, 18

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)

d. range(20, 22, 24)

68) Which choice fills in the blank so that the output prints one line for each item in
sports_list, as in: 1. Hockey?

sports_list = [ 'Hockey', 'Football', 'Cricket' ]


for i in _____:
print(f'{i+1}. {sports_list[i]}')

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?

a. Asking a user to enter names until the user enters 'Quit'.


b. Counting the number of negative values in a list of integers.

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?

a. Checking to see if a list of integers contains the value 12.

b. Counting how many keys in a dictionary start with the letter 'A'.

c. Asking the user to enter positive integers, exiting by entering -1.

d. Looping through the characters in a string, and displaying 'yes' if it contains a vowel.

71) What is the output?

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

72) What is the ending value of z?

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

d. start of the program

75) In the following code, the variable size is the function's _____.

def calc_square_area(size):
area = size * size
return area

val = float(input('Enter size of square: '))


square_area = calc_square_area(val)
print(f'A square of size {val} has area {square_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

val = float(input('Enter size of square: '))


square_area = calc_square_area(val)
print(f'A square of size {val} has area {square_area}')

a. parameter

b. argument

c. property

d. value

77) What is output?

def calc(num1, num2):


return 1 + num1 + num2

print(calc(4, 5), calc(1, 2))

a. 9 3

b. 10 4

c. 145 112

d. 4, 5, 1, 2

78) Which correctly calls the add() function?

def add(a, b, c):


return a + b + c

a. add(2 4 6)
b. add(2 + 4 + 6)

c. add(2; 4; 6)

d. add(2, 4, 6)

79) Which of the following is true?

a. A function must have exactly one return statement, or no return statement at all.

b. A function must always have at least one return statement.

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.

80) After the program runs, what is the value of y?

def print_sum(num1, num2)


print(num1 + num2)

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

83) Which of the following isnota reason to use functions?

a. To avoid writing redundant code

b. To improve code readability

c. To support modular development

d. To make the code run faster

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?

def calculate(a, b):


return a + b

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

a. The use of the function makes the program run faster

b. The use of the function decreases redundant code

c. The use of the function reduces the number of variables in the code

d. The function does not improve the code

86) What is the output?

def print_water_temp_for_coffee(temp):
if temp < 195:
print('Too cold.')

elif (temp >= 195) and (temp <= 205):


print('Perfect temperature.')

elif (temp > 205):


print('Too hot.')

print_water_temp_for_coffee(205)
print_water_temp_for_coffee(190)

a. Too cold.

b. Perfect temperature.

c. Perfect temperature.
Too cold.

d. 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

88) Which line of the function has an error?

def compute_sum_of_squares(num1, num2): # Line 1


sum = (num1 * num1) + (num2 * num2) # Line 2
return # Line 3

a. Line 1

b. Line 2

c. Line 3

d. None of the lines contains an error

89) What is the error?

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

a. No error, the program executes and outputs the correct value

b. Undefined function compute_square()

c. Undefined variable cone_vol()

d. Undefined function cone_volume()

90) What is the output?

def is_even(num):
if num % 2 == 0:
even = True
else:
even = False

is_even(7)
print(even)

a. False

b. True

c. No output: Call to is_even() fails due to no return value

d. No output: An error occurs due to unknown variable even

91) What is output?

new_string = 'One giant leap for mankind'


print(new_string[0:6])

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]

93) What is output?

new_string = 'Python'
print(new_string[0:-1:2])

a. Pto

b. Pytho

c. on

d. yhn

94) Consider the stringnew_string = 'Berlin is the capital of Germany'.


Which statement will return 'capital of Germany' as a substring?

a. new_string[15:35]

b. new_string[14:31]

c. new_string[-18:]

d. new_string[-18:1]

95) Considermy_string = 'roy came third in the running race'. Which


option will return 'Roy came 3rd in the running race' as the value of new_string?

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

96) What is output?

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)

c. 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.

email = 'Email: [email protected]'


phone = 'Ph: 333-222-1111'
print(f"Email me at {XXX} or call at {YYY}")

a. XXX: email.split(' '), YYY: ' '.join((phone.split(': ' )))

b. XXX: email.split(' '), YYY: ' '.join((phone.split(': ' )[1]))

c. XXX: email.split(' ')[0], YYY: ' '.join((phone.split(': ' )[0].split('-')))

d. XXX: email.split(' ')[1], YYY: ' '.join((phone.split(': ' )[1]).split('-'))

100) What is output?

my_poem = 'Roses are red; Violets are blue'


new_separator = '.'
new_poem = my_poem.split(';')
print(new_separator.join(new_poem))

a. Roses are red. Violets are blue

b. Roses.are.red.Violets.are.blue

c. Roses;are;red;Violets;are;blue

d. Roses are red Violets are blue

You might also like