Font size
WorksheetsCMP131 Python Final Exam Study Guide Quiz
Total questions: 70
Worksheet time: 2hrs 51mins
A control structure that causes a statement or group of statements to repeat as many times as necessary is known as a(n) _____.
loop
operator
counter
sequence
A single execution of the statements in the body of the loop is called a(n) _____.
accumulation
iteration
range
control
A while loop tests its expression:
once, during initialization
before each iteration
after each iteration
inside the loop
How many times will ‘Hello World’ be printed in the following code?
0
1
10
an infinite number of times
An infinite loop repeats until the program is
replicated
debugged
interrupted
recompiled
Assume the variable upper_limit is assigned the value 50. How many times will the following for loop iterate?
49
50
0
51
What will the code display?
10
15
20
320
What sequence of numbers will the following code display?
100
99
...
0
99
98
...
1
100
99
...
1
99
98
...
0
The program above
prints all the odd numbers between 1 and 20
prints all even numbers between 1 and 20
print all the odd numbers between 1 and 21
prints all the even numbers between 1 and 21
In a Python program, what would be the ending value of total in the WHILE LOOP code?
0
5
2
20
A design technique that helps to reduce the duplication of code within a program and is a benefit of using functions is .
code reuse
divide and conquer
debugging
facilitation of teamwork
The first line of a function definition is known as the .
body
introduction
initialization
header
You a function to execute it.
define
call
import
export
A design technique that programmers use to break down an algorithm into functions is known as .
top-down design
code simplification
code refactoring
hierarchical subtasking
The keyword is ignored by the Python interpreter and can be used as a placeholder for code that will be written later.
placeholder
pass
pause
skip
A is a variable that is created inside a function.
global variable
local variable
hidden variable
none of the above; you cannot create a variable inside a function
A(n) is the part of a program in which a variable may be accessed.
declaration space
area of visibility
scope
mode
A(n) is a piece of data that is sent into a function.
argument
parameter
header
packet
A(n) is a special variable that receives a piece of data when a function is called.
argument
parameter
header
packet
This standard library function returns a random integer within a specified range of values.
random
randint
random_integer
uniform
What is the key function for working with files in Python?
close()
file()
open()
read()
How many parameters does the open() function take in Python?
One
Two
Three
Four
What are the two parameters of the open() function in Python?
file and type
path and class
filename and mode
directory and access
What does the "r" mode do when opening a file in Python?
Reads and writes to a file
Reads a file, creates a new file if it does not exist
Reads a file, error if the file does not exist
Reads a file, truncates the file first
What does the "a" mode do when opening a file in Python?
Appends to a file, error if the file does not exist
Appends to a file, creates the file if it does not exist
Appends to a file, truncates the file first
Appends to a file, reads from the file first
What does the "w" mode do when opening a file in Python?
Writes to a file, error if the file does not exist
Writes to a file, creates the file if it does not exist
Writes to a file, reads from the file first
Writes to a file, appends to the file if it exists
What type of error is raised when there is a wrong value in a specified data type?
TypeError
ValueError
RuntimeError
AttributeError
When does a ZeroDivisionError occur?
When the first operator in a division is zero
When any operator in a division is zero
When the second operator in a division is zero
When division is performed on non-numeric types
What does "else" do in a "try" block in Python?
Run code after the block if nothing triggers
Execute code if no exception happens
Run code after try block
Create 3 errors
What is the purpose of a try-except block in Python?
To execute code that should run only if an error occurs
To handle exceptions and errors gracefully
To loop through a block of code multiple times
To define a function
Which of these is the best description of a list in Python?
A list is a collection of data that has an order and can be changed
A list is a lot of variables
A list is used for shopping
A list is a collection of data that cannot hold duplicated data and cannot be changed
Which of these is the correct code for creating a list of names?
nameList = John, Harry, Jesse, John, Harry, Harry
nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList = [John, Harry, Jesse, John, Harry, Harry]
List items have an index number. In the following list, which item has the index number of 3?
["John", "Harry", "Jesse", "John", "Harry", "Harry"]
"John"
"Harry"
"Jesse"
Which of these pieces of code would return the name "Harry" from the following list?
nameList = ["Janet", "Harry", "Samantha", "John", "Vanessa", "Harry"]
nameList()
nameList[1]
NameList(4)
nameList["4"]
What would this code show on the screen?
nameList = ['John', 'Samantha', 'Jessica', 'Steven', 'Jacqueline', 'Peter']
print(len(nameList))
7
6
5
4
An error
What would this code return on the shell?
nameList = ['John', 'Janet', 'Brian', 'Vanessa', 'Felipe', 'Amna']
if "Raheem" in nameList:
print("Woo Hoo, you scored!").
Nothing
An error
"Woo hoo, you scored!"
"Raheem"
For tuples and list which is correct?
List and tuples both are mutable.
List is mutable whereas tuples are immutable.
List and tuples both are immutable.
List is immutable whereas tuples are mutable
Given a = "Hello world" what does list(a) do?
creates a list separated by word (white spaces)
creates a list separated by character
nothing
creates a blank list named a
Given a = "Hello world" what does a.split() do?
creates a list separated by character
nothing
creates a list separated by word (white spaces)
splits the string into two strings
What does the following code do?
originalList = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60]
newList = originalList[5:10].
Save 30, 35, 40, 45, 50 into newList
Save 30, 35, 40, 45, 50, 55 into newList
Save 5, 6, 7, 8, 9 into newList
Save 5, 6, 7, 8, 9, 10 into newList
WHICH TYPE OF DECISION STRUCTURE IS THIS?
switch
elif (Nested False Path If)
if else (binary choice)
Nested True Path If
WHAT DECISION STRUCTURE WORKS BEST IF YOU HAVE MORE THAN TWO POSSIBLE ANSWERS TO A QUESTION (MULTIPLE CHOICE)?
Nested True Path if statements
A regular if statement (Binary Bypass)
A switch statement
elif statements (Nested False Path if statements)
WHAT IS MISSING FROM THIS DECISION STRUCTURE THAT WILL CAUSE CHOCOLATE AND STRAWBERRY TO OVERWRITE THE PREVIOUS VALUE OF CONE EVERY TIME, AND ALSO CRASH WITH AN ERROR THAT THE ELSE ISNT ATTACHED TO ANY IF?
Proper Indentation
A proper elif clause and relational equality comparison for 'C' and 'S'
Proper Indentation
A proper elif clause and relational equality comparison for 'C' and 'S', as well as proper indentation
and
&&
||
&
WHICH OF THE FOLLOWING STATEMENTS WOULD VALIDLY COMPARE TWO STRING VARIABLES FOR ONLY AN EXACT EQUALITY MATCH?
if myName = yourName:
if myName != yourName:
if myName == yourName:
if myName equals yourName:
if myName.upper() == yourName.upper():
if (myName.equalsIgnoreCase(yourName))
if (myName.compareTo(yourName))
None of the above
WHAT DOES THE FOLLOWING DECISION STRUCTURE DO?
Use a binary choice structure
Assign 10.60 to grandTotal if itemTaxable is true
Assign 10.00 to grandTotal if itemTaxable is false
All of the above
What keyword can be used in a nested elif structure to act as a default path if none of the condition tests passes true, so that you can present an error message for bad data input, for example?
elif default:
elif:
else:
None of these
A group of statements that exist within a program for the purpose of performing a specific task is a(n) .
block
parameter
function
expression
A variable that is visible to every function in a program file is a .
local variable
universal variable
program-wide variable
global variable
When possible, you should avoid using variables in a program
local
global
reference
parameter
This is a prewritten function that is built into a programming language.
standard function
library function
custom function
cafeteria function
Which of the following statements correctly defines a function named haunted that takes a parameter named apple?
haunted(apple)
haunted
def haunted(apple):
apple
Which of the following statements will successfully send the value "EEK" back from a function to the calling code?
"EEK"
return "EEK"
back "EEK"
#print("EEK")
What are the reasons for using functions? (Select all that apply, multiple answers)
To build code that will be reused
To simplify code
To add comments
Help us to organize longer programs
To catch an exception during program execution, use the try except clause.
True
False
What is the Python keyword used to create a function?
fun
cre
begin
def
A variable received in the definition of a function is called a(an)
argument
parameter
boolean
library
How many parameters are optional for the function defined below?
def heatcheck(a, m, c=0, q=1)
0
1
2
3
4
DOES THE FOLLOWING CODE RETURN A STRING OR NUMBER?
def f( ) :
return “3”
String
Number
DOES THE FOLLOWING CODE RETURN A STRING OR NUMBER?
def f( ) :
return 14
String
Number
Which of the following is a valid way to create a list in Python?
my_list = [1, 2, 3]
my_list = (1, 2, 3)
my_list = {1, 2, 3}
my_list = 1, 2, 3
The list needs one more name added to the end - "Felipe". Which piece of code below would do this?
nameList = ["John", "Jessica", "Randy", "Kim", "Steven", "Vanessa"]
nameList.append(Felipe)
append(nameList,"Felipe")
nameList.append["Felipe",7]
nameList.append("Felipe")
names=["Joe", "Vanessa", "Sam", "Rosemary"]
What command will add "Jim" between "Sam" and "Rosemary"?
names.pop(3,"Jim")
names.append(3,"Jim")
names.insert(3,"Jim")
names.remove(3,"Jim")
values = [5, 3, 3, 7, 10,3]
values.remove(3)
What is the output?
[3,3,3]
[5,3,3,10,3]
[5,7,10]
[5,3,7,10,3]
values = [5, 3, 7, 10,20]
values.pop(3)
What is the output?
[5,3,7,10,20,3]
[5,3,7,20]
[5,7,10,20]
[10,20]
Which operator is used to concatenate two lists in Python?
+
*
|
&
What is the term for a list that is an element of another list?
Sublist
Nested list
Inner list
Child list
