wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python Unit 6 - Data and Sorting

Total questions: 33

Worksheet time: 17mins

Name
Class
Date
1.

Python function that connects to a file in order to access it in any way.

a)

open

b)

read

c)

write

d)

close

2.

Python method that is used to put data into a file.

a)

open

b)

read

c)

write

d)

close

3.

Python method that is used to get data from an existing file.

a)

open

b)

read

c)

write

d)

close

4.

Python method that is used to make sure everything has been written to the file and releases the file for later use or use by another program.

a)

open

b)

read

c)

write

d)

close

5.

To add data to the END of a list.

a)

append

b)

'\n'

c)

strip

d)

str

6.

Python function to convert numeric values to a string.

a)

append

b)

'\n'

c)

strip

d)

str

7.

Used to force a new line when writing to a file, indicates that the end of a line or record.

a)

append

b)

'\n'

c)

strip

d)

str

8.

Removes whitespace (nonprintable) characters, tabs, line returns from a string.

a)

append

b)

'\n'

c)

strip

d)

str

9.

A collection of data or information created using brackets [ ] that is one-dimensional (it only has length) and it's values can be added, changed, or deleted.

a)

list

b)

tuple

c)

table

d)

string

10.

A collection of data or information created using parentheses ( ) that is one-dimensional (it only has length) and it's values cannot be added, changed, or deleted.

a)

list

b)

tuple

c)

table

d)

string

11.

A “list of lists” comprised of multiple rows of lists. Items are accessed using rows & positions

a)

list

b)

tuple

c)

table

d)

string

12.

You have a tuple defined as:

yummy = ("Krispie", "Kreme", "Donut")


After noticing it was misspelled, you entered this instruction:

yummy[0] = "Krispy"


Will it correct and replace the 1st item in the tuple?

a)

Yes

b)

No, it should be - yummy[1] = "Krispy"

c)

No, that would add "Krispy" in front of "Krispie"

d)

It would work except for that immutable thingy.

13.

Method of identifying items in a list using numbers to represent the position of an item in list.

a)

Indexing

b)

Refactoring

c)

Remixing

d)

Bubble Sort

14.

Algorithm to progressively arrange lists by comparing & swapping adjacent list items one at a time to put list in desired order

a)

Indexing

b)

Refactoring

c)

Remixing

d)

Bubble Sort

15.

Rewriting a program to implement changes to simplify the code, improve efficiency, readability, or durability.

a)

Indexing

b)

Refactoring

c)

Remixing

d)

Bubble Sort

16.

Rewriting a program to implement changes to the way a program operates including user input, output, user displays, and what the program does.

a)

Indexing

b)

Refactoring

c)

Remixing

d)

Bubble Sort

17.

You have a program where a menu of choices is displayed and users enter a number to select what they want the program to do.


Which function in Python would be easiest to write and result in the most readable code and less typing for the programmer?

a)

Use an if: and then elif: statements

b)

Use combinations of if: and else: statements

c)

Use the switch function

d)

if statements with multiple "or" booleans ofr comparing values.

18.

Before executing a function, your program needs to know how many items are in a list named - party_guests


Which is the most efficient way to do this?

a)

len(party_guests)

b)

count(party_guests)

c)

Create a function to count the items in a list and call it.

d)

use a for: loop with an index to count them up.

19.

You're writing a program to save a list of your favorite movies to a file. When updating your list, you don't want the program to write over it or crash if you don't have your USB drive in the computer.


Which method should you use to avoid do this when your program starts up?

a)

Use the isfile method.

b)

Write a try: error handler to check for it.

c)

Create a function to scan the usb for it.

d)

You can't save Python data files to a USB.

20.

You've written a try: error handler routine and want it to print: "On to the next task." anytime the try: is executed whether or not an exception occurs.


Which statement would be added to your error handler to do this?

a)

finally:

b)

always:

c)

with:

d)

close:

21.

Your program is going to attempt to open a file and read the contents.


You've written a try: error handler routine and want to make sure the file will automatically be released or closed without having to add a separate close statement to the error handler.


Which statement would be added to your error handler to do this?

a)

finally

b)

release:

c)

with

d)

close

22.

You've written the following code in order to save data to an file. What should be placed in location R of your code?

a)

with

b)

w

c)

write

d)

except

23.

You've written the following code in order to save data to an file. What should be placed in location S of your code?

a)

with

b)

w

c)

write

d)

except

24.

You've written the following code in order to save data to an file. What should be placed in location T of your code?

a)

with

b)

w

c)

write

d)

except

25.

You've written the following code in order to save data to an file. What should be placed in location U of your code?

a)

with

b)

w

c)

write

d)

except

26.

You have written the following piece of code but would like to add an error handler in case there is a file not found exception. Which segment of code should you use?

a)
b)
c)
d)
27.

You have the following code:

f = open(‘mylist.txt’)

f.read.all()


Which exception will be raised?

a)

AttributeError

b)

SyntaxError

c)

SystemError

d)

EOFError

28.

Your file, the_poem, contains the code shown here. Select the line of code that should be added to this code.

a)

thewords = poem.readlines()

b)

thewords = poem.load()

c)

thewords = poem.read()

d)

thewords = poem.readline()

29.

You want to write a birthday message to a file called my_message that will be in this format:


Happy Birthday!


Which code segment should you use?

a)

f = open('my_message','r')

f.write('Happy Birthday!')

f.close()

b)

f = open('my_message','w')

f.write('Happy Birthday!')

f.close()

c)

f = open('my_message','b')

f.write('Happy Birthday!')

f.close()

d)

f = open('my_message')

f.write('Happy Birthday!')

f.close()

30.

You have a file named game_scores.txt with the following data:


"Bob", 1000, 5000, 7500

"Bill", 9000, 6000, 3000


Your code should read all the contents of the file. Which code segment should be used?

a)

with open("game_scores.txt","r") f:

file_data = f.read()

b)

with open("game_scores.txt") as f:

file_data = f.read()

c)

with open("game_scores.txt","w") as f:

file_data = f.read()

d)

with open("game_scores,txt", “b”) as f:

file_data = f.read()

31.

You are writing a program which will use a file named "free_throws.txt" to track you basketball team's free throws made or missed.


In your program, you use this line of code:


shots = open("free_thows.txt")


Since you didn't specify the file mode, what mode will Python use as a default in the program?

a)

read 'r'

b)

write 'w'

c)

read and write 'w+'

d)

read a binary file 'b'

32.

A program has the code shown here. What will be displayed?

a)

3500

b)

9000

c)

6000

d)

500

33.

You have the following code of the SEC games left to play in 2020. The first name in each row of your table is the team, followed by their opponents for the next 4 weeks.


What would be the code to print Florida's opponent for 3rd game in this list?

a)

print(sec_games[2][3])

b)

print(sec_games[3,4])

c)

print(sec_games[3][4])

d)

print(sec_games(2,3))

e)

None of the selections are correct.