wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

CMP131 Python Final Exam Study Guide Quiz

Total questions: 70

Worksheet time: 2hrs 51mins

Name
Class
Date
1.

A control structure that causes a statement or group of statements to repeat as many times as necessary is known as a(n) _____.

a)

loop

b)

operator

c)

counter

d)

sequence

2.

A single execution of the statements in the body of the loop is called a(n) _____.

a)

accumulation

b)

iteration

c)

range

d)

control

3.

A while loop tests its expression:

a)

once, during initialization

b)

before each iteration

c)

after each iteration

d)

inside the loop

4.

How many times will ‘Hello World’ be printed in the following code?

a)

0

b)

1

c)

10

d)

an infinite number of times

5.

An infinite loop repeats until the program is

a)

replicated

b)

debugged

c)

interrupted

d)

recompiled

6.

Assume the variable upper_limit is assigned the value 50. How many times will the following for loop iterate?

a)

49

b)

50

c)

0

d)

51

7.

What will the code display?

a)

10

b)

15

c)

20

d)

320

8.

What sequence of numbers will the following code display?

a)

100

99

...

0

b)

99

98

...

1

c)

100

99

...

1

d)

99

98

...

0

9.

The program above

a)

prints all the odd numbers between 1 and 20

b)

prints all even numbers between 1 and 20

c)

print all the odd numbers between 1 and 21

d)

prints all the even numbers between 1 and 21

10.

In a Python program, what would be the ending value of total in the WHILE LOOP code?

a)

0

b)

5

c)

2

d)

20

11.

A design technique that helps to reduce the duplication of code within a program and is a benefit of using functions is           .

a)

code reuse

b)

divide and conquer

c)

debugging

d)

facilitation of teamwork

12.

The first line of a function definition is known as the           .

a)

body

b)

introduction

c)

initialization

d)

header

13.

You            a function to execute it.

a)

define

b)

call

c)

import

d)

export

14.

A design technique that programmers use to break down an algorithm into functions is known as           .

a)

top-down design

b)

code simplification

c)

code refactoring

d)

hierarchical subtasking

15.

The                 keyword is ignored by the Python interpreter and can be used as a placeholder for code that will be written later.

a)

placeholder

b)

pass

c)

pause

d)

skip

16.

A            is a variable that is created inside a function.

a)

global variable

b)

local variable

c)

hidden variable

d)

none of the above; you cannot create a variable inside a function

17.

A(n)            is the part of a program in which a variable may be accessed.

a)

declaration space

b)

area of visibility

c)

scope

d)

mode

18.

A(n)            is a piece of data that is sent into a function.

a)

argument

b)

parameter

c)

header

d)

packet

19.

A(n)            is a special variable that receives a piece of data when a function is called.

a)

argument

b)

parameter

c)

header

d)

packet

20.

This standard library function returns a random integer within a specified range of values.

a)

random

b)

randint

c)

random_integer

d)

uniform

21.

What is the key function for working with files in Python?

a)

close()

b)

file()

c)

open()

d)

read()

22.

How many parameters does the open() function take in Python?

a)

One

b)

Two

c)

Three

d)

Four

23.

What are the two parameters of the open() function in Python?

a)

file and type

b)

path and class

c)

filename and mode

d)

directory and access

24.

What does the "r" mode do when opening a file in Python?

a)

Reads and writes to a file

b)

Reads a file, creates a new file if it does not exist

c)

Reads a file, error if the file does not exist

d)

Reads a file, truncates the file first

25.

What does the "a" mode do when opening a file in Python?

a)

Appends to a file, error if the file does not exist

b)

Appends to a file, creates the file if it does not exist

c)

Appends to a file, truncates the file first

d)

Appends to a file, reads from the file first

26.

What does the "w" mode do when opening a file in Python?

a)

Writes to a file, error if the file does not exist

b)

Writes to a file, creates the file if it does not exist

c)

Writes to a file, reads from the file first

d)

Writes to a file, appends to the file if it exists

27.

What type of error is raised when there is a wrong value in a specified data type?

a)

TypeError

b)

ValueError

c)

RuntimeError

d)

AttributeError

28.

When does a ZeroDivisionError occur?

a)

When the first operator in a division is zero

b)

When any operator in a division is zero

c)

When the second operator in a division is zero

d)

When division is performed on non-numeric types

