WorksheetsCCS40 Midterms
Total questions: 99
Worksheet time: 55mins
How do you insert comments in Python?
#This is a comment
/This is a comment/
//This is a comment
Which one is not a legal variable name?
my_var
my-var
Myvar
_myvar
What is the correct file extension for Python files?
pyt
pyth
py
pt
What is the correct syntax to output the type of a variable x in Python
print(typeOf(x))
print(type(x))
print(typeof(x))
print(typeof x)
In Python, 'Hello' and "Hello" are the same.
True
False
What is the value of the following expression?
3**4
81
64
27
12
Which operator can be used to compare two values?
==
=
<>
<
How do you correctly start writing an if statement in Python?
if x>y:
if x>y {
if (x>y)
if x>y then:
What is the output of the following program?
a = 100
b = 50
if a<b:
m=a
else:
m=b
print(m)
Error
50
100
a = 50
b = 10
_ a _ b:
print("A is GREAT")
_ :
print("B is GREAT")
(a)
The following program is supposed to print "Yes" if a is equal to b, otherwise print "No".
a = 50
b = 10
_ a _ b:
print("Yes")
_ :
print("No")
(a)
Print "Hello" if a is equal to b, and c is equal to d.
if a == b _ c == d:
print("Hello")
(a)
Python variable names can start with a letter, number, or underscore.
True
False
A floating point number is a type that represents numbers with decimal values
True
False
Which of the following will output the result 36?
6%%6
6**6
6+6+6+6+6
6*6
a= 12
b = a-10
print(a > b)
False
10
True
12
Which of the following is not a comparison operator
=<
==
=
!=
Which of the following is NOT a logical/boolean operator?
not
no
and
or
Choose two (2) operators that will print the result of 4:
a = 24
b = 5
print(a _ b)
%
/
**
//
What is the result of the following expression?
3**3%3
0
1
3
9
Which of the following is NOT a valid variable name?
tRuE
true
True_
True
What is the output of the following statement?
print(int(3.1415926535897))
3.1416
3.00
3
3.1415926535897
What is the data type of:
False
(a)
What is the data type of:
"False"
(a)
What is the data type of:
1
(a)
What is the data type of:
1.0
(a)
What is the output of the following code?
a = 10
b = 20
c = 10
print( a==c and b==c)
False
True
Which variable names reflect an illegal variable name declaration? (Choose two)
lastName
last-name
last Name
last_Name
What is the output of the following code?
print("a","b","c", sep = "sep")
abc
asepbsepc
asepbsepcsep
a b c
What is the output of the following code?
a=b=c=1
print(a+b+c)
111
3
No output due to an error in the code.
1
What is the output of the following code?
a='25'
b=25
print(a+b)
25 25
50
Error
What is the output of the following program?
name="JAMES"
num=3
print(name*num)
JAMES 333
JAMESJAMESJAMES
No output due to an error in code
JAMES 3
What is the correct syntax to output "Hello World" in Python?
println("Hello World")
echo("Hello World")
print("Hello World")
p("Hello World")
What will this code print?
x = 0
if x:
print("Yes")
else:
print("No")
Traceback
No
0
Yes
How many asterisks will the following code snippet print?
i = 0
while i <= 3 :
i += 2
print("*")
three
two
one
zero
How many asterisks will the following code snippet print?
i = 0
while i <= 5 :
i += 1
if i % 2 == 0:
break
print("*")
one
zero
three
two
How many hash symbols will the following code snippet print?
for i in range(1):
print("#")
else:
print("#")
zero
two
three
one
var = 0
while var < 6:
var += 1
if var % 2 == 0:
continue
print("#")
two
three
zero
one
What is the output of the following code snippet?
for i in range(-1,2):
print(i**2, end="")
101
-102
-101
012
What is the output of the following program?
lang = ["Python","Java","C#","HTML","PHP"]
for x in lang:
if x=="C#":
break
print(x)
C#
HTML
HTML
PHP
Java
C#
Python
Java
What is the output of the following code?
for week in range(1,3):
print("You should walk for",week*10,"minutes")
You should walk for 0 minutes
You should walk for 10 minutes
You should walk for 10 minutes
You should walk for 20 minutes
You should walk for 0 minutes
You should walk for 10 minutes
You should walk for 20 minutes
You should walk for 10 minutes
You should walk for 20 minutes
You should walk for 30 minutes
What is the output of the following program?
i = 1
while i < 4:
print(i)
if i == 2:
break
i += 1
1
2
0
1
0
1
2
1
2
3
What is the iteration variable in the following Python code?
for letter in 'banana' :
print(letter)
in
letter
for
banana
A while loop in Python is used for what type of iteration?
definite
indefinite
What is the output of the following for loop?
for num in range(2,-5,-1):
print(num, end=", ")
2, 1, 0
2, 1, 0, -1, -2, -3, -4
2, 1, 0, -1, -2, -3, -4, -5
What is the output of the following nested loop?
for num in range(10, 14):
for i in range(2, num):
if num%i == 1:
print(num)
break
10
11
12
13
11
13
What is the output of the following while loop?
x = 0
while (x < 10):
x+=2
print(x)
2
4
6
8
Error
10
0
2
4
6
8
10
What is the output of the following snippet?
for l in 'James':
if l == 'm':
break
print(l, end=", ")
J, a, m,
J,
J, a,
m,
Which of the following are the two (2) primitive loop commands in Python?
while
repeat
do
for
With the break statement we can stop the loop even if the while condition is true.
True
False
What is the output of the following code snippet?
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
1
2
3
0
1
2
What does the continue statement do?
Resets the iteration variable to its initial value
Exits the program
Exits the currently executing loop
Goes back to the "top" of the loop and starts the next iteration
tot = 0
for i in [5, 4, 3, 2, 1] :
tot = tot + 1
print(tot)
10
0
5
15
n = 0
while n > 0 :
print('Lather')
print('Rinse')
Infinite loop
0
5
1
Choose the correct codes to complete the ff. program that prints the highest number in mylist:
mylist = [5, 2, 9, -1, 6, 8, 90, 74]
highest = _
for i in mylist[1:]:
if _:
_
print("The highest number is",highest)
(a)
Which code should be filled in the blank to output a histogram?
num = int(input("Enter a number: "))
for i in range(_):
print(i, "*" i)
1 *
2 **
3 ***
1, num
1, num+1
0, num
num
What is the time complexity of the following function?
def print_items(n):
for i in range(n):
print(i)
O(1)
O(n)
O(log n)
O(n2)
def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return _
elif arr[mid] < target:
low = _
else:
high = _
return -1
arr[mid], mid - 1, mid + 1
mid, mid + 1, mid - 1
target, low + 1, high - 1
mid, low - 1, high + 1
Given the following linear search function:
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
numbers = [5, 8, 2, 7]
print(linear_search(numbers, 2))
2
-1
3
1
What will be the output of the following program?
def mystery(a, b):
if a > b:
return a - b
else:
return a + b
x = mystery(10, 4)
y = mystery(3, 7)
print(x + y)
16
6
20
14
What will be the output of the following program?
def compute(n):
total = 0
for i in range(1, n+1):
total += i * 2
return total
print(compute(3))
14
10
12
6
What is the output of the following code?
count = 0
for i in range(5):
count += i if i % 2 == 0 else 0
print("Final count:", count)
Final count: 0
Final count: 6
Final count: 10
Final count: 4
What is the output of the following code?
def mystery(*args):
total = 0
for num in args:
total += num * (num % 2)
return total
print("Result:", mystery(3, 4, 7, 2, 5))
Results: 21
Results: 19
Results: 15
Results: 10
What is the output of the following code?
words = ["hi", "hello", "hey"]
result = ""
for word in words:
if len(word) > 2:
result += word.upper() + " "
else:
result += word + " "
print(result.strip())
HI HELLO HEY
hi HELLO HEY
hi HELLO hey
hi hello hey
This code prints the numbers 0, 1, 2, 3, 4 (each on its own line):
for i in range(5):
if i == 3:
break
print(i)
True
False
The following code will print "Done" exactly once and then stop:
i = 0
while i < 3:
print("Done")
i -= 1
True
False
Which Python keyword indicates the start of a function definition?
break
help
sweet
def
What does the following code print out?
def thing():
print('Hello')
print('There')
Hello
There
defthing
thingHelloThere
In the following Python code, which of the following are the "arguments" to a function?
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
print, my_function
fname, lname
def, my_function
"Emil", "Refsnes"
What will the following Python code print out?
def func(x) :
print(x)
func(10)
func(20)
20
10
10
20
10
20
What will the following Python program print out?
def greet(lang):
if lang == 'es':
return 'Hola'
elif lang == 'fr':
return 'Bonjour'
else:
return 'Hello'
print(greet('fr'),'Michael')
Hola Michael
Bonjour Michael
fr Michael
Hello Michael
What happens when you try to invoke a function before you define it? Example:
hi()
def hi():
print("hi!")
Error
it will still can the function hi()
The input() function is an example of a:
built-in function
user-defined function
What does the following Python Program print out?
str1 = "Hello"
str2 = 'there'
bob = str1 + str2
print(bob)
Hello
0
Hellothere
Hello there
What is the output of the following code?
def hi():
print("hi")
hi(5)
Error
hihihihihi
What is the output of the ff. code?
def introduction(firstName, lastName):
print("Hello, my name is", firstName, lastName)
introduction(firstName = "James", lastName = "Bond")
introduction(lastName = "Skywalker", firstName = "Luke")
Hello, my name is James Bond
Hello, my name is Luke Skywalker
Hello, my name is James Bond
Hello, my name is Skywalker Luke
What is the output of the following snippet?
def intro(a, b="Bond"):
print("My name is", b + ".", a + ".")
intro("Susan")
My name is Susan. Bond.
My name is Bond. Susan.
What is the output of the following snippet?
def sum(a, b=2, c):
print(a + b + c)
sum(a=1, c=3)
Error
6
What is the output of the ff. code?
def myFunction(n):
print("I got", n)
n += 1
print("I have", n)
var = 1
myFunction(var)
print(var)
I got 1
I have 2
2
I got 1
I have 2
1
What is the output of the ff. code?
def isItATriangle(a, b, c):
if a + b <= c:
return False
if b + c <= a:
return False
if c + a <= b:
return False
return True
print(isItATriangle(1, 1, 1))
True
False
What is the output of the ff. code?
def fun(inp=2, out=3):
return inp * out
print(fun(out=2))
6
2
Error
4
What is the output of the ff. code?
def myfunc(x=2, y=3):
print(x+y)
myfunc(y=10)
13
12
5
Error
What is the output of the following code?
def displayPerson(*person):
print(person[2])
displayPerson("Adam", "Jack", "Joe","Logan")
Adam
Jack
Joe
Logan
What is the output of the ff. code?
def fun1(name, age):
print(name, age)
fun1(23, "Emma")
23 Emma
Emma 23
max() and min() are examples of what type of function?
built-in functions
user-defined functions
What is the output of the ff. code?
def my_function(**candy):
print("My favorite candy is " + candy["cotton"])
my_function(choco = "Hershey's", cotton = "Tutti Fruiti", chewy = "Tootsie Roll", grains="Fruit and Nut")
My favorite candy is Hershey's
My favorite candy is Tutti Fruiti
My favorite candy is Tootsie Roll
What is the output of the ff. code?
firstName = "Mark"
lastName = "Zuckerburg"
print(len(firstName+lastName))
MarkZuckerberg
14
Mark Zuckerberg
15
A junior programmer has coded a function to compute the area of a rectangle. The code does not work. Look over the code and choose the two problems with the code:
function area(x,y):
z = x*y
print(z)
area(5,10)
The colon is not necessary after the function definition
The two lines under the function declaration need to be indented
The function keyword should be replaced with def
The call keyword needs to be added to the call to the area function
What is the output of the ff. code?
def fun1(num):
return num + 25
fun1(5)
print(num)
5
Error
25
Given the ff. function fun1(). Select ALL the correct function calls.
def fun1(name, age):
print(name, age)
fun1("Emma", age=23)
fun1(age =23, "Emma")
fun1(name="Emma", 23)
fun1(age =23, name="Emma")
What is the output of the ff. code?
def display_person(*args):
for i in range(1,5,2):
print(args[i], end="")
display_person("Harry", 1, "Joe", 2 ,"Hermione", 3, "Kate")
2Hermione
JOe2
Harry1
12
What is the output of the ff. program?
def square(num):
num_squared = num ** 2
return num_squared
print(square(2.5))
5.25
6.25
6.5
4.5
What is the output of the ff. code:
def subtra(a, b):
print(a - b)
subtra(a=5, 2)
3
Error
-3
2
What is the output of the ff. statement:
print(max('ANGELES university FoUnDaTiOn'))
T
y
A
n
Which of the following recursive functions correctly calculates the factorial of a positive integer n in Python?
def factorial(n):
return n - 1
def factorial(n):
if n == 0:
return 0
else:
return n * factorial(n - 1)
def factorial(n):
return n * factorial(n)
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
In a recursive function, the base case is necessary to stop the function from calling itself indefinitely.
True
False
The recursive case is the part of the function that directly returns a fixed value without making any further function calls.
True
False
Which line represents the recursive call in the following function that calculates the sum of squares from 1 to n?
def sum_of_squares(n):
if n == 0:
return 0
else:
return n**2 + sum_of_squares(n - 1)
if n == 0
return 0
return n**2 + sum_of_squares(n - 1)
n**2
What will be the output of the following Python code?
def trace_list(lst):
for i in range(-1, -4, -1):
print(lst[i], end=" ")
numbers = [10, 20, 30, 40, 50]
trace_list(numbers)
50 40 30
10 20 30
Error
30 40 50
