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
Which mode should you use to read a file’s contents?
A. "w"
B. "a"
C. "r"
D. "x"
C. "r"
True or False?
A while loop must always use range().
False
What three-letter keyword is used to define a function?
def
Fill in the blank:
In the line
contents = f.read()
the data type of contents is _____________.
str
True or False?
This code will automatically close the file.
with open("notes.txt", "r") as f:
contents = f.read()
True
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
How many times does this loop run?
for i in range(2, 11, 3):
A. 2
B. 3
C. 4
D. 5
B. 3
What does this function print?
def mystery(a, b, c):
print(a + b - c)
mystery(2, 3, 1)
4
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()
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())
Fill in the blank:
The file mode that adds text without deleting existing content is "___".
a
Fill in the blank to make this loop print 0 last:
x = 12
while x >= 0:
print(x)
x = x - ___
3
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
Fill in the blank:
The pandas object created when reading Excel is a __________.
DataFrame
Fill in the blank:
To close a file named f, you should write:
f.__________()
close
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
Which loop is best when you don’t know how many iterations you need?
while
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
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")
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
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
How many times will this loop run?
x = 12
while x >= 0:
print(x)
if x % 3 == 0:
print("Divisible by 3!")
infinite
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
Fill in the blank:
When writing to an existing Excel file without deleting it, the ExcelWriter mode is "___".
a