Important Note: The exact wording and numerical values in your actual exam might vary, but the core concepts and required programming logic to solve these problems remain consistent. Practice these questions diligently to ace this section!
Previous Year Questions (Last 5 Years)
Year 2024 (Expected/Sample Questions based on recent patterns)
Write a Python function
countWords(filepath)
that takes the path of a text file as an argument and returns the total number of words in the file. Assume words are separated by spaces.Explain the difference between
w
anda
modes for opening a text file in Python.A binary file
student.dat
stores student records as a list[RollNo, Name, Marks]
. Write a Python functiondisplayAboveMarks(min_marks)
that reads thestudent.dat
file and displays the details of all students who scored more thanmin_marks
.
Year 2023
Write a Python program to count the number of lines in a text file
NOTES.TXT
that do not start with a vowel (A, E, I, O, U).Differentiate between text file and binary file based on their data storage.
Write a Python function
createCSVFile(filename)
to create a CSV file namedStudents.csv
and write the following data into it:RollNo, Name, Grade 1, Amit, A 2, Priya, B 3, Rohan, A
Year 2022
A text file
REPORT.TXT
contains some text. Write a functioncountOccurrences(word)
that accepts a word as an argument and counts the number of times this word appears in theREPORT.TXT
file (case-insensitive).What is the purpose of the
seek()
andtell()
Methods in file handling?A binary file
PRODUCT.DAT
stores product information as a dictionary{'PCode': 'P001', 'PName': 'Laptop', 'Price': 85000}
. Write a Python functionupdatePrice(PCode, new_price)
that takes a product code and a new price, and updates the price of that product in thePRODUCT.DAT
file.
Year 2021
Write a Python function
readReverse(filename)
that reads a text fileDATA.TXT
and displays its content in reverse order (character by character, not line by line).What is the significance of
newline=''
opening a CSV file in Python?Consider a CSV file
Marks.csv
with data like:RollNo,Subject,Marks
. Write a Python functioncountStudentsFailed(subject_name)
that counts and returns the number of students who scored less than 33 marks in the givensubject_name
.
Year 2020
Write a Python function
displayLongestWord(filename)
that reads a text fileSTORY.TXT
and displays the longest word present in the file. If there are multiple words of the same maximum length, display any one of them.Give an example of how to write a Python list
my_list = [10, 20, 30]
into a binary filenumbers.dat
.Write a Python function
searchStudent(roll_no)
to search for a student by theirroll_no
in a CSV filestudents.csv
. If found, display their details; otherwise, print "Student not found." The file has columns:RollNo,Name,Class
.