2024 08 06 10 01 Solution
2024 08 06 10 01 Solution
Section A
* Choose The Right Answer From The Given Options.[1 Marks Each] [19]
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. : 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. : Dump
18. Which of the following are the attributes related to a file object?
(A) Closed (B) Mode (C) Filename (D) Rename
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. :
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
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:
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
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)
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)
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)
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)
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
----- -----