0% found this document useful (0 votes)
114 views10 pages

2024 08 06 10 01 Solution

Solution

Uploaded by

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

2024 08 06 10 01 Solution

Solution

Uploaded by

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

VELLAMAL BODHI CAMPUS VELLORE

Date : 06-08-2024 STD 12 Science Computer Science Total Marks : 69


worksheet file handling 4marks

Section A

* Choose The Right Answer From The Given Options.[1 Marks Each] [19]

1. The modes of file in python are


(A) r (B) r+ (C) w (D) All of the these

Ans. : All of the these


2. To open a file "c:marks.txt" for reading, we use
(A) file_read = open("c:marks.txt", "r") (B) file_read = open("c:marks.txt", "r")
(C) file_read = open(file = "c:marks.txt", (D) file_read = open(file = "c:marks.txt",
"r") "r")
Ans. : file_read = open("c:marks.txt", "r")
3. To read two characters from a file object "file_read"
(A) file_read.read(2) (B) file_read(2)
(C) file_read(read,2) (D) file_read.readlines(2)
Ans. : file_read.read(2)
4. To read the content of the file as a string via a file object "f"
(A) f.read (B) f.read (C) f=file.readline() (D) f.readlines()
Ans. : f.read
5. The readlines() returns
(A) only first line from the file (B) only last line from the file
(C) all lines from the file (D) none of the above
Ans. : all lines from the file
6. What is the use of tell() method in Python?
(A) returns the current position of record pointer within the file
(B) returns the end position of record pointer within the file
(C) returns the current position of record pointer within the line
(D) none of the above
Ans. : returns the current position of record pointer within the file
7. What is the syntax of rename() a file?
(A) rename(current_file_name, new_file_name)
(B) rename(new_file_name, current_file_name,)
(C) rename(()(current_file_name, new_file_name))

Page 1
(D) none of these
Ans. : rename(current_file_name, new_file_name)
8. seek() method in files used for
(A) sets the file's current position at the offset
(B) sets the file's previous position at the offset
(C) sets the file's current position within the file
(D) none of these

Ans. : sets the file's current position at the offset


9. What is the use of tell() method in python?
(A) tells you the current position within the file
(B) tells you the end position within the file
(C) tells you the file is opened or not
(D) none of these

Ans. : tells you the current position within the file


10. What is the syntax of remove() a file in python?
(A) remove(file_name)
(B) remove(new_file_name, current_file_name,)
(C) remove(() , file_name))
(D) none of these

Ans. : remove(file_name)
11. What is the pickling?
(A) It is used for object serialization
(B) It is used for object deserialization
(C) None of the mentioned
(D) All of the mentioned
Ans. : It is used for object serialization
12. Which of the following statement is not true regarding the opening modes of a
file?
(A) When you open a file for reading, if the file does not exist, an error occurs.
(B) When you open a file for writing, if the file does not exist, an error occurs.
(C) When you open a file for reading, if the file does not exist, the program will open
an empty file
(D) When you open a file for writing, if the file does not exist, a new file is created.

Ans. : When you open a file for reading, if the file does not exist, the program will
open an empty file

Page 2
13. Which of the following commands can be used to read "n" number of
characters from a file using the file object ?
(A) File.read(n) (B) N = file.read() (C) File.readline(n) (D) File.readlines()
Ans. : File.read(n)
14. Which of the following commands can be used to read the next line in a file
using the file object ?
(A) File.read(n) (B) File.read() (C) File.readline() (D) File.readlines()

Ans. : File.readline()
15. What does the method returns?
(A) Str (B) A list of lines (C) List of single (D) List of integers
characters

Ans. : A list of lines


16. Which of the following command is used to open a file "d:temp.txt" for writing
in binary format only?
(A) fout = (B) fout = (C) fout = open(file (D) fout = open(file
open("d:story.txt", open("d:temp.txt", = "d:temp.txt", = "d:temp.txt",
"wb") "wb") "wb+") "wb+")

Ans. : fout = open("d:temp.txt", "wb")


17. Which of the following functions do you use to write data in the binary format?
(A) Write (B) Output (C) Dump (D) Send

Ans. : Dump
18. Which of the following are the attributes related to a file object?
(A) Closed (B) Mode (C) Filename (D) Rename

Ans. : (a) and (b)


19. What is the correct syntax of open() function?
(A) File = open(file_name[, access_mode][, buffering])
(B) File object = open(file_name [, access_mode][,buffering])
(C) File object = open(file_name)
(D) None of the above

Ans. : File object = open(file_name [, access_mode][,buffering])

* Answer The Following Questions In Short.[1 Marks Each] [20]

20. What will be the output of the following code snippet? If we assume that file
contains the given text:
Str="CBSE Class XII Python Examination 2020"

Page 3
Ans. : Name of the file: myfile.txt

21. Write a statement in Python to open a text file STORY.TXT, so that the new
contents can be added at the end of it.
Ans. : File = open("STORY.TXT","w")

22. Write the difference between the following. (a) f = open('diary.txt', 'a') (b) f =
open('diary.txt', 'w')
Ans. : (a) It will open the file in append mode and data will be added at the end of
the file only. (b) It will open the file in write mode if file already exists then previous
data will be erased.
23. Differentiate the following:(a) f = open('diary.txt', 'r') (b) f = open('diary.txt',
'w')
Ans. : (a) It opens the file in reading mode only in text format.(b) It opens the file
in writing mode only in text format. If the file exists, then it erases the previous
data.
24. Difference between tell() and seek().
Ans. : Tell(). It returns the current position of the file object (Integer value). Seek().
It position the file object at the specified location.
25. Write the syntax to open a file "para.txt" in read mode.
Ans. :