29.

What does "else" do in a "try" block in Python?

a)

Run code after the block if nothing triggers

b)

Execute code if no exception happens

c)

Run code after try block

d)

Create 3 errors

30.

What is the purpose of a try-except block in Python?

a)

To execute code that should run only if an error occurs

b)

To handle exceptions and errors gracefully

c)

To loop through a block of code multiple times

d)

To define a function

31.

Which of these is the best description of a list in Python?

a)

A list is a collection of data that has an order and can be changed

b)

A list is a lot of variables

c)

A list is used for shopping

d)

A list is a collection of data that cannot hold duplicated data and cannot be changed

32.

Which of these is the correct code for creating a list of names?

a)

nameList = John, Harry, Jesse, John, Harry, Harry

b)

nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")

c)

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

d)

nameList = [John, Harry, Jesse, John, Harry, Harry]

33.

List items have an index number. In the following list, which item has the index number of 3?

["John", "Harry", "Jesse", "John", "Harry", "Harry"]

a)

"John"

b)

"Harry"

c)

"Jesse"

34.

Which of these pieces of code would return the name "Harry" from the following list?

nameList = ["Janet", "Harry", "Samantha", "John", "Vanessa", "Harry"]

a)

nameList()

b)

nameList[1]

c)

NameList(4)

d)

nameList["4"]

35.

What would this code show on the screen?

nameList = ['John', 'Samantha', 'Jessica', 'Steven', 'Jacqueline', 'Peter']

print(len(nameList))

a)

7

b)

6

c)

5

d)

4

e)

An error

36.

What would this code return on the shell?

nameList = ['John', 'Janet', 'Brian', 'Vanessa', 'Felipe', 'Amna']

if "Raheem" in nameList:

print("Woo Hoo, you scored!").

a)

Nothing

b)

An error

c)

"Woo hoo, you scored!"

d)

"Raheem"

37.

For tuples and list which is correct?

a)

List and tuples both are mutable.

b)

List is mutable whereas tuples are immutable.

c)

List and tuples both are immutable.

d)

List is immutable whereas tuples are mutable

38.

Given a = "Hello world" what does list(a) do?

a)

creates a list separated by word (white spaces)

b)

creates a list separated by character

c)

nothing

d)

creates a blank list named a

39.

Given a = "Hello world" what does a.split() do?

a)

creates a list separated by character

b)

nothing

c)

creates a list separated by word (white spaces)

d)

splits the string into two strings

40.

What does the following code do?

originalList = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60]

newList = originalList[5:10].

a)

Save 30, 35, 40, 45, 50 into newList

b)

Save 30, 35, 40, 45, 50, 55 into newList

c)

Save 5, 6, 7, 8, 9 into newList

d)

Save 5, 6, 7, 8, 9, 10 into newList

41.
USE THIS TYPE OF DECISION STATEMENT IF YOU WANT TO EXECUTE ONE SET OF STATEMENTS ON THE TRUE PATH AND AN ALTERNATE SET OF STATEMENTS ON THE FALSE PATH.
a)
if statement (binary bypass)
b)
if else statement (binary choice)
c)
switch statement (multiple choice)
d)
nested if statement
42.

WHICH TYPE OF DECISION STRUCTURE IS THIS?

a)

switch

b)

elif (Nested False Path If)

c)

if else (binary choice)

d)

Nested True Path If

43.

WHAT DECISION STRUCTURE WORKS BEST IF YOU HAVE MORE THAN TWO POSSIBLE ANSWERS TO A QUESTION (MULTIPLE CHOICE)?

a)

Nested True Path if statements

b)

A regular if statement (Binary Bypass)

c)

A switch statement

d)

elif statements (Nested False Path if statements)

44.

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?

a)

Proper Indentation

b)

A proper elif clause and relational equality comparison for 'C' and 'S'

c)

Proper Indentation

d)

A proper elif clause and relational equality comparison for 'C' and 'S', as well as proper indentation

45.
WHICH OF THE FOLLOWING IS THE RELATIONAL OPERATOR FOR NOT EQUAL TO?
a)
>=
b)
<>
c)
\=
d)
!=
46.
WHICH OF THE FOLLOWING IS THE LOGICAL OPERATOR FOR AN AND RELATIONSHIP?
a)

and

b)

&&

c)

||

d)

&

47.

