100% found this document useful (2 votes)
1K views

File Handling (MCQ'S)

The document contains questions and answers related to file handling in Python. It covers topics like opening, reading, writing and closing files; file modes; file objects; standard streams; relative paths; file methods like seek(), tell(), read(), write() etc.

Uploaded by

Amogh D G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
1K views

File Handling (MCQ'S)

The document contains questions and answers related to file handling in Python. It covers topics like opening, reading, writing and closing files; file modes; file objects; standard streams; relative paths; file methods like seek(), tell(), read(), write() etc.

Uploaded by

Amogh D G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

FILE HANDLING (MCQ’S)

Q.1. A text file stores information in ASCII or Unicode characters as per


1) the compiler
2) the interpreter
3) As per programming platform
4) computer monitor
Q.2. In Python, by default this EOL character is the
1)(\eol)
2)(\n)
3)(el)
4)(\p)
Q.3.In which format Binary file contains information
1) Zebra way
2) Quick response code
3) same format in which the data is held in memory.
4) ASCII Format
Q.4.Is there a delimiter for binary files.
1) Yes
2) No
Q.5.What is easier for a program to read and write.
1) Binary file
2) Text file
3) Doc file
4) Excel file
Q.6. for opening of the file. Which command is used.
1)read()
2)write()
3)open()
4)append()
Q.7.What does this code myfile=open(“taxes.txt”).
1) opens the taxes.txt in default read mode.
2) opens the taxes.txt in default write mode.
3) opens the taxes.txt in default append mode.
4) opens the taxes.txt in default read and write mode.
Q.8. What are file object.
File object are used to read and write data to a file on disk.
1)True
2)False
Q.9. Which is of this is binary file mode?
1)’r’
2)’rb’
3)’w’
4)’a’
Q.10.Which of the following file-modes does retains file data and append new data.
1)’a+’
2)’a’
3)’w+’
4)’r+’
Q.11.File is created if does not exist. If the file exists, file is truncated( past data is
lost). Both reading and writing operation can take place. What is the text file mode?
1)’r’
2)’w’
3)’w+’
4)’a+’
Q. f = open(“data.txt”)
Q.12.Identify name of file.
a.F
b.Open()
c.data.txt
d.Home
Q.13.Identify name of function.
a.File object
b.Open()
c.data.txt
d.Home
Q.14.What if ‘f’ in above code?
a.File object
b.Open()
c.data.txt
d.Home
Q.15.The above statement will __________ file in ________ mode.
a.File object,write
b.Open,Read
c.data.txt,append
d.Home,append plus
Q.16.Reads some bytes from the file and returns it as a string.
a. read()
b.readline()
c.readpara()
d.readlines()
Q.17. Reads a line at a time.
a. read()
b.readline()
c.readpara()
d.readlines()
Q.18. Reads all the lines from the file and returns in the form of a list.
a. read()
b.readline()
c.readpara()
d.readlines()
Q.19. Write a string in file.
a. write()
b. writeline()
c. writelines()
d. writepara()
Q.20. Writes a list in a file.
a. write()
b. writeline()
c. writelines()
d. writepara()
Q.21.The output and input devices are implemented as files.
a.upstream
b.Downstream
c.Standard stream
D.Non-Standard stream
Q.22. There are three standard streams: stdin(standard input), stdout(standard output)
and stderr(standard error).
a.True
b.False
Q.23.The relative paths are relative to the current working directory denoted as a dot(.)
while its parent directory is denoted with __________.
a.dot(.)
b.two dots(..)
c.duck(‘)
d.Double duck(”)

*******************************************************************************************
*******

24. To open a file “c:\scores.txt” for reading, we use _____________


a) infile = open(“c:\scores.txt”, “r”)
b) infile = open(“c:\\scores.txt”, “r”)
c) infile = open(file = “c:\scores.txt”, “r”)
d) infile = open(file = “c:\\scores.txt”, “r”)

25. To open a file “c:\scores.txt” for writing, we use ____________


a) outfile = open(“c:\scores.txt”, “w”)
b) outfile = open(“c:\\scores.txt”, “w”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “w”)
26. To open a file ‘c:\scores.txt’ for appending data, we use ____________
a) outfile = open(“c:\\scores.txt”, “a”)
b) outfile = open(“c:\\scores.txt”, “rw”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “w”)

27. Which of the following statements are true?


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, a new file is created
c) When you open a file for writing, if the file exists, the existing file is overwritten with
the new file
d) All of the mentioned

