wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

PY4E Finals

Total questions: 49

Worksheet time: 25mins

Name
Class
Date
1.

What does the following Python program print out?

str1 = "Hello"

str2 = 'there'

bob = str1 + str2

print(bob)

a)

Hello there

b)

Hello

c)

0

d)

Hellothere

2.

What does the following Python program print out?

x = '40'

y = int(x) + 2

print(y)

a)

42

b)

402

c)

x2

d)

int402

3.

How would you use the index operator [ ] to print out the letter q from the string?

x = 'From marquard@uct.ac.za'

a)

print x[-1]

b)

print(x[8])

c)

print(x[7])

d)

print(x[9])

4.

How would you use string slicing [ : ] to print out 'uct' from the string?

x = 'From marquard@uct.ac.za'

a)

print(x[15:3])

b)

print(x[14:3])

c)

print(x[14/17])

d)

print(x[15:18])

e)

print(x[14:17])

5.

What is the iteration variable in the following Python code?

for letter in 'banana':

print(letter)

a)

'banana'

b)

for

c)

in

d)

letter

6.

What does the following Python code print out?

print(len('banana') * 7)

a)

banana7

b)

bananabananabananabananabananabananabanana

c)

0

d)

42

7.

How would you print out the following variable in all uppercase in Python?

greet = 'Hello Bob'

a)

console.log(greet.toUpperCase());

b)

puts(greet.ucase);

c)

print(uc($greet));

d)

print(greet.upper())

8.

Which of the following is not a valid string method in Python?

a)

shout()

b)

split()

c)

startswith()

d)

join()

9.

What will the following Python code print out?

data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'

pos = data.find('.')

print(data[pos:pos+3])

a)

mar

b)

.ma

c)

Sat

d)

uct

10.

Which of the following string methods removes whitespace from both the beginning and end of a string?

a)

strtrunc()

b)

rltrim()

c)

strip()

d)

wsrem()

11.

Where are files stored?

a)

Main Memory

b)

Motherboard

c)

Machine Language

d)

Secondary Memory

12.

What is stored in a "file handle" that is returned from a successful open() call?

a)

The handle has a list of all the files in a particular folder on the hard drive

b)

The handle is a connection to the file's data

c)

The handle contains the first 10 lines of a file

d)

All the data from the file is read into memory and stored in the handle

13.

What do we use the second parameter of the open() call to indicate?

a)

What disk drive the file is stored on

b)

Whether we want to read data from the file or write data to the file

c)

How large we expect the file to be

d)

The list of folders to be searched to find the file we want to open

14.

What Python function would you use if you wanted to prompt the user for a file name to open?

a)

file_input()

b)

alert()

c)

gets()

d)

input()

15.

What is the purpose of the newline character in text files?

a)

It allows us to open more than one file and read them in a synchronized manner

b)

It indicates the end of one line of text and the beginning of another line of text

c)

It adds a new network connection to retrieve files from the network

d)

It enables random movement throughout the file

16.

If we open a file as follows:

xfile = open('mbox.txt')

a)

while ((line = xfile.readLine()) != null) {

b)

for line in xfile:

c)

while (<xfile>) {

d)

while line = xfile.gets

17.

What is the purpose of the following Python code?

fhand = open('mbox.txt')

x = 0

for line in fhand:

x = x + 1

print(x)

a)

Convert the lines in mbox.txt to lower case

b)

Count the lines in the file 'mbox.txt'

c)

Remove the leading and trailing spaces from each line in mbox.txt

d)

Reverse the order of the lines in mbox.txt

18.

If you see extra blank lines in the output of a file read, what Python string function will likely solve the problem?

a)

trim()

b)

startswith()

c)

rstrip()

d)

ljust()

19.

How would you avoid a traceback and print your own error message for a bad file name?

fname = input('Enter the file name: ')

fhand = open(fname)

a)

setjmp / longjmp

b)

try / except

c)

signal handlers

d)

try / catch / finally

20.

What does the following Python code do?

fhand = open('mbox-short.txt')

inp = fhand.read()

a)

Reads the entire file into the variable inp as a string

b)

Prompts the user for a file name

c)

Checks to see if the file exists and can be written

d)

Turns the text in the file into a graphic image like a PNG or JPG

21.

How are "collection" variables different from normal variables?

a)

Collection variables pull multiple network documents together

b)

Collection variables merge streams of output into a single stream

c)

Collection variables can only store a single value

d)

Collection variables can store multiple values in a single variable

22.

What are the Python keywords used to construct a loop to iterate through a list?

a)

foreach / in

b)

for / in

c)

def / return

d)

try / except

23.

For the following list, how would you print out 'Sally'?

friends = ['Joseph', 'Glenn', 'Sally']

a)

print friends[3]

b)

print(friends['Sally'])

c)

print(friends[2])

d)

print(friends[2:1])

24.

What would the following Python code print out?

fruit = 'Banana'

fruit[0] = 'b'

print(fruit)

a)

[0]

b)

b

c)

B

d)

Nothing would print - the program fails with a traceback error

e)

Banana

25.

Which of the following Python statements would print out the length of a list stored in the variable data?

a)

print(data.length())

b)

print(data.Len)

c)

print(data.length)

d)

print(len(data))

26.

What type of data is produced when you call the range() function?

x = list(range(5))

a)

A list of words

b)

A string

c)

A list of integers

d)

A boolean (true/false) value

