Practice Test 2
Practice Test 2
CLASS-XII
COMPUTER SCIENCE
PRACTICE TEST SERIES-II [2024-2025]
MM:70 TIME: 03 HRS
GENERAL INSTRUCTIONS:
SECTION-A
Q1.(a) Write Python statements to open a binary file "student.dat" in both read & write mode.
1
(b) Which of the following statement is incorrect in the context of binary files?
a. Information is stored in the same format in which the information is held in memory. 1
b. No character translation takes place
c. Every line ends with a new line character
d. pickle module is used for reading and writing
(c) ASSERTION AND REASONING-based questions. Mark the correct choice as
(a) Both A and R are true and R is the correct explanation for A 1
(b) Both A and R are true and R is not the correct explanation for A
(c) A is True but R is False
(d) A is false but R is True
Assertion (A): CSV (Comma Separated Values) is a file format for data storage that looks like a
text file.
Reason (R): The information is organized with one record on each line and each field is
separated by comma.
(h) Read the following Python code carefully and answers the question given afterthe code 1
import pickle
#open file in binary mode for writing.
with open('emp.dat', '____') as outfile: #Line 1
#Store data in list
employee = [101,'Simran',20000]
_________________ #Line 2
(i) Fill in the blank in line 1 to open file in binary mode for append data to the file.
(ii) Fill in the blank in line 2 to pickle the list and write to file
Which of the following file mode open a file for reading and writing both in the binary file?
(j) a) r b) rb 1
c) rb+ d) rwb
Raghav is trying to write a tuple t = (1,2,3,4,5) on a binary file test.bin.
(k) Consider the following code written by him. 1
import pickle
t = (1,2,3,4,5)
myfile = open("test.bin",'wb')
pickle._________ #Statement 1
myfile.close()
Assertion (A) : If numeric data are to be written to a text file, the data needs to be converted
(l) into a string before writing to the file. 1
Reason (R) : write() method takes a string as an argument and writes it to the text file.
(A) Both Assertion (A) and Reason (R) are true and Reason (R) is the correct explanation of
Assertion (A).
(B) Both Assertion (A) and Reason (R) are true, but Reason (R) is not the correct explanation of
Assertion (A).
(C) Assertion (A) is true, but Reason (R) is false.
(D) Assertion (A) is false, but Reason (R) is true.
(m) Which of the following function do you use to write data in the binary format: 1
(i) write() (ii) writerows() (iii) dump (iv) writerow()
Assertion: CSV file is a human readable text file where each line has a number of fields,
(n) separated by comma or some other delimeter. 1
Reason: writerow() method is used to write a single row in a CSV file.
A) Both Assertion (A) and Reason (R) are true and Reason (R) is the correct explanation of
Assertion (A).
(B) Both Assertion (A) and Reason (R) are true, but Reason (R) is not the correct explanation of
Assertion (A).
(C) Assertion (A) is true, but Reason (R) is false.
(D) Assertion (A) is false, but Reason (R) is true.
(q)
__________ files are stored in a computer in a sequence of bytes. 1
(A) Text (B) Binary
(C) CSV (D) Notepad
(r) Write commands to create a writer and reader objects to operate a csv file. 1
import csv
myfile=open(‘stock.csv’, ‘w+’, newline=)
__________________________#reader object
_________________________writer object
SECTION-B
Q2. (a) Write a function count_dwords() in python that counts numbers of words in a text file 2
“details.txt” ends with a digit.
If the file content is as follows:
On seat2 VIP1 will sit and on seat1 VVIP2 will sit.
Output will be:
Total words ending with a digit are: 4
(b) There is a text file “ Bharat.txt” containing some line of text as under: 2
“Army Public School, Ranikhet is situated between lush green of Pine and Deodar trees.” Write
the output of the given code:
x=open("india.txt", "r")
print(x.tell())
k=x.read(5)
print(x.tell())
(c) Write a function countmy() in python to read the text file "DATA.TXT" and count the number of
times "my" occurs in the file. For example if the file DATA.TXT contains-"This is my website. I
have displayed my preference in the CHOICE section ".After calling the countmy() function the
output should display the output as:"my occurs 2 times". 2
(d) Write the blank1 and Blank2 statements for the following python code to complete the python
code of records modification:
2
#Blank 1________________
with open("hem.csv","r",newline='') as f:
#Blank 2________________ reader object r
for i in r:
for j in i:
print(j,"\t\t\t",end='')
print()
(e) Write the blank1 and Blank2 statements for the following python code to complete the python
code of records modification:
#Blank1 __________________Write a statement that enables python to work with csv files
with open("hem.csv","w",newline='') as f: 2
w=csv.writer(f)
w.writerow(['Admno', 'Name','Marks'])
while True:
admno=input("Enter Adm No:")
name=input("Enter name:")
marks=int(input("Enter Marks"))
#Blank2_____________to write the above data into hem.csv file in the list form
ans=input("Enter More Records(y/n):")
if ans.upper()=='N':
break
Questions (i) to (ii) are ASSERTION AND REASONING based questions. Mark the correct choice
as:
A. Both A and R are true and R is the correct explanation for A
B. Both A and R are true and R is not the correct explanation for A
C. A is True but R is False 2
(f) D. A is false but R is True
(i) Assertion: Text files can only store objects as strings in them.
(ii) Assertion: CSV files stores data in a file with values separated by commas and we can
open them normally using open() function.
Reasoning: CSV files are simple text files with .csv extension.
(g) Consider a binary file “book.dat” that has structure [BookNo,Book_Name, Author, Price]. 2
Write a user defined function CreateFile() that takes input data for a record and add to
book.dat
(h) Write a function that will display only those lines of a text file “bharat.txt” which are starting 2
with letter ‘M’
(i) Write a function to read and display the contents of a csv file “data.csv”, the file contains data 2
in the given format: [admno, name, class, per]
(j) WAF to read and display only those words from a text file “ranikhet.txt” which are starting with 2
vowel characters.
SECTION-C
{state:city}
(b) Write a python function push(stack) that pushes on to an empty list(stack) all the values of a 3
dictionary whose keys are palindromes.
Eg: for the dictionary {'ROY':100, 'ARORA':200, 'MADAM':250, 'SAHA':300, 'LEVEL':400} the stack
should have the values 200, 250 and 400 since ARORA, MADAM and LEVEL are palindromes.
Write a python function popstack(stack) that pops out the last added element from the stack
each time the function is called and also prints a 'Stack Empty' message if all the elements have
popped out.
Also write the call statements for both the functions.
(c) A pre-written text file data.txt has some strings stored in it. Write a python function display() 3
that reads the contents of the file and displays all the words that start with the substring 'th' or
'Th'.
Eg. If the file has the following data:
“There was a man who throughout his life
thanked others for their little help”
The output should be:
There
throughout
thanked
(d) Consider a list named Nums which contains random integers. Write the following user defined 3
functions in Python and perform the specified operations on a stack named BigNums.
G PushBig() : It checks every number from the list Nums and pushes all such numbers which
have 5 or more digits into the stack, BigNunms.
(ii) PopBig() : It pops the numbers from the stack, BigNums and displays them. The function
should also display "Stack Empty" when there are no more numbers left in the stack.
For example: If the list Nums contains the following data : Nums = [213,10025,167,254923,14,
1297653, 31498 , 386, 92765]
Then on execution of PushBig(), the stack BigNums should store : [10025, 254923, 1297653,
31498, 92765]
92765
31498
1297653
254923
10025
Stack is Empty
COURIER_ADD( ): It takes ‘the values from the user ‘and adds : the details to a csv file
'courier.csv'. Each record consists of a list with field' elements as Cid, S_name, Source,
destination to store Courier ID, Sender name, Source and destination address respectively.
COURIER_SEARCH() : Takes the destination a the input and displays all the courier records going
to that destination.
2) Write a function to create a csv file cricket.csv, this file stores records in the given format:
[playercode, player_name, runs, wickets]
(g) 1) Give one difference between write() and writeline() function in text file. 1+4