COMP II
Master Python with Our Comprehensive Quiz!
Test your Python programming skills with our engaging 40-question quiz designed for students and learners alike. From basic concepts to complex fun
Key Features:
- 40 Multiple Choice Questions
- Instant Feedback on Answers
- Improve Your Coding Skills
greater_than_zero = number > 0 print(type(number))
number = 5
less_than_zero = number < 0
if less_than_zero:
print(number)
number = number - 10
less_than_zero = number < 0
if less_than_zero:
print(number)
number = 5
greater_than_zero = number > 0
less_than_zero = number < 0
if greater_than_zero:
print(number)
if less_than_zero:
print(number + 1)
if greater_than_zero and less_than_zero:
print(number + 2)
if greater_than_zero or less_than_zero:
print(number + 3)
favorite_color = "blue"
if favorite_color != "green":
print "But green is the color of grass!"
if favorite_color == "red":
print "I like red, too!"
if favorite_color == "blue":
print "Blue is the best"
if favorite_color != "yellow":
print "But yellow is the color of school buses!"
But green is the color of grass!
Blue is the best
But yellow is the color of school buses!
I like red, too!
Blue is the best
But yellow is the color of school buses!
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
say_hi(‘Margaret’)
def say_hi(name):
print ‘hi ’ + name
function say_hi(name):
print ‘hi ’ + name
def say_hi(first_name=’John’, last_name=’Doe’):
print ‘hi ’ + first_name + ‘ ’ + last_name
say_hi(‘Polly Pocket’)
I would like a green balloon.
def balloon_choice(color):
print ‘I would like a ’ + color + ‘ balloon.’
def balloon_choice():
print ‘I would like a ’ + color + ‘ balloon.’
balloon_choice(‘green’)
def balloon_choice():
print ‘I would like a green balloon.’
balloon_choice(‘green’)
What will be the output of the following program?
a = 4
def print_something():
a = "code"
print a * 3
print_something()
Consider the following fun
def fn_a(num):
if num > 3:
print "Fizz"
def fn_b(num):
if num > 2 and num < 4:
print "Fizz"
Which for loop would print
Fizz
Fizz
for I in range(6):
fn_b(i+1)
for I in range(6):
fn_a(i)
fn_b(i)
for I in range(6):
fn_a(i)
for I in range(6):
fn_b(i)
What will be printed when the program below is run?
def add_two(x):
return x + 2
def multiply_by_three(x):
return x * 3
def my_function (x):
return add_two(x) + multiply_by_three(x)
print my_function (12)
What is the value of num
when this loop completes?
num = 0
for I in range(2, 8, 2):
num = num + i
Which of the following for loops will give the same values for I as the loop below?
for I in range(10):
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
What will be printed to the screen when this program is run?
num = 100
while num > 0:
for I in range(100, 0, -25):
num = num - I
print num
100
0
-75
-125
-150
What funword
?
first_char = word[0]
word = first_char.______() + word[1:]
Which of the following choices will print AeCl
? Assume the following variables have been defined.
first_name = "Alice"
last_name = "Carmichael"
sentence = "The brown lazy dog"
Which of the following expressions will print “L”?
Let word = "PINEAPPLE"
What is the output of the following program?
sentence = "the dog"
for letter in sentence:
print letter + letter
t
h
e
d
o
g
t
h
e
d
o
g
t
h
e
d
o
g
tt
hh
ee
dd
oo
gg
alt_case("sheep")
should return “sHeEp”.def alt_case(word):
res = ""
for I in range(len(word)):
if I % 2 == 1:
res + word[i].upper()
else:
res + word[i].lower()
return res
def alt_case(word):
res = ""
return word.swapcase()
def alt_case(word):
res = ""
for I in range(word):
if I % 2 == 1:
res = res + word[i].upper()
else:
res = res + word[i].lower()
return res
def alt_case(word):
res = ""
for I in range(len(word)):
if I % 2 == 1:
res = res + word[i].upper()
else:
res = res + word[i].lower()
return res
The variable can be assigned a new value, and the string’s value can be modified.
What is printed out by this program?
word = "killer whale"
print word[0:100]
What does the following program print?
x = "ab" y = "banana" print x in y
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?