wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

12_CS_2_FinalExam

Total questions: 137

Worksheet time: 2hrs 36mins

Name
Class
Date
1.

Mr. Cusack is a computer Science teacher at Klein Collins High School?

a)

True

b)

False

2.

What function is used to display text on the screen in Python?

(a)  

3.

What function is used to get input from the user?

(a)  

4.

What data type does the input() function return?

(a)  

5.

What is the correct syntax to assign the value 10 to a variable named score?

(a)  

6.

What will be the output of the following code? x = 5 print(x + 2)

(a)  

7.

If the user enters '7', what is the value of num after num = input('Enter a number: ')?

(a)  

8.

How can you convert the input from question 6 to an integer?

(a)  

9.

What is the difference between print('5') and print(5)?

4 lines
10.

Which operator is used for assignment in Python?

(a)  

11.

Write a Python statement that assigns the string 'Hello World' to a variable named msg.

4 lines
12.

What keyword begins a conditional block in Python?

(a)  

13.

What will the following code print? x = 10 if x > 5: print('High') else: print('Low')

(a)  

14.

What is the purpose of the elif keyword?

4 lines
15.

How do you check if a variable age is equal to 18?

(a)  

16.

Write a simple if-else that prints 'Even' if n is even, otherwise 'Odd'.

4 lines
17.

What loop is typically used when you know the number of repetitions ahead of time?

(a)  

18.

Which loop repeats as long as a condition remains true?

(a)  

19.

What is the output of: for i in range(3): print(i)

(a)  

20.

What will this while loop print? count = 0 while count < 3: print(count) count += 1

(a)  

21.

What does range(5) produce?

4 lines
22.

What keyword is used to define a function in Python?

(a)  

23.

What keyword is used to send a value back from a function?

(a)  

24.

What will be printed? def add(a, b): return a + b print(add(2, 3))

(a)  

25.

Write a function called square() that returns the square of a number.

4 lines
26.

What happens if a function has no return statement?

4 lines
27.

How do you create an empty list in Python?

a)

[]

b)

mylist = []

28.

What method adds a new element to the end of a list?

(a)  

29.

What is the output of the following code? nums = [1, 2, 3] nums.append(4) print(len(nums))

(a)  

30.

How would you access the first element of a list named fruits?

(a)  

31.

What does mylist[1:3] return if mylist = [10, 20, 30, 40]?

(a)  

32.

How do you create an empty dictionary?

a)

{}

b)

mydict = {}

33.

What syntax is used to access the value of the key "name" in person = {"name": "Alice", "age": 25}?

(a)  

34.

How do you add a new key-value pair "grade": "A" to a dictionary named student?

4 lines
35.

What does the keys() method return?

4 lines
36.

What does the values() method return?

4 lines
37.

What does len("Python") return?

(a)  

38.

What does "hello".upper() return?

(a)  

39.

What does "HELLO".lower() return?

(a)  

40.

What will "a,b,c".split(",") produce?

(a)  

41.

What does "-".join(["a", "b", "c"]) return?

(a)  

42.

How do you convert "25" into an integer?

(a)  

43.

How do you convert 3.14 into a string?

(a)  

44.

What is the difference between / and //?

a)

/ is floating‑point division

b)

// is floor division

45.

What is the result of 2 ** 3?

(a)  

46.

What does 10 % 3 evaluate to?

(a)  

47.

What will be the result of 5 != 5?

(a)  

48.

What does and do in a conditional expression?

4 lines
49.

What will not True return?

(a)  

50.

What is the result of (5 > 3) or (2 > 10)?

(a)  

51.

What is the difference between == and =?

a)

== compares equality

b)

= assigns a value

52.

A example of a logical operator is

a)
and
b)
del
c)
finally
d)
for
e)
in
53.
To create an alias
a)
as
b)
and
c)
for
d)
from
e)
is
54.
For debugging
a)
assert
b)
as
c)
and
d)
global
e)
lambda
55.
To break out of a loop
a)
break
b)
assert
c)
as
d)
and
56.
To define a class
a)
class
b)
break
c)
assert
d)
as
e)
and
57.
To continue to the next iteration of a loop
a)
continue
b)
class
c)
break
d)
assert
e)
as
58.
To define a function
a)
def
b)
continue
c)
class
d)
break
e)
assert
59.
To delete an object
a)
del
b)
def
c)
continue
d)
class
e)
break
60.
Used in conditional statements, same as else if
a)
elif
b)
del
c)
def
d)
continue
e)
class
61.
Used with exceptions, what to do when an exception occurs
a)
except
b)
else
c)
elif
d)
del
e)
def
62.
Used with exceptions, a block of code that will be executed no matter if there is an exception or not
a)
finally
b)
False
c)
except
d)
else
e)
elif
63.
To create a for loop
a)
for
b)
finally
c)
False
d)
except
e)
else
64.
To import specific parts of a module
a)
from
b)
for
c)
finally
d)
False
e)
except
65.
To declare a global variable
a)
global
b)
from
c)
for
d)
finally
e)
False
66.
To make a conditional statement
a)
if
b)
global
c)
from
d)
for
e)
finally
67.
To import a module
a)
import
b)
if
c)
global
d)
from
e)
for
68.
To check if a value is present in a list, tuple, etc.
a)
in
b)
import
c)
if
d)
global
e)
from
69.
To test if two variables are equal
a)
is
b)
in
c)
import
d)
if
e)
global
70.
To create an anonymous function
a)
lambda
b)
is
c)
in
d)
import
e)
if
71.
Represents a null value
a)
None
b)
lambda
c)
is
d)
in
e)
import
72.
To declare a non-local variable
a)
nonlocal
b)
None
c)
lambda
d)
is
e)
in
73.

