Completed
Functions, Exceptions & File Handling Challenge 3
Questions From This Challenge
Q1output1 mark
What will be the output of the following Python code?
def test(a, b=3):
return a + b * 2
print(test(4))
print(test(4, 5))
Correct output:
10
14Q2output1 mark
What will be the output of the following Python code?
def calculate(a, b):
return a - b, a + b
x, y = calculate(8, 3)
print(x, y)
Correct output:
5 11Q3mcq1 mark
Which function call correctly passes 10 as the value of x and 20 as the value of y to the following function? def show(x, y): print(x, y)
show(x=10, y=20)
show(10, y=20)
show(y=20, x=10)
All of the above ✓
Q4output1 mark
What will be the output of the following Python code?
x = 10
def change():
x = 20
return x
print(change())
print(x)
Correct output:
20
10Q5output1 mark
What will be the output of the following Python code?
def update(values):
values.append(30)
return values
numbers = [10, 20]
result = update(numbers)
print(numbers)
print(result)
Correct output:
[10, 20, 30]
[10, 20, 30]Q6output1 mark
What will be the output of the following Python code?
try:
n = int("abc")
print(n)
except ValueError:
print("Invalid value")
finally:
print("Completed")
Correct output:
Invalid value
CompletedQ7output1 mark
What will be the output of the following Python code?
try:
a = 20
b = 4
print(a / b)
except ZeroDivisionError:
print("Error")
finally:
print("Done")
Correct output:
5.0
DoneQ8mcq1 mark
A program needs to open an existing text file, read its contents, and then write new content to the same file without first deleting the existing content. Which mode is appropriate?
"r"
"w"
"r+" ✓
"wb"
Q9output1 mark
What will be the output of the following code if data.txt contains the text ABCDE?
with open("data.txt", "r") as f:
print(f.read(3))
Correct output:
ABCQ10mcq1 mark
What will be written to data.txt after executing the following code? with open("data.txt", "w") as f: f.write("Python") f.seek(0) f.write("ABC")
PythonABC
ABC
ABChon ✓
Error occurs
Q11output1 mark
A text file contains the following three lines:
Red
Green
Blue
What will be the output of the following code?
with open("colors.txt", "r") as f:
lines = f.readlines()
print(len(lines))
Correct output:
3Q12output1 mark
What will be the output of the following code if data.txt initially contains 1234?
with open("data.txt", "a") as f:
f.write("56")
with open("data.txt", "r") as f:
print(f.read())
Correct output:
123456Q13mcq1 mark
Which pair correctly represents the binary file modes for reading and writing respectively?
"rb" and "wb" ✓
"r" and "w"
"br" and "bw"
"read" and "write"
Q14mcq1 mark
Consider the following code: import pickle with open("data.dat", "wb") as f: pickle.dump(25, f) pickle.dump(50, f) with open("data.dat", "rb") as f: a = pickle.load(f) b = pickle.load(f) print(a + b) What will be printed?
25
50
75 ✓
Error
Q15mcq1 mark
A CSV file named students.csv needs to be opened for adding new records while preserving the existing records. Which statement is appropriate?
open("students.csv", "r")
open("students.csv", "w", newline="")
open("students.csv", "a", newline="") ✓
open("students.csv", "rb")