26. Write the codings to display the content of file "welcome.txt".


Ans. :

27. Write the output of the given program:

Page 4
Ans. : Location of the cursor is : 36
28. What is .csv file?
Ans. : Comma Separated Values. It is a simple file format which store into the
tabular format. This format file can be open in notepad or spreadsheet. It works
better as its takes less memory space and faster in data manipulation.
29. Write the difference between “r” and “rb” mode.
Ans. :
It opens the file in reading mode only in text
r
format.
r
It opens the file reading only in binary format.
b

30. How relative path is different from absolute path.


Ans. : The absolute path is the full path from we want to open/create the file. The
relative path is the path to some file with respect to your current working
directory.
31. Nancy intends to position the file pointer to the beginning of a text file. Write
Python statement for the same assuming F is the Fileobject.
Ans. : F.seek(0)
32. Write a statement in Python to open a text file STORY.TXT so that new contents
can be added at the end of it.
Ans. :

33. Write a statement in python to perform the following operations: • To open a


text file “MYPET.TXT” in write mode. • To open a text file “MYPET.TXT” in read
mode.
Ans. :

34. Differentiate between file modes r+ and rb+ with respect to Python.
Ans. : Difference

Page 5
r It opens the file in reading and writing.
rb It opens the file in reading and writing
+ for binary format.

35. Observe the following code and answer the questions that follow:

(i) What type (Text/Binary) of file is Mydata?


(ii) Fill the Blank 1 with statement to write “ABC” in the file “Mydata”.

Ans. : (i) Text File (default mode)


(ii) File.write(“ABC”)

36. Write the output of the given program:

Ans. : 22
37. Assume that file “tear.txt”, already contain “I love my India” in the file. What will
be the output of the given program:

Ans. : 28
38. Assume that file “tear.txt”, already contain “I love my India” in the file. What will
be the output of the given program:

Ans. : 13
39. A text file “Quotes.Txt” has the following data written in it:
Living a life you can be proud of
Doing your best
Spending your time with people and activities that are important to you

Page 6
Standing up for things that are right even when it’s hard
Becoming the best version of you.
Write the output of the given program.

Ans. : ife y

Section D

* Answer The Following Questions In Brief.[5 Marks Each] [30]

40. Mr. Iqbal is a programmer, who has recently joined the company and given a
task to write a python code to perform the following binary file operations with
the help of two user defined functions/modules:
(a)

Ans. : (a) import pickle


(b) stud.dat
(c) file.tell()
(d) file.seek(pos)
(e) pickle.dump(st,file)
(f) (iii) pickle.dump(LST,FILE)

41. Mr. Mathew is a programmer, who has recently joined the company and given
a task to write a python code to perform update operation in binary file
(stud.dat) which contain admno (Admission No) and fee(Fee paid by student).
He has succeeded in writing partial code and has missed out certain statements,
You as an expert of Python have to provide the missing statements.

Page 7
(a)

Ans. : (a) import pickle


(b) stud.dat
(c) file.tell()
(d) file.seek(pos)
(e) pickle.dump(st,file)
(f) (iii) pickle.dump(LST,FILE)

42. You are a student in CBSE School. Teacher has given a task to write a python
code to perform the following binary file operations with the help of two user
defined functions/modules:
(a)

Ans. : (a)Statement 1 : student = []


(b) Statement 2 : stu = pickle.load(f)
(c)Statement 3 : student.append(s)
(d) Statement 4 : wb
(e) Statement 5 : if i[0] == r: (f) Statement 6 : break

43. Dhruv Kumar of class 12 is writing a program to create a CSV file “Teacher.csv”
which will store the teacher information entered by user. He has written the
following code. As a programmer, help him to successfully execute the given
task. Write appropriate statement / function/ code against fill ups.

Page 8
Ans. : (a) Line 1 : import csv
(b) Line 2 with open("LAPS_Teacher12.csv" , "a", newline="") as file:
(c) Line 3 : Write = csv.writer(file)
(d) LST.append([ID1,Name1,Subject])
(e) Write.writerow(LST)

44. Krishna of class 12 is writing a program to read the details of Sports


performance and store in the csv file “games.csv” delimited with a tab
character. As a programmer, help him to achieve the task.

(a)Line 1 : Name the module he should import.


(b)Line 2 : To create an object to enable to write in the csv file.
(c)Line 3 : To store the data in list
(d)Line 4 : To write a record.
(e)Line 5 : To close the file.
Ans. : (a)Line 1 : Import csv
(b) Line 2 : csv.writer
(c)Line 3 : rec=[sport,comp,prize]
(d) Line 4 : wobl.writerow(rec)
(e) Line 5 : f.close()

45. Krishna is confused in Python code given below. Please help him to answer the
following questions.

(a) Which line in the above code check for capital letter?
(b) Which line in the above code read the file “story. txt”?
(c) Which line in the above code does not affect the execution of program?
(d) Which line is the above code coverts capital letter to small letter?
(e) Which line is the above code opens the file in write mode?
(f) Which line is the above code saves the data?
(g) Which line(s) is/are the part of selection statement.
(h) Which line(s) is/are used to close story1.txt?

Ans. :
(a) Line 6 (e) Line 2
(b) Line 4 (f) Line 13
(c) Line 10 (g) Line 6, Line 8 and Line 11
(d) Line 7 (h) Line 15
----- -----

You might also like