0% found this document useful (0 votes)
38 views6 pages

Practice Test 2

Python Class 12 Practice Paper

Uploaded by

hempant1978
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Practice Test 2

Python Class 12 Practice Paper

Uploaded by

hempant1978
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ARMY PUBLIC SCHOOL, RANIKHET

CLASS-XII
COMPUTER SCIENCE
PRACTICE TEST SERIES-II [2024-2025]
MM:70 TIME: 03 HRS

GENERAL INSTRUCTIONS:

 Programming language ‘python’


 Avoid cutting and erasing
 If you re-answer any question, cancel the previous answer.
 Section A contains 18 questions of 1 marks each, section B contains 10 Questions of 2 marks each, whereas
section C contains 4 Questions of 3 marks each and 4 Questions of 5 marks each.

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.

Data Structure stack is known as:


(d) (a) First in First Out (c) First in Last Out 1
(b) Last in First Out (d) All of these

In a Stack, deletion takes place at_____________end


(e) (i) front (ii) top (iii) rear (iv) any 1

Which of the following statement is true? 1


(f) a. pickling creates an object from a sequence of bytes
b. pickling is used for object serialization
c. pickling is used for object deserialization
d. pickling is used to manage all types of files in Python

(g) What is stack? Write some applications of stack, 1

(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

(i) The default file-open mode is ……….. 1

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()

Identify the missing code in Statement 1.


a. dump(myfile,t)
b. dump(t, myfile)
c. write(t,myfile)
d. load(myfile,t)

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.

(o) What method is used to write data into a CSV file? 1

(p) Write the difference between readline() and readlines() methods. 1

(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.

Reasoning: Text files can be opened in text as well as binary modes.

(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

Q3.(a) A dictionary, d_city contains the records in the following format : 3

{state:city}

Define the following functions with the given specifications :


(i) push_city(d_city): It takes the dictionary as an argument and pushes all the cities in the stack
CITY whose states are of more than 4 characters.
(ii) pop_city(): This function pops the cities and displays "Stack empty" when there are no more
cities in the stack

(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]

And on execution of PopBig() , the following output should be displayed :

92765

31498

1297653

254923

10025
Stack is Empty

(a) Write one difference , between CSV and text files.


(e) 5
(b) Write a program in Python that defines and calls the following user _ defined functions:

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.

(f) 1) Write a method/function COUNTWORDS() in Python to read contents 3+2


from a text file DECODE.TXT, to count and return the occurrence of
those words, which are having 5 or more characters.
OR
(b) Write a method/function COUNTLINES() in Python to read lines from
a text file CONTENT.TXT, and display those lines, which have @ anywhere in the line.

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

2) A Binary file, "Items.dat" has the following structure :


[Icode, Description, Price] Where
Icode – Item code, Description – Detail of item
Price – Price of item. Write a function Add_data(), that takes Icode,
Description and Price from the user and writes the information in the binary file "Items.dat".
(h) What is the main purpose of seek() and tell() method ? 1+4
(ii) Consider a binary file, Cinema.dat containing information in
the following structure :
[Mno, Mname, Mtype]
Write a function, search_copy(), that reads the content from the file Cinema.dat and copies all
the details of the "Comedy" movie type to file named movie.dat.

You might also like