NEW
Font size
WorksheetsPYTHON PROGRAMMING - FINAL EXAM REVIEW
Total questions: 46
Worksheet time: 23mins
1. What does type(3.55) return?
str
int
float
All of these
1. How do you know if you have written your code correctly and there are no errors?
Print your code
Debug your code
View your code
Reset your code
1. Which contains only integers? (ignore the commas)
3, 2, 1, *, +, =, -
Q, W, E, R, T, Y, a, s, d, f
-2, -1, -0.5, 0, 0.5, 1, 2
1, 2, 0, -3, 2, -5
The input() function in Python returns user input as a string.
True
False
I'm not interested in guessing
name = "skye homsi"
name1 = name.title()
name2 = name1.swapcase()
print(name2)
What is the output from the above code?
"skye homsi"
"sKYE hOMSI"
"Skye Homsi"
"SKYE HOMSI"
1.name = input("enter your name")
2. print(name, "your score is", score)
3. score = 199
Order the above numbered code lines into the proper sequence:
3,1,2
1,2,3
1,3,2
3,2,1
vehicleType = "Truck"
if vehicleType.upper().startswith("P"):
print(vehicleType, 'starts with "P"')
else:
print(vehicleType, 'does not start with "P"')
What is the output from running the above code?
“False”
"True"
"Truck does not start with "P""
Truck starts with "P""
1. A function with 2 integer parameters could start with which definition?
showName.run()
showName()
run showName
open(showName)
def addNumbers(num1, num2 = 10):
return num1 + num2
Choose the correct output of calling the function above using the following code:
print(addNumbers(100))
an error
100
200
110
num_1 = 3
num_2 = "8"
What is the minimum code to mathematically add num_1 to num_2?
int(num_1) + int(num_2)
num_1 + int(num_2)
num_1 + str(num_2)
int(num_1) + str(num_2)
size_num = "8 9 10"
size = "8" # user input
if size.isdigit() == False:
print("Invalid: size should only use digits")
elif int(size) < 8:
print("size is too low")
elif size in size_num:
print("size is recorded")
else:
print("size is too high")
What is the output from running the above code?
"size is recorded"
"Invalid: size should only use digits"
"size is too high"
"size is too low"
x = 3
y = 4
calculation = x*y
print(calculation)
What is the best estimate for the output of the above code?
12.0
9
12
9.0
if True:
if False:
print("Banana")
else:
print("Apple")
else:
if True:
print("Dates")
else:
print("Corn")
What is the output of the above code?
"Dates"
"Banana"
"Corn"
"Apple"
Escape sequences start with a backslash (\).
True
False
Hello
World!
What is the best choice of code to create the output shown above?
print("Hello\nWorld!")
print("Hello\tWorld!")
print("Hello World!")
print("Hello\pWorld!")
x = 0
while True:
if x < 10:
print('run forever')
x += 1
else:
break
What is the output from running the above code most like?
"run forever" (9 times)
No Output
"run forever" (10 times)
"run forever" (11 times)
def ignore_case(name):
answer = ""
while answer.isalpha() == False:
answer = input("enter last name: ")
return answer.lower() ==
name.lower()
When will the ignore_case function exit the while loop?
When answer == name
When answer.lower() == name.lower()
When a digit is entered at the input prompt
When only letters are entered at the input prompt
Which choice will print the first half of the word "Consequences", given the following variable declaration?
word = "Consequences"
print(word[7:1])
print(word[:6])
print(word[1:7])
print(word[6:])
In the for loop below, the variable "word" is an arbitrary variable name. Any valid variable name can be used since its value is assigned only within this loop.
for letter in word:
print(letter)
False
True
Which choice best represents the output of the following code?
student_name = "iteration"
new_word = ""
for letter in student_name[:3]:
new_word += letter
print(new_word)
"iter"
"ite"
"ation"
"ration"
The .count() string method returns the number of times a for/in loop iterates
True
False
1. Python cannot display quotes in strings using print().
False
True
"Hello".isalpha()
What is the output of the above code?
True
Error
False
Hello
answer = input("enter your answer: ")
print("You entered " + answer)
If user input is 38, what is the output of the above code?
You entered 38
TypeError
SyntaxError
You entered enter your answer
What does type("Hello World!") return?
int
float
str
All the above
Choose the correct statement.
The same variable in Python can be represented in either upper or lower case
Variables only represent strings in Python.
Variables in Python can be initialized using the equals sign (=).
New variables always start out as zero.
The type() function is used to change from one data type to another.
True
False
What does type(1024) return?
int
float
str
All of these
Choose the correct statement.
Variables are updated in Python using the keyword "new".
A variable name in Python cannot contain numbers.
Variable values in Python can change type, from integer to string
A variable value in Python cannot be updated.
"Hello, world!" is a specialized program used specifically for Python.
False
True
In Python, we can use the "+" operator to add numbers, but not to add strings.
True
False
1. PriNt("Hello World!") will not output the message "Hello World!".
What will it result in?
NameError
TypeError
SyntaxError
All of these
Which is a valid Python code comment?
[comment] comments can provide use instructions for other developers.
(comment) warnings can be placed in comments.
/comments make code easier to update.
# use comments to describe the function of code.
What does the Python code: score = 3 + "45" result in?
345
type()
TypeError
48
answer = input("enter your answer: ") print(10 + answer)
If user input is 38, what is the output of the above code?
10answer
TypeError
1038
48
Which code formats the string "Green" to "GREEN"?
.isupper()
.istitle()
.upper()
.capitalize().
def addNum(num1 = 10):
return num1 + num1
Choose the correct output of calling the function above using the following code:
print(addNum(100))
100
200
20
An error
name = "SKYE HOMSI"
print("y" in name.lower())
What is the output from the above code?
"skye homsi"
3
False
True
def lowCase(wordsIn):
return wordsIn.lower()
Choose the correct output of calling the function above using the following code:
wordsLower = lowCase("Return THIS lower")
print(wordsLower)
"return this lower"
"Return THIS lower"
An error
No Output
Function names cannot begin with a number as the first character.
True
False
def addNum(num1 = 10): print(num1 + num1)
Choose the correct output of calling the above function using the following code:
addNum()
An error
0
20
10
1. greet(name)
2. name = input("enter your name: ")
3. greet(name)
4. def greet(person): u
5. print("Hi,", person)
6. greet(name)
Which line or lines need to be removed from the above code so the program prints a greeting without error?
line 1 only
line 1 and 3
line 3 only
lines 1 and 2
name = "Tobias"
print(name == "Alton")
What is the output from running the above code?
"Tobias = Alton"
False
"Alton"
True
day = "monday"
if day.capitalize() == "Monday":
print("Start the week!")
else:
pass
What is the output from running the above code?
No Output
True
"Start the week!"
False
x = 3
y = "3"
# get_diff takes an int and a str
def get_diff(xint, ystr):
# <function code needed here>
if y.isdigit() == False:
print('"y" is not an integer string')
elif get_diff(x,y) == 0:
print('x equal to y')
else:
print('x is NOT equal to y')
To make this program work, printing "x equal to y", choose the best code to replace the above comment "# <function code needed here> " in the get_diff function.
return str(ystr) - in(xint)
return int(ystr) – xint
return ystr – x
a. return int(xint) – y
x = 3
y = 3
calculation = x/y
print(calculation)
What is the best estimate for the output of the above code?
1
9.0
9
1.0
