Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

CCS40 Midterms

Total questions: 99

Worksheet time: 55mins

Name
Class
Date
1.

How do you insert comments in Python?

a)

#This is a comment

b)

/This is a comment/

c)

//This is a comment

2.

Which one is not a legal variable name?

a)

my_var

b)

my-var

c)

Myvar

d)

_myvar

3.

What is the correct file extension for Python files?

a)

pyt

b)

pyth

c)

py

d)

pt

4.

What is the correct syntax to output the type of a variable x in Python

a)

print(typeOf(x))

b)

print(type(x))

c)

print(typeof(x))

d)

print(typeof x)

5.

In Python, 'Hello' and "Hello" are the same.

a)

True

b)

False

6.

What is the value of the following expression?

3**4

a)

81

b)

64

c)

27

d)

12

7.

Which operator can be used to compare two values?

a)

==

b)

=

c)

<>

d)

<

8.

How do you correctly start writing an if statement in Python?

a)

if x>y:

b)

if x>y {

c)

if (x>y)

d)

if x>y then:

9.

What is the output of the following program?

a = 100

b = 50

if a<b:

m=a

else:

m=b

print(m)

a)

Error

b)

50

c)

100

10.

a = 50

b = 10

_ a _ b:

print("A is GREAT")

_ :

print("B is GREAT")

(a)  

11.

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)  

12.

Print "Hello" if a is equal to b, and c is equal to d.
if a == b _ c == d:

print("Hello")

(a)  

13.

Python variable names can start with a letter, number, or underscore.

a)

True

b)

False

14.

A floating point number is a type that represents numbers with decimal values

a)

True

b)

False

15.

Which of the following will output the result 36?

a)

6%%6

b)

6**6

c)

6+6+6+6+6

d)

6*6

16.

a= 12

b = a-10

print(a > b)

a)

False

b)

10

c)

True

d)

12

17.

Which of the following is not a comparison operator

a)

=<

b)

==

c)

=

d)

!=

18.

Which of the following is NOT a logical/boolean operator?

a)

not

b)

no

c)

and

d)

or

19.

Choose two (2) operators that will print the result of 4:
a = 24

b = 5

print(a _ b)

a)

%

b)

/

c)

**

d)

//

20.

What is the result of the following expression?

3**3%3

a)

0

b)

1

c)

3

d)

9

21.

Which of the following is NOT a valid variable name?

a)

tRuE

b)

true

c)

True_

d)

True

22.

What is the output of the following statement?

print(int(3.1415926535897))

a)

3.1416

b)

3.00

c)

3

d)

3.1415926535897

23.

What is the data type of:

False

(a)  

24.

What is the data type of:
"False"

(a)  

25.

What is the data type of:

1

(a)  

26.

What is the data type of:

1.0

(a)  

27.

What is the output of the following code?
a = 10

b = 20

c = 10

print( a==c and b==c)

a)

False

b)

True

28.

Which variable names reflect an illegal variable name declaration? (Choose two)

a)

lastName

b)

last-name

c)

last Name

d)

last_Name

29.

What is the output of the following code?
print("a","b","c", sep = "sep")

a)

abc

b)

asepbsepc

c)

asepbsepcsep

d)

a b c

30.

What is the output of the following code?
a=b=c=1

print(a+b+c)

a)

111

b)

3

c)

No output due to an error in the code.

d)

1

31.

What is the output of the following code?
a='25'

b=25

print(a+b)

a)

25 25

b)

50

c)

Error

32.

What is the output of the following program?
name="JAMES"

num=3

print(name*num)

a)

JAMES 333

b)

JAMESJAMESJAMES

c)

No output due to an error in code

d)

JAMES 3

33.

What is the correct syntax to output "Hello World" in Python?

a)

println("Hello World")

b)

echo("Hello World")

c)

print("Hello World")

d)

p("Hello World")

34.

What will this code print?

x = 0

if x:

print("Yes")

else:

print("No")

a)

Traceback

b)

No

c)

0

d)

Yes

35.

How many asterisks will the following code snippet print?

i = 0
while i <= 3 :
    i += 2
    print("*")

a)

three

b)

two

c)

one

d)

zero

36.

How many asterisks will the following code snippet print?

i = 0
while i <= 5 :
    i += 1
    if i % 2 == 0:
        break
    print("*")

a)

one

b)

zero

c)

three

d)

two

37.

How many hash symbols will the following code snippet print?

for i in range(1):
    print("#")
else:
    print("#")

a)

zero

b)

two

c)

three

d)

one

38.

