Completed
Functions, File Handling & Stack
Questions From This Challenge
Q1output1 mark
What will be the output of the following Python code?
def change(a, b=3):
a = a + b
b = a - b
return a, b
x = 5
y = change(x)
print(x)
print(y)
Correct output:
5
(8, 5)Q2output1 mark
What will be the output of the following Python code?
def calculate(a, b=2, c=3):
return a + b * c
print(calculate(4, 5))
print(calculate(4, c=5))
Correct output:
14
14Q3mcq1 mark
Which of the following function calls is valid for the given function? def display(a, b=10, c=20): print(a, b, c)
display()
display(5, , 15)
display(5, c=15) ✓
display(a=5, 10)
Q4output1 mark
What will be the output of the following Python code?
def test(n):
if n % 2 == 0:
return n + 3
return n * 2
x = test(5)
print(test(x))
Correct output:
16Q5output1 mark
A text file "data.txt" contains:
ABC
DEF
GHI
What will be printed by the following code?
f = open("data.txt", "r")
print(f.readline())
print(f.readline().strip())
f.close()
Correct output:
ABC
DEFQ6output1 mark
What will be the output of the following Python code?
f = open("data.txt", "w")
f.write("ABCDE")
f.close()
f = open("data.txt", "r")
f.seek(2)
print(f.read(2))
f.close()
Correct output:
CDQ7mcq1 mark
A text file contains the following line: Python Programming What will be printed? f = open("data.txt", "r") data = f.read() f.close() print(len(data))
18
19 ✓
20
21
Q8output1 mark
What will be the output of the following Python code?
f = open("data.txt", "w")
f.write("A")
f.close()
f = open("data.txt", "a")
f.write("BC")
f.close()
f = open("data.txt", "r")
print(f.read())
f.close()
Correct output:
ABCQ9output1 mark
A CSV file contains:
101,Ravi,85
102,Anu,92
What will be the output of the following code?
import csv
with open("marks.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
if int(row[2]) > 90:
print(row[1])
Correct output:
AnuQ10output1 mark
What will be the output of the following Python code?
import csv
with open("data.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["A", 10])
writer.writerow(["B", 20])
with open("data.csv", "r") as f:
reader = csv.reader(f)
total = 0
for row in reader:
total = total + int(row[1])
print(total)
Correct output:
30Q11output1 mark
What will be the output of the following Python code?
import pickle
data = [10, 20]
with open("data.dat", "wb") as f:
pickle.dump(data, f)
data.append(30)
with open("data.dat", "rb") as f:
x = pickle.load(f)
print(x)
Correct output:
[10, 20]Q12mcq1 mark
A binary file "student.dat" contains multiple records stored using pickle. Which code correctly reads all records until the end of the file?
while True: ✓
while f:
for rec in pickle.load(f):
while EOFError:
Q13mcq1 mark
A stack contains [10, 20, 30, 40], where 40 is the TOP. The following operations are performed: POP() PUSH(50) POP() PUSH(60) What is the final stack?
[10, 20, 30, 60] ✓
[10, 20, 30, 50]
[10, 20, 40, 60]
[10, 20, 30, 40, 60]
Q14output1 mark
What will be the output of the following Python code?
stack = [] stack.append(5) stack.append(10) x = stack.pop() stack.append(x + 5) print(stack.pop())
Correct output:
15Q15mcq1 mark
A stack is initially empty. The following operations are performed: PUSH(10) PUSH(20) POP() PUSH(30) PUSH(40) POP() POP() What value is returned by the LAST POP()?
10
20
30 ✓
40