wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

CS1101 Python Final Exam practice

Total questions: 103

Worksheet time: 59mins

Name
Class
Date
1.

Consider the following text from a Python interpreter.

>>> print(2 + 2)

4

What is the text ">>>" called?

a)

a) a function

b)

b) an operator

c)

c) a prompt

d)

d) a statement

e)

e) a value

2.

Consider the following text from a Python interpreter.

>>> print(2 + 2)

4

What is the text "print" called?

a)

a) a function

b)

b) an operator

c)

c) a prompt

d)

d) a statement

e)

e) a value

3.

What is Python’s response to the command: type(123)

a)

a) <class 'float'>

b)

b) <class 'bool'>

c)

c) SyntaxError: invalid syntax

d)

d) <class 'int'>

e)

e) <class 'str'>

4.

Match the following- part 1

a)

An error in a program.

1.

bug

b)

The process of finding and removing any of the three kinds of programming errors.

2.

debugging

c)

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.

3.

formal language

d)

A programming language like Python that is designed to be easy for humans to read and write.

4.

high-level language

e)

A program that reads another program and executes it.

5.

interpreter

5.

Match the following - Part 2

a)

A programming language that is designed to be easy for a computer to execute; also called machine language or assembly language.

1.

low-level language

b)

Any one of the languages that people speak that evolved naturally.

2.

natural-language

c)

To examine a program and analyze the syntactic structure.

3.

parse

d)

A property of a program that can run on more than one kind of computer.

4.

portability

e)

An instruction that causes the Python interpreter to display a value on the screen.

5.

print statement

6.

Match the following - Part 3

a)

The process of formulating a problem, finding a solution, and expressing the solution.

1.

problem solving

b)

A sequence of instructions that specifies to a computer actions and computations to be performed.

2.

program

c)

A program stored in a file (usually one that will be interpreted).

3.

script

d)

The structure of a program.

4.

syntax

e)

One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language.

5.

token

7.

Which one of the following Python expressions has the value 64?

a)

a) 8 ^ 2

b)

b) 8 ** 2

c)

c) 8 +- 2

d)

d) 8 += 2

e)

e) 8 -+ 2

8.

Consider the following text from a Python interpreter.

>>> print(2 + 2)

4

What is the text "4" called?

a)

a) a function

b)

b) an operator

c)

c) a prompt

d)

d) a statement

e)

e) a value

9.

Programming languages are ______ languages that have been designed to express computations.

a)

a) informal

b)

b) mathematical

c)

c) formal

d)

d) natural

e)

e) logical

10.

What does the Python interpreter output for the following input?

>>> 1,234.567,890

a)

a) 1234

b)

b) 1234.6

c)

d) 1234.56789

d)

d) (1, 234.567, 890)

e)

e) SyntaxError: invalid token

11.

Which one of the following Python expressions computes the total number of seconds in 21 minutes and 21 seconds?

a)

a) 21 + 21

b)

b) 21 + 60

c)

c) "21 minutes" + "21 seconds"

d)

d) 21 * 60 + 21

e)

e) seconds = 21 + "21 minutes"

12.

What is Python’s response to the command: type(0.123)

a)

a) <class 'float'>

b)

b) <class 'bool'>

c)

c) SyntaxError: invalid syntax

d)

d) <class 'int'>

e)

e) <class 'str'>

13.

Consider the following text from a Python interpreter.

>>> print(2 + 2) 4

What is the text "+" called?

a)

a) a function

b)

b) an operator

c)

c) a prompt

d)

d) a statement

e)

e) a value

14.

In Python, the expression "a**(b**c)" is the same as "(a**b)**c".

a)

True

b)

False

15.

What output will the following Python statements produce?

>>> n = 17

>>> print (n)

a)

a) 0

b)

b) 17.0

c)

c) n

d)

d) 17

16.

What is the output of the following Python statements?

def recurse(a):    

if (a == 1):        

print(a)    

else:        

recurse(a)

recurse(1)

a)

a) 0

b)

b) 1

c)

c) no output

d)

d) RuntimeError: maximum recursion depth exceeded

