No student devices needed. Know more
67 questions
Which Python code segment will display “Hello, world!” on the screen?
display Hello, world!
print("Hello, world!")
print("Hello, world!")
"Hello, world!"
What does the following Python code display?
print("Hello")
print("World")
Hello
World
HelloWorld
Hello World
Hello
World
What type is the following variable?
x = "Hi there"
float
integer
boolean
string
What does the following Python program print?
x = "I am"
y = 6
z = "feet tall"
print(x)
print(y)
print(z)
I am 6 feet tall
I am6feet tall
I am
6
feet tall
x
y
z
What is the type of the variable x in the following Python program?
x = input("Enter something: ")
string
integer
float
The type cannot be determined.
Suppose you run the following Python program:
x = input("Enter some text: ")
print(x)
While the program is running, after the prompt has been printed, the user types the following and presses Enter:
Delaware ratified the U.S. Constitution in 1787.
What does the program print?
Delaware ratified the U.S. Constitution in 1787.
Delaware ratified the U.S. Constitution in
Delaware
(The program does not print anything.)
What does the following Python program print?
x = 9 + 6 / 3 * 2 - 1
print(x)
9
12
15
21
What does the following Python program print?
x = 12 % 4
print(x)
0
1
3
4
Which of the following Python programs will not run?
x = 4
y = 5
print(x + y)
x = 4
y = "hi"
print(x + y)
x = 4
y = 5.5
print(x + y)
x = 4
print(x + 5)
Which of the following is NOT a program that will produce the following output?
hellohellohello
var = "hello" * 3
print(var)
var = "hello" + "hello" + "hello"
print(var)
var = "hello"
print(var)
print(var)
print(var)
num_times = int(input("How many times?: "))
var = "hello" * num_times
print(var)
Which of the following is NOT a valid type of comment in Python?
%% This is a comment
# This is a comment
"""
This is a comment
"""
Which of the following best describes the main purpose of comments?
Comments create better spacing in programs.
Comments describe your program so other people can read it more easily.
Comments warn the people running the program about potential problems with the program.
How many possible values are there for a boolean variable?
1
2
3
There is an infinite number of possibilities.
Assume you are writing a program, and you have a boolean variable called b, defined like so:
b = True
Pick the correct if statement to follow the code above. The if statement should be correct Python, and the body of the if statement should only run if b is True.
if b:
print("b is True!")
if b:
print("b is True!")
if True:
print(b)
if True:
print(b)
Which of the following programs will run but will not print anything?
x = True
if x:
print("hi")
if False:
print("hi")
x = False
if x:
print("hi")
else:
print("hello")
if False:
print "hi"
What does this Python expression evaluate to?
100 != 100
True
False
"True"
"False"
Which of the following is not a comparison operator?
<=
!=
?
>
Which of the following is not a logical operator in Python?
and
or
not
because
Which of the following conditions tell me to sleep if my alarm did not go off (alarm = False) and I’m tired (tired = True)?
if alarm and tired:
sleep
if not alarm or tired:
sleep
if not alarm and tired:
sleep
if not alarm and not tired:
sleep
Which of the following evaluates to the variable x rounded to two decimal places?
round(x, 2)
round(2, x)
round(x), 2
round(2), x
Which of the following values is rounded to 3 decimal places?
2.3
3.000
3.0001
3
How many lines will this program print?
while True:
print("hi")
0
1
an infinite number of lines
How many lines will this program print?
x = 10
while x > 0:
print(x)
x = x - 3
3
4
5
6
Which of the following programs prints ten lines?
for i in range(10):
print("hi")
for i = 1 to 10:
print("hi")
for i in 1 - 10:
print("hi")
for i from 0 to 9:
print("hi")
Which of the following best describes the purpose of a for loop?
A for loop is for doing something an indeterminate number of times.
A for loop is doing something an infinite number of times.
A for loop is for doing something a fixed number of times.
A for loop is for doing something three times.
If I am using the following condition in my program:
while True:
which keyword should I use to make sure I don’t create an infinite loop?
break
continue
Which Python keyword skips back to the beginning of a loop?
break
continue
What does the following program print?
for i in range(2):
for j in range(2):
print(i + j)
0
1
1
2
0112
0
1
0
1
0101
Which of the following does not properly nest control structures?
for i in range(3):
for j in range(6):
print(j)
for i in range(3):
if i > 2:
break
else:
print(i)
count = 0
if count < 10:
for i in range(3):
print(count)
count = count + 1
count = 10
for i in range(3):
if count > 0:
print(i)
else:
print(count)
Which of the following is the correct way to declare a function in Python?
print_hello:
print("hello")
function print_hello():
print("hello")
print_hello():
print("hello")
def print_hello():
print("hello")
What is the correct way to call a function named print_sum?
def print_sum():
print_sum
print_sum():
print_sum()
Which of the following code samples correctly passes the number 10 as an argument to the function print_number?
print_number(10)
print_number()
print 10
10(print_number)
print print_number
print 10
Which of the following code samples correctly creates a function that takes a single parameter and prints it?
def print_number(x):
print(x)
def print_number():
print(parameter)
def print_number():
print(x)
def print(x)
In which namespace is the variable ‘x’ defined in the following program?
x = 10
def add_nums():
y = 2
z = x + y
print(z)
function namespace
global namespace
object namespace
class namespace
Consider this code snippet.
x = 10
def add_nums():
y = 2
z = x + y
print(z)
Which of the following additional functions could be safely added to this code, without causing an error?
def subtract_nums():
z = x - y
print(z)
def subtract_nums():
z = z - x
print(z)
def subtract_nums():
y = 5
z = x - y
print(z)
def subtract_nums():
z = y
z = x - y
print(z)
Which of the following functions successfully returns double the variable ‘number’?
def my_function(number):
return number*2
def my_function(number*2):
return
def my_function():
return number
def my_function(number):
number*2
Which of the following functions successfully returns the number 10?
def my_function():
10
def my_function(10):
return
def my_function():
return 10
def my_function():
print(10)
Which of the following keywords are relevant to exceptions in Python?
I. def
II. except
III. try
II and III
II only
I and II
none of the above
What type of error occurs when a program is expecting an integer value and the user enters a string?
NumberError
ValueError
InputError
ZeroDivisionError
Which of the following prints the letter A?
my_string = "ABCDE"
print(my_string[1])
my_string = "ABCDE"
print(my_string(0))
my_string = "ABCDE"
print(my_string(1))
my_string = "ABCDE"
print(my_string[0])
Which of the following prints E?
my_string = "ABCDE"
print(my_string[-1])
my_string = "ABCDE"
print(my_string[5])
my_string = "ABCDE"
print(my_string[-5])
my_string = "ABCDE"
print(my_string(5))
Which operator allows you to create a string that is the result of putting two different strings together, side by side?
+
-
*
\
What is the correct way to concatenate the variable sign (sign = "Happy day!") to produce the following output?
expected output: “Happy Birthday!”
print(sign[:6] + "Birth" + sign[6:])
print(sign[:5] + "Birth" + sign[6:])
print(sign[:6] + "Birth" + sign[5:])
print(sign[:6] + "Birth" + sign[:6])
Which of the following prints CDE?
my_string = "ABCDE"
print(my_string[3:])
my_string = "ABCDE"
print(my_string[2:])
my_string = "ABCDE"
print(my_string[3:5])
my_string = "ABCDE"
print(my_string[2:4])
Which word applies to strings in Python?
mutable
immutable
What does your answer in question 1 mean?
Strings in Python can be modified and replaced.
Strings in Python can be modified, but not replaced.
Strings in Python can be replaced, but not modified.
Strings in Python can be neither replaced nor modified.
Which of the following programs DOES NOT PRINT the first five letters of the alphabet, each on its own line?
my_string = "abcde"
for letter in range(len(my_string)):
print(letter)
my_string = "abcde"
for letter in my_string:
print(letter)
my_string = "abcde"
for i in range(len(my_string)):
print(my_string[i])
What does len("hello") evaluate to?
“hello”
4
“—–”
5
What does the following program print?
print("a" in "banana")
True
False
1
"aaa"
What does the following program print?
x = "ab"
y = "banana"
print(x in y)
True
False
0
"ba"
Which of the following functions prints 3, and nothing else?
s = "hello"
x = s.find("l")
print(x)
s = "banana"
x = s.find("a")
print(x)
s = "hello"
for i in range(len(s)):
print(i)
s = "apple"
x = s.find("l")
print(x)
If I’m getting input from a user and having their answer dictate my if/else statement, like so:
happy = input("Are you happy? (yes/no): ")
if happy == "yes":
draw_smile()
elif happy == "no":
draw_frown()
else:
print("Invalid response")
Which string method can I use to make sure that the phrase ‘invalid response’ only displays if the user has typed something other than ‘yes’ or ‘no’, without paying attention to their capitalization?
upper
lower
swapcase
strip
Which of the following is NOT true about tuples?
Tuples are ordered.
Tuples are immutable.
Tuples can contain elements of different types.
Tuples use parentheses to access individual elements, rather than square brackets.
Which of the following is the correct way to declare a tuple?
x = (1, 2, 3)
x = [1, 2, 3]
Which of the following Python programs creates a list with the numbers 1 through 5?
my_list = (1, 2, 3, 4, 5)
my_list = [1, 2, 3, 4, 5]
my_list = 1, 2, 3, 4, 5
my_list = "1, 2, 3, 4, 5"
Which of the following code snippets will correctly convert a string to a list?
my_string = "hello"
str_as_list = list(my_string)
my_string = "hello"
str_as_list = listify(my_string)
my_string = "hello"
str_as_list = make_list(my_string)
my_string = "hello"
str_as_list = new_list(my_string)
What does the following code print?
time_of_day = ["morning", "afternoon", "evening"]
for word in time_of_day:
print("Good " + word)
Good morning, Good afternoon, Good evening
Good morning
Good afternoon
Good evening
Good morning
Good morning
Good morning
Good morning
What code below will print the following?
Letter 1: T
Letter 2: i
Letter 3: m
name = "Tim"
name_list = list(name)
for index, value in enumerate(name_list):
print("Letter " + str(index+1) + ": " + value)
name = "Tim"
name_list = list(name)
for index, value in (name_list):
print("Letter " + str(index+1) + ": " + value)
name = "Tim"
name_list = list(name)
for index in enumerate(name_list):
print("Letter " + str(index) + ": " + value)
name = "Tim"
name_list = list(name)
for index, value in enumerate(name_list):
print(value + str(index))
Look at the following program:
my_list = ["bananas", "oranges", "grapes", "pineapples", "apples"]
# You pick the code that goes here...
# ...
# ...
print(my_list)
Pick the code that results in the following output:
['apples', 'bananas', 'grapes', 'oranges', 'pineapples']
my_list.sort()
my_list.reverse()
my_list.sort()
my_list.reverse()
my_list.remove("grapes")
Look at the following program:
my_list = ["bananas", "oranges", "grapes", "pineapples", "apples"]
# You pick the code that goes here...
# ...
# ...
print(my_list)
Pick the code that results in the following output:
['pineapples', 'oranges', 'grapes', 'apples']
my_list.remove("bananas")
my_list.sort()
my_list.remove("bananas")
my_list.reverse()
my_list.remove("bananas")
my_list.sort()
my_list.reverse()
my_list.remove("bananas")
Refer to the following program. Which statement is accurate?
class State:
def __init__(self):
self.name = ""
self.capital = ""
s = State()
s.name = "Washington"
s.capital = "Olympia"
State is a class, and s is an instance.
State is an instance, and s is a class.
Which of the following programs correctly creates a class called Bell with a method called ring that prints Ding!?
class Bell:
def __init__(self):
self.sound = "Ding!"
def ring(self):
print(self.sound)
class Bell:
def __init__(self):
self.ring("Ding!")
class Bell:
def __init__(self):
self.ring = "Ding!"
class Bell:
def __init__(self):
self.sound = "Ding!"
def ring():
print(sound)
Assuming you have the class and method described in question 1, which of the following lines correctly instantiates a Bell and calls the ring method?
b = Bell()
ring(b)
Bell()
ring(Bell)
b = Bell()
b.ring()
b = Bell()
ring.b()
Which method can be implemented in order to make instances of a class comparable using ==?
__==__
__equals__
__eq__
__bool__
Which method can be implemented in order to make instances of a class directly printable?
__print__
__printable__
__repr__
__string__
Which of the following is the correct name of the method that allows for the use of the + operator between two instances of a class?
__add__
__+__
__plus__
__concat__
Which of the following best describes the parameters in the method from the previous question?
It has one parameter, which should not be self.
It has one parameter, which should be self.
It has two parameters, neither of which should be self.
It has two parameters, one of which should be self.
Explore all questions with a free account