Font size
Worksheets12_CS_2_FinalExam
Total questions: 137
Worksheet time: 2hrs 36mins
Mr. Cusack is a computer Science teacher at Klein Collins High School?
True
False
What function is used to display text on the screen in Python?
(a)
What function is used to get input from the user?
(a)
What data type does the input() function return?
(a)
What is the correct syntax to assign the value 10 to a variable named score?
(a)
What will be the output of the following code?
x = 5
print(x + 2)
(a)
If the user enters '7', what is the value of num after num = input('Enter a number: ')?
(a)
How can you convert the input from question 6 to an integer?
(a)
What is the difference between print('5') and print(5)?
Which operator is used for assignment in Python?
(a)
Write a Python statement that assigns the string 'Hello World' to a variable named msg.
What keyword begins a conditional block in Python?
(a)
What will the following code print?
x = 10
if x > 5:
print('High')
else:
print('Low')
(a)
What is the purpose of the elif keyword?
How do you check if a variable age is equal to 18?
(a)
Write a simple if-else that prints 'Even' if n is even, otherwise 'Odd'.
What loop is typically used when you know the number of repetitions ahead of time?
(a)
Which loop repeats as long as a condition remains true?
(a)
What is the output of:
for i in range(3):
print(i)
(a)
What will this while loop print?
count = 0
while count < 3:
print(count)
count += 1
(a)
What does range(5) produce?
What keyword is used to define a function in Python?
(a)
What keyword is used to send a value back from a function?
(a)
What will be printed?
def add(a, b):
return a + b
print(add(2, 3))
(a)
Write a function called square() that returns the square of a number.
What happens if a function has no return statement?
How do you create an empty list in Python?
[]
mylist = []
What method adds a new element to the end of a list?
(a)
What is the output of the following code?
nums = [1, 2, 3]
nums.append(4)
print(len(nums))
(a)
How would you access the first element of a list named fruits?
(a)
What does mylist[1:3] return if mylist = [10, 20, 30, 40]?
(a)
How do you create an empty dictionary?
{}
mydict = {}
What syntax is used to access the value of the key "name" in person = {"name": "Alice", "age": 25}?
(a)
How do you add a new key-value pair "grade": "A" to a dictionary named student?
What does the keys() method return?
What does the values() method return?
What does len("Python") return?
(a)
What does "hello".upper() return?
(a)
What does "HELLO".lower() return?
(a)
What will "a,b,c".split(",") produce?
(a)
What does "-".join(["a", "b", "c"]) return?
(a)
How do you convert "25" into an integer?
(a)
How do you convert 3.14 into a string?
(a)
What is the difference between / and //?
/ is floating‑point division
// is floor division
What is the result of 2 ** 3?
(a)
What does 10 % 3 evaluate to?
(a)
What will be the result of 5 != 5?
(a)
What does and do in a conditional expression?
What will not True return?
(a)
What is the result of (5 > 3) or (2 > 10)?
(a)
What is the difference between == and =?
== compares equality
= assigns a value
A example of a logical operator is
A example of a logical operator is
A example of a logical operator is
To create an alias
(a)
For debugging
(a)
To break out of a loop
(a)
To define a class
(a)
To continue to the next iteration of a loop
(a)
To define a function
(a)
To delete an object
(a)
Used with exceptions, what to do when an exception occurs
(a)
Used with exceptions, a block of code that will be executed no matter if there is an exception or not
(a)
To create a for loop
(a)
To import specific parts of a module
(a)
To declare a global variable
(a)
To import a module
(a)
To check if a value is present in a list, tuple, etc.
(a)
To test if two variables are equal
(a)
To create an anonymous function
(a)
Represents a null value
(a)
To declare a non-local variable
(a)
A null statement, a statement that will do nothing
(a)
To raise an exception
(a)
To exit a function and return a value
(a)
To make a try...except statement
(a)
To create a while loop
(a)
Used to simplify exception handling
(a)
To end a function, returns a generator
(a)
Which of the following is a mutable data type in Python?
tuple
list
str
int
What is the output type of the expression 3.5+2 in Python?
int
str
float
bool
Which keyword is used to define a function in Python?
func
def
function
define
Which of the following is used to open a file for reading in Python?
open('file.txt', 'w')
open('file.txt', 'r')
open('file.txt', 'a')
open('file.txt', 'x')
Which control structure is used to repeat a block of code a specific number of times?
if
for
break
continue
What is the result of the following code? ```python x = 5 y = 2 print(x // y) ```
2.5
2
3
0
Which of the following is NOT a valid Python data type?
float
char
bool
list
Which method is used to add an item to the end of a list in Python?
add()
append()
insert()
extend()
Which statement will correctly close a file in Python?
file.close()
close(file)
file.end()
end(file)
What is the boolean value of the expression 5>10 in Python?
True
False
None
0
Which of the following will create a dictionary in Python?
dict = [1, 2, 3]
dict = (1, 2, 3)
dict = {'a': 1, 'b': 2}
dict = 'a, b, c'
Which control structure is used to make decisions in Python?
for
if
while
break
Which function returns the length of a list in Python?
length()
count()
len()
size()
Which of the following is the correct way to write a comment in Python?
// This is a comment
# This is a comment
/* This is a comment */
Which method is used to read all lines from a file in Python?
read()
readline()
readlines()
getlines()
Given the list `numbers = [1, 2, 3, 4]`, which code will print each number doubled?
for n in numbers: print(n * 2)
for n in numbers: print(n + 2)
for n in numbers: print(n / 2)
for n in numbers: print(n - 2)
Given the string `s = "Python"`, which code will print the first three characters?
print(s[0:3])
print(s[1:4])
print(s[0:2])
print(s[3:6])
Which code will check if a number is even and print "Even" if it is?
if number % 2 == 0: print("Even")
if number / 2 == 0: print("Even")
if number % 2 != 0: print("Even")
if number == 2: print("Even")
Given a function defined as `def add(a, b): return a + b`, what is the output of `add(3, 5)`?
8
35
53
15
Which code will write "Hello, World!" to a file named `greet.txt`?
open('greet.txt', 'w').write("Hello, World!")
open('greet.txt', 'r').write("Hello, World!")
open('greet.txt', 'a').read("Hello, World!")
open('greet.txt', 'x').read("Hello, World!")
Given the list `fruits = ['apple', 'banana', 'cherry']`, which code will remove 'banana' from the list?
fruits.remove('banana')
fruits.delete('banana')
fruits.pop('banana')
fruits.erase('banana')
Which code will read the contents of a file and print each line?
for line in open('file.txt'): print(line)
print(open('file.txt').readline())
print(open('file.txt').read())
open('file.txt').write(line)
Given the dictionary `d = {'a': 1, 'b': 2}`, which code will print all keys?
print(d.keys())
print(d.values())
print(d.items())
print(d.get())
Which code will define a function that returns the square of a number?
def square(x): return x * x
def square(x): return x + x
def square(x): return x / x
def square(x): return x - x
Which code will check if a file exists before opening it for reading?
import os; if os.path.exists('file.txt'): open('file.txt', 'r')
open('file.txt', 'r')
if 'file.txt' in os.listdir(): open('file.txt', 'w')
open('file.txt', 'x')
Write a Python function that takes a list of numbers and returns a new list containing only the even numbers.
def even_numbers(lst): return [x for x in lst if x % 2 == 0]
def even_numbers(lst): return [x for x in lst if x % 2 != 0]
def even_numbers(lst): return [x for x in lst if x > 0]
def even_numbers(lst): return [x for x in lst if x < 0]
You have a file containing numbers, one per line. Write Python code to read the file and calculate the sum of all numbers.
total = sum([int(line) for line in open('numbers.txt')])
total = sum([float(line) for line in open('numbers.txt')])
total = sum([str(line) for line in open('numbers.txt')])
total = sum([line for line in open('numbers.txt')])
Write a Python function that takes a string and returns True if it is a palindrome (reads the same forwards and backwards), otherwise False.
def is_palindrome(s): return s == s[::-1]
def is_palindrome(s): return s == s[1:]
def is_palindrome(s): return s == s[-1]
def is_palindrome(s): return s == s[::2]
Given a list of numbers, write Python code to find and print the largest number without using the built-in `max()` function.
largest = numbers[0]; for n in numbers: if n > largest: largest = n; print(largest)
largest = numbers[0]; for n in numbers: if n < largest: largest = n; print(largest)
largest = 0; for n in numbers: if n > largest: largest = n; print(largest)
largest = 0; for n in numbers: if n < largest: largest = n; print(largest)
Write Python code to count how many times each word appears in a text file and print the results.
counts = {}; for line in open('file.txt'): for word in line.split(): counts[word] = counts.get(word, 0) + 1; print(counts)
counts = {}; for line in open('file.txt'): for word in line.split(): counts[word] = counts.get(word, 0) - 1; print(counts)
counts = {}; for line in open('file.txt'): for word in line.split(): counts[word] = 1; print(counts)
counts = {}; for line in open('file.txt'): for word in line.split(): counts[word] = 0; print(counts)
