Completed
Functions, Exceptions & File Handling Challenge
Questions From This Challenge
Q1output1 mark
What will be the output of the following Python code?
x = 10
def change():
global x
x = x + 5
def inner():
x = 20
return x
return inner() + x
y = change()
print(x, y)
Correct output:
15 35Q2mcq1 mark
Consider the following function definition: def calculate(a, b=4, c=2): return a + b * c What will be the output? print(calculate(b=2, a=3)) print(calculate(3, c=1)) print(calculate(c=5, a=1, b=0))
7, 7, 1 ✓
7, 11, 1
11, 7, 5
Error occurs due to out-of-order arguments
Q3output1 mark
What will be the output of the following Python code?
data = [10, 20]
def test(data):
data.append(30)
data = [40, 50]
data.append(60)
test(data)
print(data)
Correct output:
[10, 20, 30]Q4output1 mark
What will be the output of the following Python code?
def operation(a, b):
return a + b, a - b, a * b
x, *y = operation(5, 2)
print(x, y)
Correct output:
7 [3, 10]Q5mcq1 mark
Which of the following function calls is INVALID for the given function definition? def func(p, q, r=10, s=20): pass
func(1, 2, s=30)
func(p=1, q=2, r=3)
func(1, q=2, 30) ✓
func(1, 2)
Q6output1 mark
What will be the output of the following Python code?
def show(a, L=[]):
L.append(a)
print(L)
show(5)
show(10, [1, 2])
show(20)
Correct output:
[5]
[1, 2, 10]
[5, 20]Q7output1 mark
What will be the output of the following Python code?
def check_error():
try:
return 10 // 2
except Exception:
return 0
finally:
return "Done"
print(check_error())
Correct output:
DoneQ8mcq1 mark
A text file named "poem.txt" contains exactly the following 3 lines, and each line ends with a newline character: Line1 Line2 Line3 The following code is executed: f = open("poem.txt", "r") lines = f.readlines() print(len(lines[0])) f.close() What will be printed?
5
6 ✓
4
15
Q9mcq1 mark
Which of the following modes opens an existing TEXT file for both reading and writing, raises FileNotFoundError if the file does not exist, and does NOT truncate its existing contents?
w+
r+ ✓
a+
w
Q10mcq1 mark
What will be the output of the following Python code if data.txt initially contains "ABC"? with open("data.txt", "a+") as f: f.write("DEF") print(f.read())
ABCDEF
DEF
Blank (no output is printed) ✓
An Error occurs
Q11output1 mark
What will be the output of the following Python code?
f = open("data.txt", "w")
f.writelines(["Apple", "Mango", "Banana"])
f.close()
f = open("data.txt", "r")
print(f.readline())
f.close()
Correct output:
AppleMangoBananaQ12mcq1 mark
A binary file named emp.dat contains 5 dictionary objects pickled sequentially. What happens when the following code is executed? import pickle try: with open("emp.dat", "rb") as f: while True: rec = pickle.load(f) except EOFError: print("Done")
It reads indefinitely, causing an infinite loop
It successfully reads all 5 objects and prints "Done" when finished ✓
It raises a PickleError after reading the first object
It prints "Done" immediately without reading any objects
Q13output1 mark
What will be the output of the following Python code?
f = open("data.txt", "w+")
f.write("PYTHON")
f.seek(2)
f.write("A")
f.seek(0)
print(f.read())
f.close()
Correct output:
PYAHONQ14mcq1 mark
Consider a file log.txt containing the text "12345". What is the final content of the file after executing this code? f = open("log.txt", "a") f.seek(0) f.write("67") f.close()
67345
1234567 ✓
67
An Error occurs
Q15mcq1 mark
Which statement correctly opens a CSV file for writing while allowing the csv module to handle newline characters appropriately?
open("data.csv", "r")
open("data.csv", "w", newline="") ✓
open("data.csv", "a+")
open("data.csv", "rb")