Font size
WorksheetsCS1101 Python Final Exam practice
Total questions: 103
Worksheet time: 59mins
Consider the following text from a Python interpreter.
>>> print(2 + 2)
4
What is the text ">>>" called?
a) a function
b) an operator
c) a prompt
d) a statement
e) a value
Consider the following text from a Python interpreter.
>>> print(2 + 2)
4
What is the text "print" called?
a) a function
b) an operator
c) a prompt
d) a statement
e) a value
What is Python’s response to the command: type(123)
a) <class 'float'>
b) <class 'bool'>
c) SyntaxError: invalid syntax
d) <class 'int'>
e) <class 'str'>
Match the following- part 1
An error in a program.
bug
The process of finding and removing any of the three kinds of programming errors.
debugging
Any one of the languages that people have designed for specific purposes, such as representing mathematical ideas or computer programs; all programming languages are these kinds of languages.
formal language
A programming language like Python that is designed to be easy for humans to read and write.
high-level language
A program that reads another program and executes it.
interpreter
Match the following - Part 2
A programming language that is designed to be easy for a computer to execute; also called machine language or assembly language.
low-level language
Any one of the languages that people speak that evolved naturally.
natural-language
To examine a program and analyze the syntactic structure.
parse
A property of a program that can run on more than one kind of computer.
portability
An instruction that causes the Python interpreter to display a value on the screen.
print statement
Match the following - Part 3
The process of formulating a problem, finding a solution, and expressing the solution.
problem solving
A sequence of instructions that specifies to a computer actions and computations to be performed.
program
A program stored in a file (usually one that will be interpreted).
script
The structure of a program.
syntax
One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language.
token
Which one of the following Python expressions has the value 64?
a) 8 ^ 2
b) 8 ** 2
c) 8 +- 2
d) 8 += 2
e) 8 -+ 2
Consider the following text from a Python interpreter.
>>> print(2 + 2)
4
What is the text "4" called?
a) a function
b) an operator
c) a prompt
d) a statement
e) a value
Programming languages are ______ languages that have been designed to express computations.
a) informal
b) mathematical
c) formal
d) natural
e) logical
What does the Python interpreter output for the following input?
>>> 1,234.567,890
a) 1234
b) 1234.6
d) 1234.56789
d) (1, 234.567, 890)
e) SyntaxError: invalid token
Which one of the following Python expressions computes the total number of seconds in 21 minutes and 21 seconds?
a) 21 + 21
b) 21 + 60
c) "21 minutes" + "21 seconds"
d) 21 * 60 + 21
e) seconds = 21 + "21 minutes"
What is Python’s response to the command: type(0.123)
a) <class 'float'>
b) <class 'bool'>
c) SyntaxError: invalid syntax
d) <class 'int'>
e) <class 'str'>
Consider the following text from a Python interpreter.
>>> print(2 + 2) 4
What is the text "+" called?
a) a function
b) an operator
c) a prompt
d) a statement
e) a value
In Python, the expression "a**(b**c)" is the same as "(a**b)**c".
True
False
What output will the following Python statements produce?
>>> n = 17
>>> print (n)
a) 0
b) 17.0
c) n
d) 17
What is the output of the following Python statements?
def recurse(a):
if (a == 1):
print(a)
else:
recurse(a)
recurse(1)
a) 0
b) 1
c) no output
d) RuntimeError: maximum recursion depth exceeded
A program is a sequence of instructions that specifies how to perform a computation.
True
False
Which one of the following Python expressions generates a syntax error?
a) 2 ^ 2
b) 2 ** 2
c) 2 +- 2
d) 2 += 2
e) 2 -+ 2
Consider the following text from a Python interpreter.
>>> print(2 + 2) 4
What is the text "4" called?
a) a function
b) operator
c) prompt
d) statement
e) value
Which of the following is an invalid Python assignment statement?
a) a = b = 123
b) ‘3’ = 3
c) x = int(“123”)
d) y = None
e) z = “hi” * 10
The % or modulus operator returns the remainder from dividing two numbers.
True
False
Consider the following text from a Python interpreter.
>>> print(2 + 2) 4
What is the text "print" called?
a) a function
b) an operator
c) a prompt
d) a statement
e) a value
Match concepts with their definition!
To join two operands end-to-end.
concatenate
What the Python interpreter does to an expression to find its value.
evaluate
A combination of variables, operators, and values that represents a single result value.
expression
A reserved word that is used by the interpreter to parse programs.
keyword
A special symbol that represents a simple computation like addition, multiplication, or string concatenation.
operator
A unit of code that the Python interpreter can execute.
statement
variable
A name that refers to a value.
variable
statement
What is the output of the following Python 3 statements?
x=2
y=1
if x == y:
print (x, "and", y, "are equal")
else:
if x < y:
print (x, "is less than", y)
else:
print (x, "is greater than", y)
a) 1 and 2 are equal
b) 1 is less than 2
c) 1 is greater than 2
d) 2 is greater than 1
What is the output of the following Python statements?
percentage = ( 60 * 100) // 55
print (percentage)
a) percentage
b) 109
c) 109.0909090909091
d) 109.0
What output will the following Python statement produce?
>>> print ((1+1)**(5-2))
a) 16
b) 8
c) 4
d) 2
What output will the following Python statement produce?
>>> print (2*3-1)
a) 6
b) 5
c) 4
d) 3
Learning to debug can be frustrating, but it is a valuable skill that is useful for many activities beyond programming.
True
False
What output will the following Python statements produce?
>>> percentage = ( 60.0 * 100.0) / 55.0
>>> print (percentage)
a) percentage
b) 109
c) 109.0909090909091
d) 109.0
Python functions may or may not take arguments and may or may not return a result.
True
False
If you assign the result of calling a void function to a variable in Python, you get:
a) an empty string
b) the value -1
c) the value 0
d) the special value None
e) an exception
When defining a Python function that has no parameters, the parentheses that follow the function’s name are optional.
True
False
What is the output of the following Python statements?
pi = int(3.14159)
print (pi)
a) 3
b) 3.0
c) 3.14159
d) 0
One way to generalize a function is to replace a variable with a value.
True
False
Expressions evaluate to either true or false. What will the output of the following code be when the expression “Ni!” is evaluated?
if "Ni!":
print ('We are the Knights who say, "Ni!"')
else:
print ("Stop it! No more of this!")
a) Stop it!
b) We are the Knights who say, "Ni!"
c) Stop it! No more of this!"
d) No output will be produced
How many different values can a Boolean expression have?
a) 3
b) 2
c) infinate number
d) 1
e) not definate
Match the following terms and definitions
The order in which statements are executed during a program run.
flow of execution
The first part of a compound statement, begins with a keyword and ends with a colon ( : )
header
A statement that executes a function. It consists of the name of the function followed by a list of arguments enclosed in parentheses.
function call
A variable defined inside a function that can only be used inside its function.
local variable
A graphical representation of functions, their variables, and the values to which they refer.
stack diagram
What is the output of the following Python statements?
percentage = ( 60 * 100) // 55
print (percentage)
a) percentage
b) 109
c) 109.0909090909091
d) 109.0
What is the output of the following Python statements?
def recurse(a):
if (a == 0):
print(a)
else:
recurse(a)
recurse(0)
a) 0
b) 1
c) no output
d) RuntimeError: maximum recursion depth exceeded
Functions allow the programmer to encapsulate and generalize sections of code.
True
False
Occasionally, it is useful to have a body of an if statement that does nothing. In that case, you can use the following statement:
a) # do nothing
b) 0 = 0
c) Null
d) pass
e) Void
What is the output of the following statements?
pi = float(3.14159)
print (pi)
a) 3
b) 3.0
c) 3.14159
d) 0
Functions can only return Boolean expressions.
True
False
A function that returns an integer value grater than 0 is called a boolean function.
True
False
What will the output of this program be when it is executed?
def test_function( length, width, height):
print ("the area of the box is ", length*width*height)
return length*width*height
l = 12.5
w = 5
h = 2
test_function(l, w, h)
print ("The area of the box is ", length*width*height)
a) A NameError because a variable not defined
b) The area of the box is 125.0
c) The area of the box is 0
d) A SyntaxError due to illegal function call
What output will the following code produce?
def area(l, w):
temp = l * w;
return temp
l = 4.0
w = 3.25
x = area(l, w)
if ( x ):
print (x)
a) 13.0
b) 0
c) Expression does not evaluate to boolean true
d) 13
What will the output of this python program be?
def test_function( length, width, height):
print ("the area of the box is ",length*width*height)
return length*width*height
l = 12.5
w = 5
h = 2
test_function(l, w, h)
a) The area of the box is 125
b) The area of the box is 125.0
c) The area of the box is 120
d) 125.0
The int function can convert floating-point values to integers, and it performs rounding up/down as needed.
a) True
b) False
The following Python script will generate an error when executed. What is the cause of the error?
def function2(param):
print (param, param)
print (cat)
def function1(part1, part2):
cat = part1 + part2
function2(cat)
chant1 = "See You "
chant2 = "See Me "
function1(chant1, chant2)
a) The variable cat is local to function1 and cannot be used in function2
b) The variable param is used twice in function2 and this is illegal
c) Function2 does not have a return value defined
d) Function1 does not have a return value defined
What is the output of the Python code below?
n = int(10.)
print(isinstance(n, float), isinstance(n * 1.0, float))
a) False
b) True False
c) True True
d) False True
e) False False
What output will the following statements produce using Python IDLE in interactive mode?
>>> n = 2
>>> n += 5
>>> n
a) 7
b) 5
c) 2
d) an error message will occur
A development approach that that is intended to avoid a lot of debugging by only adding and testing small amounts of code at a time is called.
a) structured development
b) incremental development
c) unit testing
d) Systems development life cycle
Repeated execution of a set of programming statements is called repetitive execution.
a) True
b) False
Python allows while loops inside while loops and if statements within the body of if statements.
a) True
b) False
What output will the following Python program produce?
n = 10
while n != 1:
print (n,)
i. if n % 2 == 0: # n is even
n = n // 2
else: # n is odd
n = n * 3 + 1
a) 10 5 16 8 4 2
b) None an error will be displayed
c) 8 4 2
d) 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2
What output will the following Python commands produce?
n = 10000
count = 0
while n:
count = count + 1
n = n // 10
print (count)
a) 5
b) 0
c) 10000
d) 1000
What is the value of the following Python expression?
"Xanadu" > "Yellowstone"
a) True
b) False
An algorithm is a general process for solving a category of problems.
a) True
b) False
What is the output of the Python method call below?
"bib".find('b', 1, 2)
a) 0
b) 2
c) -1
d) Syntax error
e) 3
A Python string is a sequence of characters. You can access the string characters with the _______.
a) () operator
b) % operator
c) dot operator
d) // operator
e) [] operator
The following Python code illustrates what programming concept?
bruce = 5
print (bruce,)
bruce = 7
print (bruce)
a) Reassignment
b) Iteration
c) Logical operators
d) Conditionals
This Python code:
for fruit in ["banana", "apple", "quince"]:
print (fruit)
will produce this output:
banana apple quince
a) True
b) False
What is the output of the Python code below?
my_list = [3, 2, 1]
print(my_list.sort())
a) 0
b) {3, 2, 1}
c) None
d) syntax error
e) [3, 2, 1]
String objects are modified with string slices.
a) True
b) False
The Python expression 'Unit 6'[-1] has value '6'.
a) True
b) False
To create a new object that has the same value as an existing object is knows as creating an alias.
a) True
b) False
In Python, a list of characters is the same as a string.
a) True
b) False
What is the output of the following Python program?
fruit = "banana"
letter = fruit[1]
print (letter)
a) b
b) a
c) n
d) banana
What is the output of the following Python program?
mylist = [ [2,4,1], [1,2,3], [2,3,5] ]
total = 0
for sublist in mylist:
total += sum(sublist)
print(total)
a) 14
b) 23
c) 0
d) 13
Assume that d is a Python dictionary. What does the following Python code produce?
for k in d:
if d[k] == v:
return k
a) a histogram
b) an inverted dictionary
c) a list of tuples
d) a lookup
e) a reverse lookup
Python tuples are immutable.
a) True
b) False
What is the output of the Python code below?
my_tup = (3, 2, 1, 2)
print(tuple(sorted(my_tup)))
a) (1, 2, 3)
b) {1, 2, 2, 3}
c) (1, 2, 2, 3)
d) syntax error
e) [1, 2, 2, 3]
If you use a Python dictionary in a for statement, it traverses the _____ of the dictionary.
a) values and keys
b) indices
c) keys and values
d) values
e) keys
What will the contents of mylist be after the following Python code has been executed?
>>> mylist = [1, 4, 2, 3]
>>> mylist.append(5)
a) [1, 4, 2, 3, 5]
b) [5, 1, 4, 2, 3]
c) [null]
d) [1, 4, 2, 3]
Assume that d is a Python dictionary. What does the following Python code produce?
result = dict()
for key in d:
val = d[key]
if val not in result:
result[val] = [key]
else:
result[val].append(key)
a) a histogram
b) an inverted dictionary
c) a list of tuples
d) a lookup
e) a reverse lookup
Which of the following types are allowed for Python dictionary values?
a) dictionary
b) list
c) list of dictionaries
d) tuple
e) All of the above
Which of the following types are allowed for Python dictionary keys?
a) dictionary
b) list
c) list of dictionaries
d) tuple
e) All of the above
Traversal can only be accomplished with the "while" loop.
a) True
b) False
Given a Python dictionary d and a value v, it is efficient to find the corresponding key: d[k] = v.
a) True
b) False
Assume the following Python code has already executed.
import os
cwd = os.getcwd()
Which answer is most likely output from the following Python statement?
os.listdir(cwd)
a) ['Music', 'Pictures', 'Desktop', 'Library', 'Documents', 'Downloads']
b) False
c) True
d) /Users/me
e) /Users/me/Documents/file.txt
Exceptions allow the programmer to _________________.
a) override all runtime errors
b) write code to handle runtime errors
c) ignore syntax errors
d) load data from a file
Assume the following Python code has already executed.
import os
cwd = os.getcwd()
Which answer is most likely output from the following Python statement?
os.path.exists(cwd)
a) ['Music', 'Pictures', 'Desktop', 'Library', 'Documents', 'Downloads']
b) False
c) True
d) /Users/me
/Users/me/Documents/file.txt
What is the output from the following interactive Python statement?
>>> '%g' % (0,1)
a) '0'
b) '0.1'
c) TypeError: float argument required, not str
d) TypeError: not all arguments converted during string formatting
e) TypeError: not enough arguments for format string
What is the output of the Python code below?
print('%d + %d + %d = %d' % (1, 2, 3, 1+2+3))
a) 6
b) (1 + 2 + 3 = 6)
c) None
d) 1 + 2 + 3 = 6
e) “1 + 2 + 3 = 6”
Consider the following Python program.
fin = open('words.txt')
for line in fin:
word = line.strip()
print(word)
What is line?
a) A file object
b) A list of characters
c) A list of words
d) A string that may have a newline
e) A string with no newline
What is the output from the following interactive Python statement?
>>> '%g' % '0.1'
a) '0'
b) '0.1'
c) TypeError: float argument required, not str
d) TypeError: not all arguments converted during string formatting
e) TypeError: not enough arguments for format string
Consider the following Python program.
fin = open('words.txt')
for line in fin:
word = line.strip()
print(word)
What is word?
a) A file object
b) A list of characters
c) A list of words
d) A string that may have a newline
e) A string with no newline
What is the output from the following interactive Python statement?
>>> '%g %d' % (0.1)
a) '0'
b) '0.1'
c) TypeError: float argument required, not str
d) TypeError: not all arguments converted during string formatting
e) TypeError: not enough arguments for format string
What is the output from the following interactive Python statement?
>>> '%d' % (0.1)
a) '0'
b) '0.1'
c) TypeError: float argument required, not str
d) TypeError: not all arguments converted during string formatting
e) TypeError: not enough arguments for format string
Consider the following Python program.
fin = open('words.txt')
for line in fin:
word = line.strip()
print(word)
What does the program loop over?
a) Lines in a file
b) Lines in a list
c) Words in a dictionary
d) Words in a list
e) Words in a string
Assume the following Python code has already executed.
import os
cwd = os.getcwd()
Which answer is most likely output from the following Python statement?
os.path.abspath(cwd)
a) ['Music', 'Pictures', 'Desktop', 'Library', 'Documents', 'Downloads']
b) False
c) True
d) /Users/me
e) /Users/me/Documents/file.txt
Consider the following Python program.
fin = open('words.txt')
for line in fin:
word = line.strip()
print(word)
What is fin?
a) A file object
b) A list of characters
c) A list of words
d) A string that may have a newline
e) A string with no newline
Assume the following Python code has already executed.
import os
cwd = os.getcwd()
Which answer is most likely output from the following Python statement?
os.path.isfile(cwd)
a) ['Music', 'Pictures', 'Desktop', 'Library', 'Documents', 'Downloads']
b) False
c) True
d) /Users/me
e) /Users/me/Documents/file.txt
Which of the following Python statements runs without error?
a) open('three.txt').write(3)
b) open('three.txt','w').write(3)
c) open('three.txt','w').write(str(3))
d) All of the above
e) None of the above
Handling an exception with a try statement is called throwing an exception.
a) True
b) False
What is the output from the following interactive Python statement?
>>> '%g' % (0.1)
a) '0'
b) '0.1'
c) TypeError: float argument required, not str
d) TypeError: not all arguments converted during string formatting
e) TypeError: not enough arguments for format string
Assume the following Python code has already executed.
import os
cwd = os.getcwd()
Which answer is most likely output from the following Python statement?
os.path.isdir(cwd)
a) ['Music', 'Pictures', 'Desktop', 'Library', 'Documents', 'Downloads']
b) False
c) True
d) /Users/me
e) /Users/me/Documents/file.txt
What is the output of the following Python program?
try:
fin = open('answer.txt')
fin.write('Yes')
except:
print('No')
print('Maybe')
a) Yes
b) No
c) Maybe
d) YesMaybe
e) No Maybe
Assume the following Python code has already executed.
import os
cwd = os.getcwd()
Which answer is most likely output from the following Python statement?
os.path.join(cwd, 'Documents/file.txt')
a) ['Music', 'Pictures', 'Desktop', 'Library', 'Documents', 'Downloads']
b) False
c) True
d) /Users/me
e) /Users/me/Documents/file.txt
Any one of the languages that people have designed for specific purposes, such as representing mathematical ideas or computer programs; all programming languages are this kind of languages.
Formal Languages
Any one of the languages that people speak that evolved naturally.
Natural Languages
An error that does not occur until the program has started to execute but that prevents the program from continuing.
Runtime Error
An error in a program that makes it do something other than what the programmer intended.
Semantic Error
The meaning of a program.
Semantic
The structure of a program.
Syntax
An error in a program that makes it impossible to parse — and therefore impossible to interpret.
Syntax Error
One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language.
Token
