Font size
WorksheetsPython Practice Quiz
Total questions: 60
Worksheet time: 31mins
print("Hello world!\nHello world!")
Hello world!
print("Hello" + str(2) + "world!")
What is it called when code is executed based on a condition being true
Sequence
Selection
Iteration
Variable
What is is called when code is repeated / looped until a condition has been met or a set number of times.
Sequence
Selection
Iteration
Variable
What does the following code do? myAge = int (myAge)
Converts the var (variable) myAge to a string
Converts the var (variable) myAge to a integer
Converts the var (variable) myAge from a integer to a string
Converts the var (variable) myAge to if statement
print(Hello world!)
Which function decides if a string only contains numbers
isalpha()
isnumeric()
int()
string()
What keyword tells the interpreter that the code which follows is a function
const
int
def
while
elif
else
ifif
else if
What is the output for:
people = ["John", "Rob", "Bob"]
print (people[1])
John
Rob
Bob
[ ]
What is the output for:
people = ["John", "Rob", "Bob"]
print (people[-1])
John
Rob
Bob
[ ]
What is the output for:
people = ["John", "Rob", "Bob"]
print (people[4])
John
Rob
Bob
Error
What will be the value of the following Python expression?
4 + 3 % 5
a) 7
b) 2
c) 4
d) 1
Which of the following character is used to give single-line comments in Python?
a) //
b) #
c) !
d) /*
Which of the following is not a core data type in Python programming?
a) Tuples
b) Lists
c) Class
d) Dictionary
To add a new element to a list we use which Python command?
a) list1.addEnd(5)
b) list1.addLast(5)
c) list1.append(5)
d) list1.add(5)
What will be the output of the following Python statement?
print("abcd"[2:])
a) a
b) ab
c) cd
d) dc
What will be the output of:
print(“hello” +1+2+3)?
a) hello123
b) hello
c) Error
d) hello6
Suppose list1 is [2445,133,12454,123], what is max(list1)?
a) 2445
b) 133
c) 12454
d) 123
Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?
a) [2, 33, 222, 14]
b) Error
c) 25
d) [25, 14, 222, 33, 2]
If a=(1,2,3,4), a[1:-1] is _________
a) Error, tuple slicing doesn’t exist
b) [2,3]
c) (2,3,4)
d) (2,3)
What is the final result of the expression 7 / 2 + 6?
0
9.5
9
0.875
Choose the print statement below that will cause an error. Assume that num has the value 6, and name has the value Isabella.
print(name + ":" )
print(num)
print(name + " wants " + "num " + "candies")
print(name + " wants " + "num " + "candies")
print(name + ": " + num)
Which one of the statements below will cause an error?
ans = “hi” * 8
ans = “hi” + 9
ans = “hi” + “hi” + “hi”
ans = (“a” * 4) + “b”
Suppose you have a variable defined a = "4". What is the variable type of a?
str
int
float
number
What kind of data does a float variable contain?
Whole numbers
Words
Numbers that can have decimal components
A float is not a Python variable type
What does the following code print?
x = 3.4
y = 1
print(int(x))
print(x + y)
3.4
4.4
3
4.4
3
4
The code causes an error
Which of the following options is the best way to get a number from the user that you plan to use in a mathematical equation?
num = str(input(“Enter a number: “))
num = number(input(“Enter a number: “))
num = input(“Enter a number: “)
num = int(input(“Enter a number: “))
What is the final result of the expression 2**3?
6
8
23
That is not a valid Python expression.
On which line of code will Python FIND the error?
weight = input("How much do you weigh? ")
oz_water = weight / 2
print("You should drink " + str(oz_water))
print("ounces of water every day if you weigh " + weight)
Line 1
Line 2
Line 3
Line 4
In what order should these statements be executed in order to get input from the user and print out a confirmed result?:
A) response = input("Do you like cheese? ")
B) print("You have chosen " + confirm)
C) print("You responded " + response)
D) confirm = input("Are you sure? ")
A, B, C, D
B, C, A, D
A, C, D, B
C, B, A, D
Which of the following choices is a properly formed Python variable name, meaning it is both legal in the Python language and considered good style?
third_answer
thirdAnswer
thirdanswer
3rdAnswer
Which of the following choices is NOT a Python variable type?
int
str
float
number
How would you round the number held in the variable pi to 3.14?
pi = 3.14159265
round(pi)
round(pi, 3)
round(pi, 3)
round(pi, 2)
Why should you use round when you compare two numbers that have type float?
If you don’t, Python will only compare the whole part and discard the decimal part.
You don’t have to, but it’s good style. All of the comparisons will still work out the same way.
Sometimes numbers can be rounded in unexpected ways based on how Python computes them. Therefore, it is best to use round in order to make sure both numbers are rounded the same way.
You should always use round when you compare two numbers, even if they are of type int.
What will be the output of this program?
number = 5
greater_than_zero = number > 0
if greater_than_zero:
-> print(number)
# ignore the ->, it's showing the indent
0
5
True
Nothing will print
What will be the output of this program?
number = 5
greater_than_zero = number > 0
if greater_than_zero:
-> if number > 5:
-> -> print(number)
# ignore the ->, it's showing the indents
0
5
True
Nothing will print
What will be the output of this program?
5
5
7
5
7
8
5
8
What will the following program print when run?
1
1
3
5
1
4
1
4
5
What will the following program print when run?
1
1
3
5
1
4
1
4
5
What does this program print?
But green is the color of grass!
Blue is the best
But yellow is the color of school buses!
But green is the color of grass!
But green is the color of grass!
I like red, too!
Blue is the best
But yellow is the color of school buses!
Blue is the best
Which of the following for loops would print the following numbers?
0
1
2
3
4
5
for i in range(5):
-> print(i)
for i in range(1, 5,1):
-> print(i)
for i in range(6):
-> print(i)
for i in range(0, 5, 1):
-> print(i)
Which of the following for loops would print the following numbers?
3
5
7
9
for i in range(3, 10, 2):
-> print(i)
for i in range(3, 9, 2):
-> print(i)
for i in range(9):
-> print(i)
for i in range(3,9):
-> print(i)
What is the value of num when this loop completes?
num = 0
for i in range(2, 8, 2):
-> num = num + i
2
8
12
20
Which of the following for loops will give the same values for i as the loop below?
for i in range(10):
for i in range(11, 0):
for i in range(10, 0, -1):
for i in range(0, 10):
for i in range(0, 11, 1):
What does this program print?
It’s still a nice day
It’s still a nice day
It’s still a nice day
It’s still a nice day
It’s still a nice day
It’s still a nice day
It’s still a nice day
It’s still a nice day
It’s still a nice day
It’s still a nice day
It’s still a nice day
It’s still a nice day
It’s still a nice day
It’s still a nice day
Which of the following while loops would continue to loop as long as num has values between the range of 3-12, exclusive?
while num > 12 and num < 3:
# do something with num
while num < 12 or num < 3:
# do something with num
while num <= 12 and num >= 3:
# do something with num
while num < 12 and num > 3:
# do something with num
Which of the following while loops will cause an infinite loop?
secret_num = 10
while secret_num == 10:
-> secret_num = secret_num - 1
secret_num = 10
while secret_num == 10:
-> print(secret_num)
secret_num = 10
while secret_num > 0:
-> secret_num = secret_num - 1
secret_num = 10
while secret_num != 10:
-> print(secret_num)
When would a while loop be a better control structure to use than a for loop?
When you need to repeat multiple commands
When you need to repeat something 5 times
When you don’t know how many times something will need to repeat
When the user is inputting how many times to repeat something
What will be printed to the screen when this program is run?
0
1
0
1
0
2
0
1
0
1
0
1
1
2
1
2
1
2
What is the value of sum when this code completes?
8
9
20
0
What will be printed to the screen when this program is run?
100
0
-75
-125
-150
-150
-175
175
What does this program print?
0
1
2
0
1
2
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
What is the benefit of using the break command in this program?
The user can enter as many answers as they want
The program will break out of the loop as soon as the user enters the magic_number
The loop will prompt the user to enter a number no matter what input they give
The loop will give the magic_number after a certain number of tries
If the user enters ‘4’, I want the loop to exit. At which line should the break command be placed in order to end the loop when the user enters this value?
On line 3
On line 5
On line 7
On line 9
