wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java-Basics

Total questions: 21

Worksheet time: 11mins

Name
Class
Date
1.

You run the code below from the editor.

type(5)

print(3.0-1)

What's printed?

a)

int

b)

2.0

c)

int then 2.0

d)

nothing

2.

Which is allowed in Python?

a)

x + y = 2

b)

x*x = 2

c)

2 = x

d)

xy = 2

e)

None of the Above

3.

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?

a)

74 then 74

b)

74 then 75

c)

74

d)

75

4.

Strings

What is the value of variable `u` from the code below?

once = "umbr"

repeat = "ella"

u = once + (repeat+" ")*4

a)

umbrella ella ella ella

b)

umbrellaellaellaella

c)

umbrella

d)

umbrella4

5.

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)

a)

False then False

b)

False then True

c)

True then False

d)

True then True

6.

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")

a)

x is smaller

b)

y is smaller

c)

x / y is 0.0

7.

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!")

a)

You're in the Lost Forest. Go left or right?

b)

You got out of the Lost Forest!

8.

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)

a)

5

b)

6

c)

21

d)

24

9.

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)

a)

260000

b)

26100

c)

26 100

d)

nothing, it will give an error

e)

6.00 is 6.0001 and 6.0002

10.

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

a)

4

b)

6

c)

7

d)

8

e)

10

11.

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))

a)

0

b)

2

c)

4

d)

5

12.

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)

a)

4

b)

8

c)

16

d)

nothing, it will show an error

13.

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)

a)

('sunny', 'cc')

b)

('sunny', 'ccold')

c)

('sunny', 'cloudycold')

14.

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"

a)

["life", "answer", 42, 0]

b)

["universe", "answer", 42, 0]

c)

["universe", "everything", 42, 0]

d)

["life", "everything", 42, 0]

15.

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'])

a)

['mi', 're', ['fa', 'la']]

b)

['mi', 're', 'fa', 'la']

c)

['re', 'mi', ['fa', 'la']]

d)

['do', 'mi', ['fa', 'la']]

16.

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)

a)

['bacon', 'eggs', 'toast', 'jam']

b)

['bacon', 'eggs', 'juice', 'toast', 'jam']

c)

['bacon', 'eggs', 'juice', ['toast', 'jam']]

d)

['bacon', 'eggs', ['toast', 'jam']]

17.

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?

a)

n is very large (and positive)

b)

n is very small (and negative)

c)

n is 0

18.

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()

a)

You are not allowed to name an integer with the variable name L

b)

range is not allowed to have an expression inside its parentheses

c)

You are not allowed to call len on an integer

d)

You are not allowed to print the loop variable i

19.

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?

a)

prints "You've gone through 0.0 % of your life!"

b)

prints "Division by zero."

20.

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.")

a)

prints "You've gone through 25.0 % of your life!"

b)

prints "Oops, must enter a number."

21.

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

a)

Yes

b)

No