var = 0
while var < 6:
    var += 1
    if var % 2 == 0:
        continue
    print("#")

a)

two

b)

three

c)

zero

d)

one

39.

What is the output of the following code snippet?

for i in range(-1,2):
    print(i**2, end="")

a)

101

b)

-102

c)

-101

d)

012

40.

What is the output of the following program?

lang = ["Python","Java","C#","HTML","PHP"]
for x in lang:
    if x=="C#":
        break
    print(x)

a)

C#

HTML

b)

HTML

PHP

c)

Java

C#

d)

Python

Java

41.

What is the output of the following code?

for week in range(1,3):
    print("You should walk for",week*10,"minutes")

a)

You should walk for 0 minutes
You should walk for 10 minutes

b)

You should walk for 10 minutes
You should walk for 20 minutes

c)

You should walk for 0 minutes
You should walk for 10 minutes
You should walk for 20 minutes

d)

You should walk for 10 minutes
You should walk for 20 minutes
You should walk for 30 minutes

42.

What is the output of the following program?

i = 1
while i < 4:
    print(i)
    if i == 2:
        break
    i += 1

a)

1

2

b)

0

1

c)

0

1

2

d)

1

2

3

43.

What is the iteration variable in the following Python code?

for letter in 'banana' :
    print(letter)

a)

in

b)

letter

c)

for

d)

banana

44.

A while loop in Python is used for what type of iteration?

a)

definite

b)

indefinite

45.

What is the output of the following for loop?

for num in range(2,-5,-1):
      print(num, end=", ")

a)

2, 1, 0

b)

2, 1, 0, -1, -2, -3, -4

c)

2, 1, 0, -1, -2, -3, -4, -5

46.

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

a)

10

11

12

13

b)

11

13

47.

What is the output of the following while loop?

x = 0
while (x < 10):
  x+=2
print(x)

a)

2

4

6

8

b)

Error

c)

10

d)

0

2

4

6

8

10

48.

What is the output of the following snippet?

for l in 'James':
   if l == 'm':
      break
   print(l, end=", ")

a)

J, a, m,

b)

J,

c)

J, a,

d)

m,

49.

Which of the following are the two (2) primitive loop commands in Python?

a)

while

b)

repeat

c)

do

d)

for

50.

With the break statement we can stop the loop even if the while condition is true.

a)

True

b)

False

51.

What is the output of the following code snippet?

i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1

a)

1

2

3

b)

0

1

2

52.

What does the continue statement do?

a)

Resets the iteration variable to its initial value

b)

Exits the program

c)

Exits the currently executing loop

d)

Goes back to the "top" of the loop and starts the next iteration

53.

tot = 0
for i in [5, 4, 3, 2, 1] :
    tot = tot + 1
print(tot)

a)

10

b)

0

c)

5

d)

15

54.

n = 0
while n > 0 :
    print('Lather')
    print('Rinse')

a)

Infinite loop

b)

0

c)

5

d)

1

55.

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)  

56.

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

a)

1, num

b)

1, num+1

c)

0, num

d)

num

57.

What is the time complexity of the following function?

def print_items(n):
    for i in range(n):
        print(i)

a)

O(1)

b)

O(n)

c)

O(log n)

d)

O(n2)

58.

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

a)

arr[mid], mid - 1, mid + 1

b)

mid, mid + 1, mid - 1

c)

target, low + 1, high - 1

d)

mid, low - 1, high + 1

59.

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

a)

2

b)

-1

c)

3

d)

1

60.

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)

a)

16

b)

6

c)

20

d)

14

61.

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

a)

14

b)

10

c)

12

d)

6

62.

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)

a)

Final count: 0

b)

Final count: 6

c)

Final count: 10

d)

Final count: 4

63.

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

a)

Results: 21

b)

Results: 19

c)

Results: 15

d)

Results: 10

64.

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

a)

HI HELLO HEY

b)

hi HELLO HEY

c)

hi HELLO hey

d)

hi hello hey

65.

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)

a)

True

b)

False

66.

The following code will print "Done" exactly once and then stop:

i = 0
while i < 3:
    print("Done")
    i -= 1

a)

True

b)

False

67.

Which Python keyword indicates the start of a function definition?

a)

break

b)

help

c)

sweet

d)

def

68.

What does the following code print out?

def thing():
    print('Hello')
print('There')

a)

Hello

b)

There

c)

defthing

d)

thingHelloThere

69.

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

a)

print, my_function

b)

fname, lname

c)

def, my_function

d)

"Emil", "Refsnes"

70.

What will the following Python code print out?

def func(x) :
    print(x)
func(10)
func(20)

