a. Which of the following statements are TRUE? check all that apply: -Before a f
ID: 3709217 • Letter: A
Question
a. Which of the following statements are TRUE? check all that apply:
-Before a file can be processed for reading or writing, it must be open by the programmer.
-After processing a file, the file should be closed
-When a piece of data is written to a file, it is copied from a variable in RAM to the file.
-Sequential file access means that you have to read data from the beginning of the file before reading data stored at the end.
-In Python, the first argument to function open specifies the mode in which the file will be opened.
b. Which of the following statements are TRUE? check all that apply:
-When opening a file for writing using the 'w' mode, the new file gets to be created on the disk.
-When opening a file for writing using the 'a' mode, the new file erases the old file.
-When a file is open for reading, its read position is set to the beginning of the file.
-When a file is open for reading, a special value known as a read position is internally maintained for that file.
-A file read position is updated each time an item is read from that file
Explanation / Answer
Below you will find which statement is true or false.
a. Which of the following statements are TRUE? check all that apply:
-Before a file can be processed for reading or writing, it must be open by the programmer: True. Before you can read, write to a file you must open it. Different languages have different methods to open a file. Like in case of Python you must use open () before you start processing.
-After processing a file, the file should be closed: True. It’s is not necessary in Python but it is considered to be a good practice. Python automatically closes a file when the reference object of a file is reassigned to another file.
-When a piece of data is written to a file, it is copied from a variable in RAM to the file. TRUE. So, whenever the data is written to a file it is copied to RAM
-Sequential file access means that you have to read data from the beginning of the file before reading data stored at the end.: True. Sequential access means data will be accessed in an ordered way. Data in the file doesn’t have to be in order only their access is ordered. sequential files must be read from the beginning, up to the location of the desired data
-In Python, the first argument to function open specifies the mode in which the file will be opened. FALSE. Python’s open method generally takes 2 argument filename and mode. Open () from Python api:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
First arg is mandatory and mode is always 2nd argument not first.
b. Which of the following statements are TRUE? check all that apply:
-When opening a file for writing using the 'w' mode, the new file gets to be created on the disk. True. ‘W’ means opening a file for writing. It either creates a new file or overwrites the existing file. So, if there are already content in the file and you open a same file name with ‘w’ you will loose all the content. Its better to use ‘a’ in that case.
-When opening a file for writing using the 'a' mode, the new file erases the old file. FALSE. If we write with ‘a’ means append mode it will add new data to the end of the file; that is new information is automatically added to the end.
-When a file is open for reading, its read position is set to the beginning of the file. TRUE. This is the default mode. It will display all the text inside the file. If you want only a few characters then enter how many characters you want i.e. read(size). If we want one line at a time we should use readline().
-When a file is open for reading, a special value known as a read position is internally maintained for that file. TURE. It’s a special position that tells the location of next item that will be read from the file. Initially, the read position is set to the beginning of the file.
-A file read position is updated each time an item is read from that file. TRUE. It is updated each time we read from the file
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.