A example of a logical operator is

a)
not
b)
nonlocal
c)
None
d)
lambda
e)
is
74.

A example of a logical operator is

a)
or
b)
nonlocal
c)
None
d)
lambda
75.
A null statement, a statement that will do nothing
a)
pass
b)
or
c)
not
d)
nonlocal
e)
None
76.
To raise an exception
a)
raise
b)
pass
c)
or
d)
not
e)
nonlocal
77.
To exit a function and return a value
a)
return
b)
raise
c)
pass
d)
or
e)
not
78.
Boolean value, result of comparison operations
a)
True
b)
return
c)
raise
d)
pass
e)
or
79.
To make a try...except statement
a)
try
b)
True
c)
return
d)
raise
e)
pass
80.
To create a while loop
a)
while
b)
try
c)
True
d)
return
e)
raise
81.
Used to simplify exception handling
a)
with
b)
while
c)
try
d)
True
e)
return
82.
To end a function, returns a generator
a)
yield
b)
with
c)
while
d)
try
e)
True
83.

To create an alias

(a)  

84.

For debugging

(a)  

85.

To break out of a loop

(a)  

86.

To define a class

(a)  

87.

To continue to the next iteration of a loop

(a)  

88.

To define a function

(a)  

89.

To delete an object

(a)  

90.

Used with exceptions, what to do when an exception occurs

(a)  

91.

Used with exceptions, a block of code that will be executed no matter if there is an exception or not

(a)  

92.

To create a for loop

(a)  

93.

To import specific parts of a module

(a)  

94.

To declare a global variable

(a)  

95.

To import a module

(a)  

96.

To check if a value is present in a list, tuple, etc.

(a)  

97.

To test if two variables are equal

(a)  

98.

To create an anonymous function

(a)  

99.

Represents a null value

(a)  

100.

To declare a non-local variable

(a)  

101.

A null statement, a statement that will do nothing

(a)  

102.

To raise an exception

(a)  

103.

To exit a function and return a value

(a)  

104.

To make a try...except statement

(a)  

105.

To create a while loop

(a)  

106.

Used to simplify exception handling

(a)  

107.

To end a function, returns a generator

(a)  

108.

Which of the following is a mutable data type in Python?

a)

tuple

b)

list

c)

str

d)

int

109.

What is the output type of the expression 3.5+23.5 + 2 in Python?

a)

int

b)

str

c)

float

d)

bool

110.

Which keyword is used to define a function in Python?

a)

func

b)

def

c)

function

d)

define

111.

Which of the following is used to open a file for reading in Python?

a)

open('file.txt', 'w')

b)

open('file.txt', 'r')

c)

open('file.txt', 'a')

d)

open('file.txt', 'x')

112.

Which control structure is used to repeat a block of code a specific number of times?

a)

if

b)

for

c)

break

d)

continue

113.

What is the result of the following code? ```python x = 5 y = 2 print(x // y) ```

a)

2.5

b)

2

c)

3

d)

0

114.

Which of the following is NOT a valid Python data type?

a)

float

b)

char

c)

bool

d)

list

115.

Which method is used to add an item to the end of a list in Python?

a)

add()

b)

append()

c)

insert()

d)

extend()

116.

Which statement will correctly close a file in Python?

a)

file.close()

b)

close(file)

c)

file.end()

d)

end(file)

117.

What is the boolean value of the expression 5>105 > 10 in Python?

a)

True

b)

False

c)

None

d)

0

118.

Which of the following will create a dictionary in Python?

a)

dict = [1, 2, 3]

b)

dict = (1, 2, 3)

c)

dict = {'a': 1, 'b': 2}

d)

dict = 'a, b, c'

119.

Which control structure is used to make decisions in Python?

a)

for

b)

if

c)

while

d)

break

120.