17.

A program is a sequence of instructions that specifies how to perform a computation.

a)

True

b)

False

18.

Which one of the following Python expressions generates a syntax error?

a)

a) 2 ^ 2

b)

b) 2 ** 2

c)

c) 2 +- 2

d)

d) 2 += 2

e)

e) 2 -+ 2

19.

Consider the following text from a Python interpreter.

>>> print(2 + 2) 4

What is the text "4" called?

a)

a) a function

b)

b) operator

c)

c) prompt

d)

d) statement

e)

e) value

20.

Which of the following is an invalid Python assignment statement?

a)

a) a = b = 123

b)

b) ‘3’ = 3

c)

c) x = int(“123”)

d)

d) y = None

e)

e) z = “hi” * 10

21.

The % or modulus operator returns the remainder from dividing two numbers.

a)

True

b)

False

22.

Consider the following text from a Python interpreter.

>>> print(2 + 2) 4

What is the text "print" called?

a)

a) a function

b)

b) an operator

c)

c) a prompt

d)

d) a statement

e)

e) a value

23.

Match concepts with their definition!

a)

To join two operands end-to-end.

1.

concatenate

b)

What the Python interpreter does to an expression to find its value.

2.

evaluate

c)

A combination of variables, operators, and values that represents a single result value.

3.

expression

d)

A reserved word that is used by the interpreter to parse programs.

4.

keyword

e)

A special symbol that represents a simple computation like addition, multiplication, or string concatenation.

5.

operator

24.

A unit of code that the Python interpreter can execute.

a)

statement

b)

variable

25.

A name that refers to a value.

a)

variable

b)

statement

26.

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)

a) 1 and 2 are equal

b)

b) 1 is less than 2

c)

c) 1 is greater than 2

d)

d) 2 is greater than 1

27.

What is the output of the following Python statements?

percentage = ( 60 * 100) // 55

print (percentage)

a)

a) percentage

b)

b) 109

c)

c) 109.0909090909091

d)

d) 109.0

28.

What output will the following Python statement produce?

>>> print ((1+1)**(5-2))

a)

a) 16

b)

b) 8

c)

c) 4

d)

d) 2

29.

What output will the following Python statement produce?

>>> print (2*3-1)

a)

a) 6

b)

b) 5

c)

c) 4

d)

d) 3

30.

Learning to debug can be frustrating, but it is a valuable skill that is useful for many activities beyond programming.

a)

True

b)

False

31.

What output will the following Python statements produce?

>>> percentage = ( 60.0 * 100.0) / 55.0

>>> print (percentage)

a)

a) percentage

b)

b) 109

c)

c) 109.0909090909091

d)

d) 109.0

32.

Python functions may or may not take arguments and may or may not return a result.

a)

True

b)

False

33.

If you assign the result of calling a void function to a variable in Python, you get:

a)

a) an empty string

b)

b) the value -1

c)

c) the value 0

d)

d) the special value None

e)

e) an exception

34.

When defining a Python function that has no parameters, the parentheses that follow the function’s name are optional.

a)

True

b)

False

35.

What is the output of the following Python statements?

pi = int(3.14159)

print (pi)

a)

a) 3

b)

b) 3.0

c)

c) 3.14159

d)

d) 0

36.

One way to generalize a function is to replace a variable with a value.

a)

True

b)

False

37.

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)

a) Stop it!

b)

b) We are the Knights who say, "Ni!"

c)

c) Stop it! No more of this!"

d)

d) No output will be produced

38.

How many different values can a Boolean expression have?

a)

a) 3

b)

b) 2

c)

c) infinate number

d)

d) 1

e)

e) not definate

39.

Match the following terms and definitions

a)

The order in which statements are executed during a program run.

1.

flow of execution

b)

The first part of a compound statement, begins with a keyword and ends with a colon ( : )

2.

header

c)

A statement that executes a function. It consists of the name of the function followed by a list of arguments enclosed in parentheses.

3.

function call

d)

A variable defined inside a function that can only be used inside its function.

4.

local variable

e)