a)

20

10

b)

10

20

c)

10

d)

20

71.

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

a)

Hola Michael

b)

Bonjour Michael

c)

fr Michael

d)

Hello Michael

72.

What happens when you try to invoke a function before you define it? Example:

hi()

def hi():
    print("hi!")

a)

Error

b)

it will still can the function hi()

73.

The input() function is an example of a:

a)

built-in function

b)

user-defined function

74.

What does the following Python Program print out?

str1 = "Hello"
str2 = 'there'
bob = str1 + str2
print(bob)

a)

Hello

b)

0

c)

Hellothere

d)

Hello there

75.

What is the output of the following code?

def hi():
    print("hi")

hi(5)

a)

Error

b)

hihihihihi

76.

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

a)

Hello, my name is James Bond
Hello, my name is Luke Skywalker

b)

Hello, my name is James Bond
Hello, my name is Skywalker Luke

77.

What is the output of the following snippet?

def intro(a, b="Bond"):
    print("My name is", b + ".", a + ".")

intro("Susan")

a)

My name is Susan. Bond.

b)

My name is Bond. Susan.

78.

What is the output of the following snippet?

def sum(a, b=2, c):
    print(a + b + c)

sum(a=1, c=3)

a)

Error

b)

6

79.

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)

a)

I got 1
I have 2
2

b)

I got 1
I have 2
1

80.

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

a)

True

b)

False

81.

What is the output of the ff. code?

def fun(inp=2, out=3):
    return inp * out

print(fun(out=2))

a)

6

b)

2

c)

Error

d)

4

82.

What is the output of the ff. code?

def myfunc(x=2, y=3):
    print(x+y)

myfunc(y=10)

a)

13

b)

12

c)

5

d)

Error

83.

What is the output of the following code?

def displayPerson(*person):
    print(person[2])

displayPerson("Adam", "Jack", "Joe","Logan")

a)

Adam

b)

Jack

c)

Joe

d)

Logan

84.

What is the output of the ff. code?

def fun1(name, age):
     print(name, age)

fun1(23, "Emma") 

a)

23 Emma

b)

Emma 23

85.

max() and min() are examples of what type of function?

a)

built-in functions

b)

user-defined functions

86.

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

a)

My favorite candy is Hershey's

b)

My favorite candy is Tutti Fruiti

c)

My favorite candy is Tootsie Roll

87.

What is the output of the ff. code?

firstName = "Mark"
lastName = "Zuckerburg"
print(len(firstName+lastName))

a)

MarkZuckerberg

b)

14

c)

Mark Zuckerberg

d)

15

88.

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)

a)

The colon is not necessary after the function definition

b)

The two lines under the function declaration need to be indented

c)

The function keyword should be replaced with def

d)

The call keyword needs to be added to the call to the area function

89.

What is the output of the ff. code?

def fun1(num):
    return num + 25

fun1(5)
print(num)

a)

5

b)

Error

c)

25

90.

Given the ff. function fun1().  Select ALL the correct function calls.

def fun1(name, age):
    print(name, age)

a)

fun1("Emma", age=23)

b)

fun1(age =23, "Emma")

c)

fun1(name="Emma", 23)

d)

fun1(age =23, name="Emma")

91.

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

a)

2Hermione

b)

JOe2

c)

Harry1

d)

12

92.

What is the output of the ff. program?

def square(num):
    num_squared = num ** 2
    return num_squared

print(square(2.5))

a)

5.25

b)

6.25

c)

6.5

d)

4.5

93.

What is the output of the ff. code:

def subtra(a, b):
     print(a - b)

subtra(a=5, 2)

a)

3

b)

Error

c)

-3

d)

2

94.

What is the output of the ff. statement:

 print(max('ANGELES university FoUnDaTiOn'))

a)

T

b)

y

c)

A

d)

n

95.

Which of the following recursive functions correctly calculates the factorial of a positive integer n in Python?

a)

def factorial(n):
    return n - 1

b)

def factorial(n):
    if n == 0:
        return 0
    else:
        return n * factorial(n - 1)

c)

def factorial(n):
    return n * factorial(n)

d)

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

96.

In a recursive function, the base case is necessary to stop the function from calling itself indefinitely.

a)

True

b)

False

97.

The recursive case is the part of the function that directly returns a fixed value without making any further function calls.

a)

True

b)

False

98.

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)

a)

if n == 0

b)

return 0

c)

return n**2 + sum_of_squares(n - 1)

d)

n**2

99.

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)

a)

50 40 30

b)

10 20 30

c)

Error

d)

30 40 50