WHICH OF THE FOLLOWING STATEMENTS WOULD VALIDLY COMPARE TWO STRING VARIABLES FOR ONLY AN EXACT EQUALITY MATCH?

a)

if myName = yourName:

b)

if myName != yourName:

c)

if myName == yourName:

d)

if myName equals yourName:

48.
WHICH METHOD WOULD ALLOW YOU TO COMPARE TWO STRINGS FOR AN EXACT EQUALITY MATCH REGARDLESS OF WHETHER OR NOT ONE OF THE STRINGS IS ALL CAPITALIZED AND THE OTHER IS ALL LOWERCASE?
a)

if myName.upper() == yourName.upper():

b)

if (myName.equalsIgnoreCase(yourName))

c)

if (myName.compareTo(yourName))

d)

None of the above

49.

WHAT DOES THE FOLLOWING DECISION STRUCTURE DO?

a)

Use a binary choice structure

b)

Assign 10.60 to grandTotal if itemTaxable is true

c)

Assign 10.00 to grandTotal if itemTaxable is false

d)

All of the above

50.

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?

a)

elif default:

b)

elif:

c)

else:

d)

None of these

51.

A group of statements that exist within a program for the purpose of performing a specific task is a(n)           .

a)

block

b)

parameter

c)

function

d)

expression

52.

A variable that is visible to every function in a program file is a           .

a)

local variable

b)

universal variable

c)

program-wide variable

d)

global variable

53.

When possible, you should avoid using            variables in a program

a)

local

b)

global

c)

reference

d)

parameter

54.

This is a prewritten function that is built into a programming language.

a)

standard function

b)

library function

c)

custom function

d)

cafeteria function

55.

Which of the following statements correctly defines a function named haunted that takes a parameter named apple?

a)

haunted(apple)

b)

haunted

c)

def haunted(apple):

d)

apple

56.

Which of the following statements will successfully send the value "EEK" back from a function to the calling code?

a)

"EEK"

b)

return "EEK"

c)

back "EEK"

d)

#print("EEK")

57.

What are the reasons for using functions? (Select all that apply, multiple answers)

a)

To build code that will be reused

b)

To simplify code

c)

To add comments

d)

Help us to organize longer programs

58.

To catch an exception during program execution, use the try except clause.

a)

True

b)

False

59.

What is the Python keyword used to create a function?

a)

fun

b)

cre

c)

begin

d)

def

60.

A variable received in the definition of a function is called a(an)

a)

argument

b)

parameter

c)

boolean

d)

library

61.

How many parameters are optional for the function defined below?

def heatcheck(a, m, c=0, q=1)

a)

0

b)

1

c)

2

d)

3

e)

4

62.

DOES THE FOLLOWING CODE RETURN A STRING OR NUMBER?


def f( ) :

return “3”

a)

String

b)

Number

63.

DOES THE FOLLOWING CODE RETURN A STRING OR NUMBER?


def f( ) :

return 14

a)

String

b)

Number

64.

Which of the following is a valid way to create a list in Python?

a)

my_list = [1, 2, 3]

b)

my_list = (1, 2, 3)

c)

my_list = {1, 2, 3}

d)

my_list = 1, 2, 3

65.

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

a)

nameList.append(Felipe)

b)

append(nameList,"Felipe")

c)

nameList.append["Felipe",7]

d)

nameList.append("Felipe")

66.

names=["Joe", "Vanessa", "Sam", "Rosemary"]

What command will add "Jim" between "Sam" and "Rosemary"?

a)

names.pop(3,"Jim")

b)

names.append(3,"Jim")

c)

names.insert(3,"Jim")

d)

names.remove(3,"Jim")

67.

values = [5, 3, 3, 7, 10,3]

values.remove(3)

What is the output?

a)

[3,3,3]

b)

[5,3,3,10,3]

c)

[5,7,10]

d)

[5,3,7,10,3]

68.

values = [5, 3, 7, 10,20]

values.pop(3)

What is the output?


a)

[5,3,7,10,20,3]

b)

[5,3,7,20]

c)

[5,7,10,20]

d)

[10,20]

69.

Which operator is used to concatenate two lists in Python?

a)

+

b)

*

c)

|

d)

&

70.

What is the term for a list that is an element of another list?

a)

Sublist

b)

Nested list

c)

Inner list

d)

Child list