A graphical representation of functions, their variables, and the values to which they refer.

5.

stack diagram

40.

What is the output of the following Python statements?

percentage = ( 60 * 100) // 55

print (percentage)

a)

a) percentage

b)

b) 109

c)

c) 109.0909090909091

d)

d) 109.0

41.

What is the output of the following Python statements?

def recurse(a):    

if (a == 0):        

print(a)    

else:        

recurse(a)

recurse(0)

a)

a) 0

b)

b) 1

c)

c) no output

d)

d) RuntimeError: maximum recursion depth exceeded

42.

Functions allow the programmer to encapsulate and generalize sections of code.

a)

True

b)

False

43.

 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)

a) # do nothing

b)

b) 0 = 0

c)

c) Null

d)

d) pass

e)

e) Void

44.

What is the output of the following statements?

pi = float(3.14159)

print (pi)

a)

a) 3

b)

b) 3.0

c)

c) 3.14159

d)

d) 0

45.

Functions can only return Boolean expressions.

a)

True

b)

False

46.

A function that returns an integer value grater than 0 is called a boolean function.

a)

True

b)

False

47.

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) A NameError because a variable not defined

b)

b) The area of the box is 125.0

c)

c) The area of the box is 0

d)

d) A SyntaxError due to illegal function call

48.

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)

a) 13.0

b)

b) 0

c)

c) Expression does not evaluate to boolean true

d)

d) 13

49.

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)

a) The area of the box is 125

b)

b) The area of the box is 125.0

c)

c) The area of the box is 120

d)

d) 125.0

50.

The int function can convert floating-point values to integers, and it performs rounding up/down as needed.

a)

a) True

b)

b) False

51.

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)

a) The variable cat is local to function1 and cannot be used in function2

b)

b) The variable param is used twice in function2 and this is illegal

c)

c) Function2 does not have a return value defined

d)

d) Function1 does not have a return value defined

52.

What is the output of the Python code below?

n = int(10.)

print(isinstance(n, float), isinstance(n * 1.0, float))

a)

a) False

b)

b) True False

c)

c) True True

d)

d) False True

e)

e) False False

53.

What output will the following statements produce using Python IDLE in interactive mode?

>>>  n = 2

>>>  n += 5

>>>  n

a)

a) 7

b)

b) 5

c)

c) 2

d)

d) an error message will occur

54.

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)

a) structured development

b)

b) incremental development

c)

c) unit testing

d)

d) Systems development life cycle

55.

Repeated execution of a set of programming statements is called repetitive execution.

a)

a) True

b)

b) False

56.

Python allows while loops inside while loops and if statements within the body of if statements.

a)

a) True

b)

b) False

57.

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)

a) 10 5 16 8 4 2

b)

b) None an error will be displayed

c)

c) 8 4 2

d)

d) 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2

58.

What output will the following Python commands produce?

n = 10000

count = 0

while n:   

count = count + 1   

n = n // 10

print (count)

a)

a) 5

b)

b) 0

c)

c) 10000

d)

d) 1000

59.

What is the value of the following Python expression?

"Xanadu" > "Yellowstone"

a)

a) True

b)

b) False

60.

An algorithm is a general process for solving a category of problems.

a)

a) True

b)

b) False

61.

What is the output of the Python method call below?

"bib".find('b', 1, 2)

a)

a) 0

b)

b) 2

c)

c) -1

d)

d) Syntax error

e)

e) 3

62.

A Python string is a sequence of characters. You can access the string characters with the _______.

a)

a) () operator

b)

b) % operator

c)

c) dot operator

d)

d) // operator

e)

e) [] operator

63.

The following Python code illustrates what programming concept?

bruce = 5

print (bruce,)

bruce = 7

print (bruce)

a)

a) Reassignment

b)

b) Iteration

c)

c) Logical operators

d)

d) Conditionals

64.

This Python code:

for fruit in ["banana", "apple", "quince"]:   

print (fruit)

will produce this output:

banana apple quince

a)

a) True

b)

b) False

65.

What is the output of the Python code below?

