fIle handlIng In pyThon – important questions for Boards 2027
CBSE Class 12 Computer Science
What does the following statement return and assign to f? f = open("data.txt", "r") (A) A file object (file handle) (B) The file’s contents as a string (C) The file’s name as a list (D) A Boolean indicating whether the file exists
Answer
(A) A file object (file handle) — open() returns a file object called a file handle, which is assigned to f.
In the shown output of Program 2-4, the two lines of stored text appear joined together. What change to the displayed output would make each line appear on a separate line?
Answer
Print each stored line separately, ensuring a newline follows the first line, so the two strings appear on different lines. A newline character can be written between data lines or included in the displayed output.
How are text-file characters stored internally, and how does a text editor make them readable to a user? (2 marks)
Answer
Text-file contents are stored as bytes consisting of 0s and 1s. Each character’s value is stored in ASCII, UNICODE or another encoding scheme. A text editor translates each ASCII value into its equivalent readable character, for example, ASCII value 65 is displayed as ‘A’.
A program must write data to a text file and then read it using the same file object. State the mode in which the file should be opened and explain why this mode is suitable.
Answer
1. Open the text file in w+ mode, which permits both reading and writing through the same file object. This mode suits a program that must perform both operations using one object. 2. After writing, use seek(0) to position the file object at the beginning, then read the stored data with that object. The example uses read() to obtain the file contents.
Which option correctly describes text files? (4 marks) (A) They contain only alphabetic characters, and .docx files are a common example. (B) They contain characters such as letters, numbers and special symbols; .txt, .py and .csv are examples. (C) They contain only binary audio and video data. (D) They cannot contain numbers or special symbols.
Answer
(B) They contain characters such as letters, numbers and special symbols; .txt, .py and .csv are examples. — A text file is a sequence of characters consisting of alphabets, numbers and other special symbols.
Explain how the bytes in a binary file represent content, and give three examples of the kinds of content stored in binary files. (5 marks)
Answer locked. Unlock all answers on SmartKalike
Which statement about file access modes is correct? (A) The default mode is append. (B) The access mode determines the operation, and the default mode is read. (C) The default mode is write and read. (D) Access modes only specify the file’s name.
Answer locked. Unlock all answers on SmartKalike
When an existing file is opened in append mode (`a`), where is new data written relative to the existing data?
Answer locked. Unlock all answers on SmartKalike
After a program has finished reading data from a file, state what it should do with the file and explain why this step is important.
Answer locked. Unlock all answers on SmartKalike
Which statement about line endings in a text file is correct? (4 marks) (A) Every line must end with a comma; Python’s default is a tab. (B) Each line ends with an End of Line character; Python’s default EOL is the newline character (\n). (C) Lines have no ending character, and Python uses a space as its default EOL. (D) Each line ends with the character 0; Python’s default EOL is a full stop.
Answer locked. Unlock all answers on SmartKalike
77 more probable questions in this chapter, every one with a full answer.
Unlock all answers on SmartKalike