29. To read two characters from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

30. To read the entire remaining contents of the file as a string from a file object infile, we
use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

31. What will be the output of the following Python code?

1. f = None
2. for i in range (5):
3. with open("data.txt", "w") as f:
4. if i > 2:
5. break
6. print(f.closed)

a) True
b) False
c) None
d) Error

32. To read the next line of the file from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

33. To read the remaining lines of the file from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

34. The readlines() method returns ____________


a) str
b) a list of lines
c) a list of single characters
d) a list of integers

35. Which one of the following is not attributes of file?


a) closed
b) softspace
c) rename
d) mode

36. What is the use of tell() method in python?


a) tells you the current position within the file
b) tells you the end position within the file
c) tells you the file is opened or not
d) none of the mentioned

37. What is the current syntax of rename() a file?


a) rename(current_file_name, new_file_name)
b) rename(new_file_name, current_file_name,)
c) rename(()(current_file_name, new_file_name))
d) none of the mentioned

38. What is the current syntax of remove() a file?


a) remove(file_name)
b) remove(new_file_name, current_file_name,)
c) remove(() , file_name))
d) none of the mentioned

39. What will be the output of the following Python code?


1. fo = open("foo.txt", "rw+")
2. print "Name of the file: ", fo.name
3.
4. # Assuming file has following 5 lines
5. # This is 1st line
6. # This is 2nd line
7. # This is 3rd line
8. # This is 4th line
9. # This is 5th line
10.
11. for index in range(5):
12. line = fo.next()
13. print "Line No %d - %s" % (index, line)
14.
15. # Close opened file
16. fo.close()

a) Compilation Error
b) Syntax Error
c) Displays Output
d) None of the mentioned

40. What is the use of seek() method in files?


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 the mentioned

41. What is the use of the truncate() method in file?


a) truncates the file size
b) deletes the content of the file
c) deletes the file size
d) none of the mentioned

42. Which is/are the basic I/O connections in the file?


a) Standard Input
b) Standard Output
c) Standard Errors
d) All of the mentioned

43. Which of the following mode(s) will refer to binary data?


a) r
b) w
c) +
d) rb

44. What is 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

45. What is unpickling?


a) It is used for object serialization
b) It is used for object deserialization
c) None of the mentioned
d) All of the mentioned

46. What is the correct syntax of open() function?


a) file = open(file_name [, access_mode][, buffering])
b) file object = open(file_name [, access_mode][, buffering])
c) file object = open(file_name)
d) none of the mentioned

47. What will be the output of the following Python code?


1. fo = open("foo.txt", "wb")
2. print("Name of the file: ", fo.name)
3. fo.flush()
4. fo.close()

a) Compilation Error
b) Runtime Error
c) No Output
d) Flushes the file when closing them

24. Correct syntax of file.writelines() is?


a) file.writelines(sequence)
b) fileObject.writelines()
c) fileObject.writelines(sequence)
d) none of the mentioned

25. Correct syntax of file.readlines() is?


a) fileObject.readlines( sizehint );
b) fileObject.readlines();
c) fileObject.readlines(sequence)
d) none of the mentioned

26. Which of the following are the modes of both writing and reading in binary format in
file?
a) wb+
b) w
c) wb
d) w+

27. 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

28. How do you get the current position within the file?
a) fp.seek()
b) fp.tell()
c) fp.loc
d) fp.pos

29. How do you delete a file?


a) del(fp)
b) fp.delete()
c) os.remove(‘file’)
d) os.delete(‘file’)

30. 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

Sanjay is a software developer for KVS. He has written a program to create a


“kvs.txt” file to store the data. Fill the blank with appropriate command / method

f = open('kvs.txt', 'w')
f.write('This is my Record File')

# Create save and close the file


f._______ #Line 1

#Open the file in read from beginning mode


f = open('kvs.txt', '___’) #Line 2

#Now read from the beginning first 5characters


f.seek(___ , ___ ) #Line 3

#Now read from the end last 12 characters


f.seek(___ , ___ ) #Line 4

# reading the entire content of file from current position
rea = f.______ #Line 5

print(rea)

f.close()

i) Write the method to save and close the file ‘kvs.txt’


ii) Write the file open mode to read from beginning
iii) Write the method parameter to read from beginning to 5 characters (offset)
iv) Write the method parameter to read from end to 12 characters (offset)
v) Write the method to read the file from currect position.

You might also like