[Type A] Chapter– 5 Class 12 CS – Sumita Arora Assignment | Q/A

Here is class 12 computer science Unit 5 [Type A] solutions for Sumita Arora back exercise assignment. Below includes both textual and video solutions wherever required. View all the answers in assignment for chapter 5 and for all chapters here.

Watch all tutorials for chapter 5: Python Revision Tour.

Q1: What is the difference between “w” and “a” modes?

“w” and “a” modes are the writing methods in file.
“w” is method in which the new content overlap the previous content and write the new content in the file.
“a” is method in which the new content is appended in file that is without deleting the previous content.

Q2: What is the significance of file-object?

File-object is very important in python as it provides user the reference to the existing file and also with several access options such as writing, reading and many more. It gives direct option to act on hardware memory.

f1 = open("file_path", 'r') 

f1 is the file_object to access and manipulate the file content.

Q3: How is file open( ) function different from close( ) function?

File open() function opens the access to the existing file and also create new file to user to perform various task on the file content.
For example various operation are ‘w’ (write), ‘a’ (append), ‘r’ (read), ‘x’ (new).

File close() function close the access to the specific file provided that file was open.

file_object = open('file_name', 'mode') #creating access of the 

file_object.close() #closing the access to the file_object
Q4: Write statements to open a binary file C:\Myfiles\Textl.txt in reading and write mode by specifying the file path in two different formats.
f = open(r"C:\Myfiles\Textl.txt", 'r+')
This open the file with path provided and 'r+' indicate the read and write mode access. r prefix indicate the raw string that no special meaning to character.
f = open("C:\\Myfiles\\Textl.txt", 'r+')
In this double slash '\\' suppress special meaning of '\'.

Q5: When a file is opened for output, what happens when –
(i) The mentioned file does not exist?
(ii) The mentioned file does exist?

(i) When the mentioned file does not exist then –
‘r’ in this mode error will generate as file do not found.
‘w’ in this mode a new file will be created with mentioned name.

(ii) when the mentioned file does exist then
‘r’ in this mode no changes is occurred.
‘w’ in this mode the data is either appended or truncated.

Q6: What role is played by file modes in file operations? Describe the various file mode constants and their meanings.

File mode provides the various kind of access to the user after the file is opened, to apply on the data in the file. A number of file operations can be performed on the bases of file mode as mentioned below:

  •  r “, for reading.
  •  w “, for writing.
  •  a “, for appending.
  •  r+ “, for both reading and writing and file must exist.
  • w+“, for both writing and reading if file is not found then creates with the name mentioned.
  • etc.

Q7: What are the advantages of saving data in : (i) binary form (it) text form?

In binary mode the file and its content is stored in the same format as the data is stored in the main memory. Here no encoding and the data conversion is held and addressing of the data is done accordance to the IEEE. It also offers advantage in terms of the speed access to the fetching and loading of the content.

In the text mode the content is stored in the ASCII and Unicode characters and here the end of each line is specified with special character called EOL. In this the transition on the data take place as of conversion and less prone to data get corrupted as of binary mode.

Q8: When do you think text files should be preferred over binary files?

Text file is more preferred over the binary file as the content in text file is more readable by user easily and ported to different type of system. In this mode less chance of data corruption occur.

Q9: Write a statement in Python to perform the following operations:
(a) To open a text file “BOOK.TXT” in read mode.
(b) To open a text file “BOOK.TXT” in write mode.
(a) f = open("BOOK.TXT", 'r')
(b) f = open("BOOK.TXT", 'w')

Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in

You cannot copy content of this page