my_list = [3, 2, 1]

print(my_list.sort())

a)

a) 0

b)

b) {3, 2, 1}

c)

c) None

d)

d) syntax error

e)

e) [3, 2, 1]

66.

String objects are modified with string slices.

a)

a) True

b)

b) False

67.

The Python expression 'Unit 6'[-1] has value '6'.

a)

a) True

b)

b) False

68.

To create a new object that has the same value as an existing object is knows as creating an alias.

a)

a) True

b)

b) False

69.

 In Python, a list of characters is the same as a string.

a)

a) True

b)

b) False

70.

What is the output of the following Python program? 

fruit = "banana"

letter = fruit[1]

print (letter)

a)

a) b

b)

b) a

c)

c) n

d)

d) banana

71.

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)

a) 14

b)

b) 23

c)

c) 0

d)

d) 13

72.

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) a histogram

b)

b) an inverted dictionary

c)

c) a list of tuples

d)

d) a lookup

e)

e) a reverse lookup

73.

Python tuples are immutable.

a)

a) True

b)

b) False

74.

What is the output of the Python code below?

my_tup = (3, 2, 1, 2)

print(tuple(sorted(my_tup)))

a)

a) (1, 2, 3)

b)

b) {1, 2, 2, 3}

c)

c) (1, 2, 2, 3)

d)

d) syntax error

e)

e) [1, 2, 2, 3]

75.

 If you use a Python dictionary in a for statement, it traverses the _____ of the dictionary.

a)

a) values and keys

b)

b) indices

c)

c) keys and values

d)

d) values

e)

e) keys

76.

What will the contents of mylist be after the following Python code has been executed?

>>> mylist = [1, 4, 2, 3]

>>> mylist.append(5)

a)

a) [1, 4, 2, 3, 5]

b)

b) [5, 1, 4, 2, 3]

c)

c) [null]

d)

d) [1, 4, 2, 3]

77.

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) a histogram

b)

b) an inverted dictionary

c)

c) a list of tuples

d)

d) a lookup

e)

e) a reverse lookup

78.

Which of the following types are allowed for Python dictionary values?

a)

a) dictionary

b)

b) list

c)

c) list of dictionaries

d)

d) tuple

e)

e) All of the above

79.

Which of the following types are allowed for Python dictionary keys?

a)

a) dictionary

b)

b) list

c)

c) list of dictionaries

d)

d) tuple

e)

e) All of the above

80.

Traversal can only be accomplished with the "while" loop.

a)

a) True

b)

b) False

81.

Given a Python dictionary d and a value v, it is efficient to find the corresponding key: d[k] = v.

a)

a) True

b)

b) False

82.

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)

a) ['Music', 'Pictures', 'Desktop', 'Library', 'Documents', 'Downloads']

b)

b) False

c)

c) True

d)

d) /Users/me

e)

e) /Users/me/Documents/file.txt

83.

Exceptions allow the programmer to _________________.

a)

a) override all runtime errors

b)

b) write code to handle runtime errors

c)

c) ignore syntax errors

d)

d) load data from a file

84.

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)

a) ['Music', 'Pictures', 'Desktop', 'Library', 'Documents', 'Downloads']

b)

b) False

c)

c) True

d)

d) /Users/me

e)

/Users/me/Documents/file.txt

85.

What is the output from the following interactive Python statement?

>>> '%g' % (0,1)

a)

a) '0'

b)

b) '0.1'

c)

c) TypeError: float argument required, not str

d)

d) TypeError: not all arguments converted during string formatting

e)

e) TypeError: not enough arguments for format string

86.

What is the output of the Python code below?

 print('%d + %d + %d = %d' % (1, 2, 3, 1+2+3))

a)

a) 6

b)

b) (1 + 2 + 3 = 6)

c)

c) None

d)

d) 1 + 2 + 3 = 6

e)

e) “1 + 2 + 3 = 6”

87.

Consider the following Python program.

fin = open('words.txt')

for line in fin:    

word = line.strip()    

print(word)

What is line?

a)

a) A file object

b)

b) A list of characters

c)

