Fix the Code
File Things
Loop the Loop
Functions
Mixing it Up
100

What is wrong with this line?

f = open("data.txt", "r"

A. Missing file name
B. Missing quotation marks
C. Missing closing parenthesis
D. Wrong file mode

C. Missing closing parenthesis

100

Which mode should you use to read a file’s contents?

A. "w"
B. "a"
C. "r"
D. "x"

C. "r"

100

True or False?

A while loop must always use range().

False

100

What three-letter keyword is used to define a function?

def

100

Fill in the blank:

In the line 

contents = f.read()

the data type of contents is _____________.

str

200

True or False?

This code will automatically close the file.

with open("notes.txt", "r") as f:

    contents = f.read()

True

200

What does this line do?

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

A. Opens file for reading only
B. Opens file and deletes contents before writing
C. Opens file without closing it
D. Appends text to the end

B. Opens file and deletes contents before writing

200

How many times does this loop run?

for i in range(2, 11, 3):
A. 2
B. 3
C. 4
D. 5 

B. 3

200

What does this function print?

def mystery(a, b, c):

    print(a + b - c)

mystery(2, 3, 1)

4

200

Which pandas function reads an Excel file?

A. pd.load_excel()
B. pd.open_excel()
C. pd.write_excel()
D. pd.read_excel()

D. pd.read_excel()

300

Which line correctly fixes the error?

print(f.read)


A. print(read.f())
B. print(f.read())
C. print(f.read[])
D. print(f.read("r")) 

B. print(f.read())

300

Fill in the blank:

The file mode that adds text without deleting existing content is "___".

a

300

Fill in the blank to make this loop print 0 last:

x = 12

while x >= 0:

    print(x)

    x = x - ___

3

300

Why use functions?
A. To make code longer
B. To avoid loops
C. To reduce repeated code
D. To store files

C. To reduce repeated code

300

Fill in the blank:

The pandas object created when reading Excel is a __________.

DataFrame

400

Fill in the blank:

To close a file named f, you should write:
f.__________()

close

400

Why is with open(...) as f: preferred?
A. Faster execution
B. Automatically closes the file
C. Reads multiple files
D. Prevents syntax errors

B. Automatically closes the file

400

Which loop is best when you don’t know how many iterations you need?

while

400

What is wrong with the code below?

def add(x, y):
   return x + y

add(2)

A. It should be called addition
B. It should not return a value
C. It does not have a loop
D. It is missing a second parameter

D. It is missing a second parameter

400

Which line writes df to a sheet named "Summary"?
A. df.write_excel("file.xlsx", "Summary")
B. df.to_excel("file.xlsx", sheet="Summary")
C. df.to_excel("file.xlsx", sheet_name="Summary")
D. pd.write_excel(df, "file.xlsx")

C. df.to_excel("file.xlsx", sheet_name="Summary")

500

Why is this code risky?

f = open("data.txt", "w")

f.write("Hello")

A. The file won’t save
B. The file may not close properly
C. The file cannot be written
D. The file opens read-only

B. The file may not close properly

500

What happens if you open an existing file in "w" mode?
A. Python throws an error
B. Text is added to the bottom
C. File is erased and rewritten
D. File becomes read-only

C. File is erased and rewritten

500

How many times will this loop run?

x = 12

while x >= 0:

   print(x)

   if x % 3 == 0:

      print("Divisible by 3!")

infinite

500

What is wrong with the code below?

def print_hello_world:

   print("     Hello World!    ")

A. It is missing parameters
B. Nothing is wrong
C. It is missing parenthesis
D. It has too many spaces in the print statement

C. It is missing parenthesis

500

Fill in the blank:

When writing to an existing Excel file without deleting it, the ExcelWriter mode is "___".

a