0% found this document useful (0 votes)
5 views

Computer Science Practice Questions-2, fILE and DS

This document contains practice questions for a Computer Science topic focused on Data File and Stack Data Structure, with a total of 20 marks. It includes multiple-choice questions, coding tasks, and instructions for students to engage with the material and seek help if needed. The questions cover binary files, CSV file handling, stack operations, and specific coding challenges related to file input/output.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Computer Science Practice Questions-2, fILE and DS

This document contains practice questions for a Computer Science topic focused on Data File and Stack Data Structure, with a total of 20 marks. It includes multiple-choice questions, coding tasks, and instructions for students to engage with the material and seek help if needed. The questions cover binary files, CSV file handling, stack operations, and specific coding challenges related to file input/output.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Computer Science

Practice Questions
Topic: Data File & Stack Data Structure
Full Marks 20
NOTE
• You are advised against doing selective study.
• The questions given above are sample questions for practice prior to Board Examination.
• Although Answer Keys will be provided within 2-3 days of posting of these questions, you are
advised to answer them yourself.
• In case you have queries regarding a portion of the chapters being revised here you may send
your query to your teacher through Chat section in MS Teams.
1. A binary file: 1
a. Is faster to read and write than text file
b. Can be opened by programs in any programming language
c. Can be used to store only text type data
d. Stores data in a human readable format
2. Which of the following is a valid statement? 1
a. A csv file is a binary file
b. The seek() function gives you the location of the file pointer
c. The fetchall( ) function works with a csv file
d. The writeline() function can be used to write data to a text file
3. Which of the following is a valid syntax for working with a file object? 1
a. fout = open ( ‘C:\new\temp\abc.txt’ )
b. with fout = open ( ‘abc.csv’, ‘w’ ):
c. fin.seek(0, 0)
d. n = tell( fin )
4. Which of the following is an invalid syntax for working with a csv file? 1
a. readerObj = csv.reader( fileObj )
b. fileObj = open( ‘abc.csv’, 'w', newline = '' )
c. writerObj.writerow( [ ‘abc’, ‘pqr’, ‘xyz’ ] )
d. writerObj = csv.writer( delimiter = ‘#’, fileObj )
5. For adding data to an existing csv file, which of the following is a valid syntax for opening the file? 1
a. with open( 'sports.csv', 'w', newline='') as fcsvOut:
b. with open( 'sports.csv', 'a', newline='') as fcsvOut:
c. with open( 'sports.csv', 'ab', newline='') as fcsvOut:
d. with open( 'sports.csv', 'wb', newline='') as fcsvOut:
6. When working with a stack type data structure which of the following statements is invalid? 1
a. The push() function can insert data at the front of the stack
b. The pop() function always removes the last data from the stack
c. A stack can be called a FILO type data structure
d. The working of hyperlinks in a web browser to move forward and backward through different
web-pages is based on the working of a stack
7. Find the output of the following code based on the text file cities.txt shown below: 2
fin = open('cities.txt')
count = 1 cities.txt
while count < 15: vishakhapattanam
s = fin.read(2) vellore
count += 1 vadodhara
if s[-1] in 'shortly': vijayanagaram
print(s[0], end='')
fin.close()
8. Find the output of the following code based on the text file cities.txt shown below: 2
fin = open('cities.txt')
while True: cities.txt
s = fin.readline(15) vishakhapattanam
if s == '': break vellore
print( s[:4] ) vadodhara
fin.close() vijayanagaram

9. Find the output of the following code based on the text file cities.txt shown below: 2
fin = open('cities.txt')
cities.txt
M = fin.readlines()
for x in M: vishakhapattanam
print( len(x) ) vellore
fin.close() vadodhara
vijayanagaram

10. Find the output of the following code based on the text file cities.txt shown below: 2
fin = open('cities.txt')
for x in fin: cities.txt
L = x.split('r') vishakhapattanam
print( L[-1].rstrip() ) vellore
fin.close() vadodhara
vijayanagaram

11. Fill in the blanks with appropriate code to get the output of the program as shown below: 2
import csv
with open('elements.csv') as fin: elements.csv
RObj = csv. BLANK-1 (fin, BLANK-2 =':') Cu:Copper:Metal
for data in BLANK-3 : Au:Gold:Metal
if data[2] == BLANK-4: B:Boron:Nonmetal
print(data[1])
Cl:Chlorine:Gas
Ag:Silver:Metal

Output of Program

12. A stack FurnStack stores name of a furniture and its cost as elements of a list. Write the code for the
PUSH() function to store the data for one furniture in the stack, whenever it is called. The PUSH
function receives the stack FurnStack and the list LFurn containing the data for a single furniture.
Write the code for the POP() function to take out one list type data from the stack and store it under
the main code. 1+1+1 = 3
Write a function called PEEK() to make a copy of the last value stored in the stack and return it. The
original stack remans intact

13. Write the code for a function that receives a dictionary as its argument. The function opens a binary
file called ‘shipment.dat’. It next checks if the ‘destination’ (key) of a shipment is ‘Darjeeling’ (value) or
not. If so, the received dictionary is directly written to the binary file. Otherwise, it is not written to the
file

. 2

You might also like