Which function returns the length of a list in Python?

a)

length()

b)

count()

c)

len()

d)

size()

121.

Which of the following is the correct way to write a comment in Python?

a)

// This is a comment

b)

# This is a comment

c)

d)

/* This is a comment */

122.

Which method is used to read all lines from a file in Python?

a)

read()

b)

readline()

c)

readlines()

d)

getlines()

123.

Given the list `numbers = [1, 2, 3, 4]`, which code will print each number doubled?

a)

for n in numbers: print(n * 2)

b)

for n in numbers: print(n + 2)

c)

for n in numbers: print(n / 2)

d)

for n in numbers: print(n - 2)

124.

Given the string `s = "Python"`, which code will print the first three characters?

a)

print(s[0:3])

b)

print(s[1:4])

c)

print(s[0:2])

d)

print(s[3:6])

125.

Which code will check if a number is even and print "Even" if it is?

a)

if number % 2 == 0: print("Even")

b)

if number / 2 == 0: print("Even")

c)

if number % 2 != 0: print("Even")

d)

if number == 2: print("Even")

126.

Given a function defined as `def add(a, b): return a + b`, what is the output of `add(3, 5)`?

a)

8

b)

35

c)

53

d)

15

127.

Which code will write "Hello, World!" to a file named `greet.txt`?

a)

open('greet.txt', 'w').write("Hello, World!")

b)

open('greet.txt', 'r').write("Hello, World!")

c)

open('greet.txt', 'a').read("Hello, World!")

d)

open('greet.txt', 'x').read("Hello, World!")

128.

Given the list `fruits = ['apple', 'banana', 'cherry']`, which code will remove 'banana' from the list?

a)

fruits.remove('banana')

b)

fruits.delete('banana')

c)

fruits.pop('banana')

d)

fruits.erase('banana')

129.

Which code will read the contents of a file and print each line?

a)

for line in open('file.txt'): print(line)

b)

print(open('file.txt').readline())

c)

print(open('file.txt').read())

d)

open('file.txt').write(line)

130.

Given the dictionary `d = {'a': 1, 'b': 2}`, which code will print all keys?

a)

print(d.keys())

b)

print(d.values())

c)

print(d.items())

d)

print(d.get())

131.

Which code will define a function that returns the square of a number?

a)

def square(x): return x * x

b)

def square(x): return x + x

c)

def square(x): return x / x

d)

def square(x): return x - x

132.

Which code will check if a file exists before opening it for reading?

a)

import os; if os.path.exists('file.txt'): open('file.txt', 'r')

b)

open('file.txt', 'r')

c)

if 'file.txt' in os.listdir(): open('file.txt', 'w')

d)

open('file.txt', 'x')

133.

Write a Python function that takes a list of numbers and returns a new list containing only the even numbers.

a)

def even_numbers(lst): return [x for x in lst if x % 2 == 0]

b)

def even_numbers(lst): return [x for x in lst if x % 2 != 0]

c)

def even_numbers(lst): return [x for x in lst if x > 0]

d)

def even_numbers(lst): return [x for x in lst if x < 0]

134.

You have a file containing numbers, one per line. Write Python code to read the file and calculate the sum of all numbers.

a)

total = sum([int(line) for line in open('numbers.txt')])

b)

total = sum([float(line) for line in open('numbers.txt')])

c)

total = sum([str(line) for line in open('numbers.txt')])

d)

total = sum([line for line in open('numbers.txt')])

135.

Write a Python function that takes a string and returns True if it is a palindrome (reads the same forwards and backwards), otherwise False.

a)

def is_palindrome(s): return s == s[::-1]

b)

def is_palindrome(s): return s == s[1:]

c)

def is_palindrome(s): return s == s[-1]

d)

def is_palindrome(s): return s == s[::2]

136.

Given a list of numbers, write Python code to find and print the largest number without using the built-in `max()` function.

a)

largest = numbers[0]; for n in numbers: if n > largest: largest = n; print(largest)

b)

largest = numbers[0]; for n in numbers: if n < largest: largest = n; print(largest)

c)

largest = 0; for n in numbers: if n > largest: largest = n; print(largest)

d)

largest = 0; for n in numbers: if n < largest: largest = n; print(largest)

137.

Write Python code to count how many times each word appears in a text file and print the results.

a)

counts = {}; for line in open('file.txt'): for word in line.split(): counts[word] = counts.get(word, 0) + 1; print(counts)

b)

counts = {}; for line in open('file.txt'): for word in line.split(): counts[word] = counts.get(word, 0) - 1; print(counts)

c)

counts = {}; for line in open('file.txt'): for word in line.split(): counts[word] = 1; print(counts)

d)

counts = {}; for line in open('file.txt'): for word in line.split(): counts[word] = 0; print(counts)