Test Series - python

Test Number 101/108

Q: Which of the following is not a valid mode to open a file?
A. ab
B. rw
C. r+
D. w+
Solution: Use r+, w+ or a+ to perform both read and write operations using a single file object.
Q: What is the difference between r+ and w+ modes?
A. no difference
B. in r+ the pointer is initially placed at the beginning of the file and the pointer is at the end for w+
C. in w+ the pointer is initially placed at the beginning of the file and the pointer is at the end for r+
D. depends on the operating system
Solution: none.
Q: How do you get the name of a file from a file object (fp)?
A. fp.name
B. fp.file(name)
C. self.__name__(fp)
D. fp.__name__()
Solution: name is an attribute of the file object.
Q: Which of the following is not a valid attribute of a file object (fp)?
A. fp.name
B. fp.closed
C. fp.mode
D. fp.size
Solution: fp.size has not been implemented.
Q: How do you close a file object (fp)?
A. close(fp)
B. fclose(fp)
C. fp.close()
D. fp.__close__()
Solution: close() is a method of the file object.
Q: How do you get the current position within the file?
A. fp.seek()
B. fp.tell()
C. fp.loc
D. fp.pos
Solution: It gives the current position as an offset from the start of file.
Q: How do you rename a file?
A. fp.name = ‘new_name.txt’
B. os.rename(existing_name, new_name)
C. os.rename(fp, new_name)
D. os.set_name(existing_name, new_name)
Solution: os.rename() is used to rename files.
Q: How do you delete a file?
A. del(fp)
B. fp.delete()
C. os.remove(‘file’)
D. os.delete(‘file’)
Solution: os.remove() is used to delete files.
Q: How do you change the file position to an offset value from the start?
A. fp.seek(offset, 0)
B. fp.seek(offset, 1)
C. fp.seek(offset, 2)
D. none of the mentioned
Solution: 0 indicates that the offset is with respect to the start.
Q: What happens if no arguments are passed to the seek function?
A. file position is set to the start of file
B. file position is set to the end of file
C. file position remains unchanged
D. error
Solution: seek() takes at least one argument.

You Have Score    /10