27.

What does the following Python code print out?

a = [1, 2, 3]

b = [4, 5, 6]

c = a + b

print(len(c))

a)

15

b)

[1, 2, 3, 4, 5, 6]

c)

6

d)

[1, 2, 3]

e)

21

28.

Which of the following slicing operations will produce the list [12, 3]?

t = [9, 41, 12, 3, 74, 15]

a)

t[12:3]

b)

t[2:4]

c)
  • t[1:3]

d)
  • t[2:2]

29.

What list method adds a new item to the end of an existing list?

a)

index()

b)

append()

c)

forward()

d)

add()

30.

What will the following Python code print out?

friends = ['Joseph', 'Glenn', 'Sally']

friends.sort()

print(friends[0])

a)

Glenn

b)

Joseph

c)

Sally

d)

friends

31.

How are Python dictionaries different from Python lists?

a)

Python lists store multiple values and dictionaries store a single value

b)

Python dictionaries are a collection and lists are not a collection

c)

Python lists are indexed using integers and dictionaries can use strings as indexes

d)

Python lists can store strings and dictionaries can only store words

32.

What is a term commonly used to describe the Python dictionary feature in other programming languages?

a)

Sequences

b)

Lambdas

c)

Associative arrays

d)

Closures

33.

What would the following Python code print out?

stuff = dict()

print(stuff['candy'])

a)

candy

b)

0

c)

-1

d)

The program would fail with a traceback.

34.

What would the following Python code print out?

stuff = dict()

print(stuff.get('candy', -1))

a)

0

b)

-1

c)

The program would fail with a traceback

d)

candy

35.

What is a common use of Python dictionaries in a program?

a)

Splitting a line of input into words using a space as a delimiter

b)

Building a histogram counting the occurrences of various strings in a file

c)

Computing an average of a set of numbers

d)

Sorting a list of names into alphabetical order

36.

Which of the following lines of Python is equivalent to the following sequence of statements assuming that counts is a dictionary?

if key in counts:

counts[key] = counts[key] + 1

else:

counts[key] = 1

a)

counts[key] = (key in counts) + 1

b)

counts[key] = (counts[key] * 1) + 1

c)

counts[key] = counts.get(key, 0) + 1

d)

counts[key] = counts.get(key, -1) + 1

37.

In the following Python code, what does the for loop iterate through?

x = dict()

...

for y in x:

...

a)

It loops through the keys in the dictionary

b)

It loops through the integers in the range from zero through the length of the dictionary

c)

It loops through the values in the dictionary

d)

It loops through all of the dictionaries in the program

38.

Which method in a dictionary object gives you a list of the values in the dictionary?

a)

values()

b)

items()

c)

keys()

d)

all()

39.

What is the purpose of the second parameter of the get() method for Python dictionaries?

a)

The key to retrieve

b)

To provide a default value if the key is not found

c)

The value to retrieve

d)
  • An alternate key to use if the first key cannot be found

40.

What is the difference between a Python tuple and Python list?

a)

Lists maintain the order of the items and tuples do not maintain order

b)

Lists are indexed by integers and tuples are indexed by strings

c)

Tuples can be expanded after they are created and lists cannot

d)

Lists are mutable and tuples are not mutable

41.

Which of the following methods are available for both Python lists and Python tuples?

a)

append()

b)

reverse()

c)

index()

d)

sort()

42.

What will end up in the variable y after this code is executed?

x, y = 3, 4

a)

A two-item list

b)

3

c)

4

d)

A two-item tuple

43.

In the following Python code, what will end up in the variable y?

x = { 'chuck' : 1 , 'fred' : 42, 'jan': 100 }

y = x.items()

a)

A list of strings

b)
  • A list of integers

c)

A list of tuples

d)

A tuple with three integers

44.

Which of the following tuples is greater than x in the following Python sequence?

x = (5, 1, 3)

if ??? > x :

...

a)

(0, 1000, 2000)

b)
  • (4, 100, 200)

c)
  • (5, 0, 300)

d)

(6, 0, 0)

45.

What does the following Python code accomplish, assuming c is a non-empty dictionary?

tmp = list()

for k, v in c.items():

tmp.append((v, k))

a)

It computes the largest of all the values in the dictionary

b)

It sorts the dictionary based on its key values

c)

It creates a list of tuples where each tuple is a value, key pair

d)

It computes the average of all the values in the dictionary

46.

If the variable data is a Python list, how do we sort it in reverse order?

a)

data.sort.reverse()

b)

data.sort(reverse=True)

c)

data = data.sort(-1)

d)

data = sortrev(data)

47.

Using the following tuple, how would you print 'Wed'?

days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')

a)

print[days(2)]

b)

print(days[1])

c)

print(days{2})

d)

print(days[2])

48.

In the following Python loop, why are there two iteration variables (k and v)?

c = {'a': 10, 'b': 1, 'c': 22}

for k, v in c.items():

...

a)

Because the keys for the dictionary are strings

b)

Because for each item we want the previous and current key

c)

Because the .items() method in dictionaries returns a list of tuples

d)

Because there are two items in the dictionary

49.

Given that Python lists and Python tuples are quite similar, when might you prefer to use a tuple over a list?

a)

For a list of items that will be extended as new items are found

b)

For a temporary variable that you will use and discard without modifying

c)

For a list of items you intend to sort in place

d)

For a list of items that want to use strings as key values instead of integers