c) A list of words

d)

d) A string that may have a newline

e)

e) A string with no newline

88.

What is the output from the following interactive Python statement?

>>> '%g' % '0.1'

a)

a) '0'

b)

b) '0.1'

c)

c) TypeError: float argument required, not str

d)

d) TypeError: not all arguments converted during string formatting

e)

e) TypeError: not enough arguments for format string

89.

Consider the following Python program.

fin = open('words.txt')

for line in fin:    

word = line.strip()    

print(word)

What is word?

a)

a) A file object

b)

b) A list of characters

c)

c) A list of words

d)

d) A string that may have a newline

e)

e) A string with no newline

90.

What is the output from the following interactive Python statement?

>>> '%g %d' % (0.1)

a)

a) '0'

b)

b) '0.1'

c)

c) TypeError: float argument required, not str

d)

d) TypeError: not all arguments converted during string formatting

e)

e) TypeError: not enough arguments for format string

91.

What is the output from the following interactive Python statement?

>>> '%d' % (0.1)

a)

a) '0'

b)

b) '0.1'

c)

c) TypeError: float argument required, not str

d)

d) TypeError: not all arguments converted during string formatting

e)

e) TypeError: not enough arguments for format string

92.

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)

a) Lines in a file

b)

b) Lines in a list

c)

c) Words in a dictionary

d)

d) Words in a list

e)

e) Words in a string

93.

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)

a) ['Music', 'Pictures', 'Desktop', 'Library', 'Documents', 'Downloads']

b)

b) False

c)

c) True

d)

d) /Users/me

e)

e) /Users/me/Documents/file.txt

94.

Consider the following Python program.

fin = open('words.txt')

for line in fin:    

word = line.strip()    

print(word)

What is fin?

a)

a) A file object

b)

b) A list of characters

c)

c) A list of words

d)

d) A string that may have a newline

e)

e) A string with no newline

95.

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)

a) ['Music', 'Pictures', 'Desktop', 'Library', 'Documents', 'Downloads']

b)

b) False

c)

c) True

d)

d) /Users/me

e)

e) /Users/me/Documents/file.txt

96.

Which of the following Python statements runs without error?

a)

a) open('three.txt').write(3)

b)

b) open('three.txt','w').write(3)

c)

c) open('three.txt','w').write(str(3))

d)

d) All of the above

e)

e) None of the above

97.

Handling an exception with a try statement is called throwing an exception.

a)

a) True

b)

b) False

98.

What is the output from the following interactive Python statement?

>>> '%g' % (0.1)

a)

a) '0'

b)

b) '0.1'

c)

c) TypeError: float argument required, not str

d)

d) TypeError: not all arguments converted during string formatting

e)

e) TypeError: not enough arguments for format string

99.

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)

a) ['Music', 'Pictures', 'Desktop', 'Library', 'Documents', 'Downloads']

b)

b) False

c)

c) True

d)

d) /Users/me

e)

e) /Users/me/Documents/file.txt

100.

What is the output of the following Python program?

try:   

fin = open('answer.txt')   

fin.write('Yes')

except:   

print('No')

print('Maybe')

a)

a) Yes

b)

b) No

c)

c) Maybe

d)

d) YesMaybe

e)

e) No Maybe

101.

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)

a) ['Music', 'Pictures', 'Desktop', 'Library', 'Documents', 'Downloads']

b)

b) False

c)

c) True

d)

d) /Users/me

e)

e) /Users/me/Documents/file.txt

102.

Match the following

a)

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.

1.

Formal Languages

b)

Any one of the languages that people speak that evolved naturally.

2.

Natural Languages

c)

An error that does not occur until the program has started to execute but that prevents the program from continuing.

3.

Runtime Error

d)

An error in a program that makes it do something other than what the programmer intended.

4.

Semantic Error

e)

The meaning of a program.

5.

Semantic

103.

Match the following

a)

The structure of a program.

1.

Syntax

b)

An error in a program that makes it impossible to parse — and therefore impossible to interpret.

2.

Syntax Error

c)

One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language.

3.

Token