Page 21 - PYTHON-12
P. 21
OR
(B) TCP stands for Transmission Control Protocol.
TCP enables two hosts to establish a connection and exchange streams of data.
It also collects all the packets at the receiver’s end and arranges them in a sequential order.
Section C
29. (A) Write a Python function that reads ‘Words.txt’ and displays the number of times the word ‘Python’
appears in the file. (3)
OR
(B) Write a Python function that reads ‘Words.txt’ and displays the first 5 words from the file.
Ans. (A) def count():
count = 0
try:
with open('Words.txt', 'r') as file:
for line in file:
words = line.split()
for word in words:
if word == "Python":
count += 1
print('The word "Python" appears', count, 'times in the file.')
except FileNotFoundError:
print("Error: The file was not found.")
OR
(B) def display_words():
try:
with open('Words.txt', 'r') as file:
words = []
for line in file:
words.extend(line.split())
if len(words) >= 5:
break
print("The first 5 words in the file are:", words[:5])
except FileNotFoundError:
print("Error: The file Words.txt was not found.")
30. (A) In a call centre, customer complaints are handled through a Stack named ComplaintStack. Each
complaint is represented as a list with details: complaint_id, customer_name and complaint_details.
Write the following user-defined functions in Python to perform the specified operations on
ComplaintStack: (3)
Model Test Paper (Solved) A.7