WorksheetsJava-Basics
Total questions: 21
Worksheet time: 11mins
You run the code below from the editor.
type(5)
print(3.0-1)
What's printed?
int
2.0
int then 2.0
nothing
Which is allowed in Python?
x + y = 2
x*x = 2
2 = x
xy = 2
None of the Above
You run the code below from the file editor.
usa_gold = 46
uk_gold = 27
romania_gold = 1
total_gold = usa_gold + uk_gold + romania_gold
print(total_gold)
romania_gold += 1
print(total_gold)
What's printed?
74 then 74
74 then 75
74
75
Strings
What is the value of variable `u` from the code below?
once = "umbr"
repeat = "ella"
u = once + (repeat+" ")*4
umbrella ella ella ella
umbrellaellaellaella
umbrella
umbrella4
Comparisons
What does the code below print?
pset_time = 15
sleep_time = 8
print(sleep_time > pset_time)
derive = True
drink = False
both = drink and derive
print(both)
False then False
False then True
True then False
True then True
Branching
What's printed when x = 0 and y = 5?
x = float(input("Enter a number for x: "))
y = float(input("Enter a number for y: "))
if x == y:
if y != 0:
print("x / y is", x/y)
elif x < y:
print("x is smaller")
else:
print("y is smaller")
x is smaller
y is smaller
x / y is 0.0
While LoopsIn the code below from Lecture 2, what is printed when you type "Right"?n = input("You're in the Lost Forest. Go left or right? ")
while n == "right":
n = input("You're in the Lost Forest. Go left or right? ")
print("You got out of the Lost Forest!")
You're in the Lost Forest. Go left or right?
You got out of the Lost Forest!
For Loops
What is printed when the below code is run?
mysum = 0
for i in range(5, 11, 2):
mysum += i
if mysum == 5:
break
mysum += 1
print(mysum)
5
6
21
24
String ManipulationsWhat does the code below print?
s = "6.00 is 6.0001 and 6.0002"
new_str = ""
new_str += s[-1]
new_str += s[0]
new_str += s[4::30]
new_str += s[13:10:-1]
print(new_str)
260000
26100
26 100
nothing, it will give an error
6.00 is 6.0001 and 6.0002
For Loops with StringsHow many times will the code below print "common letter"?
s1 = "mit u rock"
s2 = "i rule mit"
if len(s1) == len(s2):
for char1 in s1:
for char2 in s2:
if char1 == char2:
print("common letter")
break
4
6
7
8
10
Function Calls
How many total lines of output will show up if you run the code below?
def add(x, y):
return x+y
def mult(x, y):
print(x*y)
add(1,2)
print(add(2,3))
mult(3,4)
print(mult(4,5))
0
2
4
5
Functions as Arguments
What does the code below print?
def sq(func, x):
y = x**2
return func(y)
def f(x):
return x**2
calc = sq(f, 2)
print(calc)
4
8
16
nothing, it will show an error
Tuples
Examine the code below. What does always_sunny(('cloudy'), ('cold',)) evaluate to?
def always_sunny(t1, t2):
""" t1, t2 are non empty """
sun = ("sunny","sun")
first = t1[0] + t2[0]
return (sun[0], first)
('sunny', 'cc')
('sunny', 'ccold')
('sunny', 'cloudycold')
Simple Lists
What is the value of L after you run the code below?
L = ["life", "answer", 42, 0]
for thing in L:
if thing == 0:
L[thing] = "universe"
elif thing == 42:
L[1] = "everything"
["life", "answer", 42, 0]
["universe", "answer", 42, 0]
["universe", "everything", 42, 0]
["life", "everything", 42, 0]
List Operations
What is the value of L3 after you execute all the operations in the code below?
L1 = ['re']
L2 = ['mi']
L3 = ['do']
L4 = L1 + L2
L3.extend(L4)
L3.sort()
del(L3[0])
L3.append(['fa','la'])
['mi', 're', ['fa', 'la']]
['mi', 're', 'fa', 'la']
['re', 'mi', ['fa', 'la']]
['do', 'mi', ['fa', 'la']]
List Aliasing/Mutation
What is the value of brunch after you execute all the operations in the code below?
L1 = ["bacon", "eggs"]
L2 = ["toast", "jam"]
brunch = L1
L1.append("juice")
brunch.extend(L2)
['bacon', 'eggs', 'toast', 'jam']
['bacon', 'eggs', 'juice', 'toast', 'jam']
['bacon', 'eggs', 'juice', ['toast', 'jam']]
['bacon', 'eggs', ['toast', 'jam']]
Black Box and Glass Box Testing
With the below implementation, is the test set "n = 4 | n = -4 | n = 5" path complete?
def is_even(n):
"""
Returns True if a number is even
and False if not
"""
if n > 0 and n % 2 == 0:
return True
elif n < 0 and n % 2 == 0:
return True
else:
return False
With the above implementation, which value for n is incorrectly labeled by is_even?
n is very large (and positive)
n is very small (and negative)
n is 0
Errors
Below is a piece of code and an error shown when running it. What is the problem?
L = 3
for i in range(len(L)):
print(i)
ERROR MESSAGE:
File "C:/Users/Ana/.spyder2-py3/temp.py", line 2, in
for i in range(len(L)):
TypeError: object of type 'int' has no len()
You are not allowed to name an integer with the variable name L
range is not allowed to have an expression inside its parentheses
You are not allowed to call len on an integer
You are not allowed to print the loop variable i
Exceptions
try:
n = int(input("How old are you? "))
percent = round(n*100/80, 1)
print("You've gone through", percent, "% of your life!")
except ValueError:
print("Oops, must enter a number.")
except ZeroDivisionError:
print("Division by zero.")
except:
print("Something went very wrong.")
If the user enters "0" in the code above what does the program do?
prints "You've gone through 0.0 % of your life!"
prints "Division by zero."
If the user enters "twenty" in the code below what does the program do?
try:
n = int(input("How old are you? "))
percent = round(n*100/80, 1)
print("You've gone through", percent, "% of your life!")
except ValueError:
print("Oops, must enter a number.")
except ZeroDivisionError:
print("Division by zero.")
except:
print("Something went very wrong.")
prints "You've gone through 25.0 % of your life!"
prints "Oops, must enter a number."
Black Box and Glass Box Testing
With the below implementation, is the test set "n = 4 | n = -4 | n = 5" path complete?
def is_even(n):
"""
Returns True if a number is even
and False if not
"""
if n > 0 and n % 2 == 0:
return True
elif n < 0 and n % 2 == 0:
return True
else:
return False
Yes
No
