There are 50 question available in this python files quiz. In each question there are four option and the below of that option there is correct answer given.
1. 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”)
Answer: (b)
2. 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”)
Answer: (b)
3. 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”)
Answer: (a)
4. 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
Answer: (d)
5. To read two characters from a file object infile, we use _________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
Answer: (a)
6. 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()
Answer: (b)
7. What will be the output of the following Python code?
- f = None
- for i in range (5):
- with open(“data.txt”, “w”) as f:
- if i > 2:
- break
- print(f.closed)
a) True
b) False
c) None
d) Error
Answer: (a)
8. 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()
Answer: (c)
9. 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()
Answer: (d)
10. The readlines() method returns ____________
a) str
b) a list of lines
c) a list of single characters
d) a list of integers
Answer: (b)
11. Which are the two built-in functions to read a line of text from standard input, which by default comes from the keyboard?
a) Raw_input & Input
b) Input & Scan
c) Scan & Scanner
d) Scanner
Answer: (a)
12. What will be the output of the following Python code?
- str = raw_input(“Enter your input: “);
- print “Received input is : “, str
a)Enter your input: Hello PythonReceived input is : Hello Python
b)Enter your input: Hello PythonReceived input is : Hello
c)Enter your input: Hello PythonReceived input is : Python
d) None of the mentioned
Answer: (a)
13. What will be the output of the following Python code?
- str = input(“Enter your input: “);
- print “Received input is : “, str
a) Enter your input: [x*5 for x in range(2,10,2)]Received input is : [x*5 for x in range(2,10,2)]
b)Enter your input: [x*5 for x in range(2,10,2)]Received input is : [10, 30, 20, 40]
c)Enter your input: [x*5 for x in range(2,10,2)]Received input is : [10, 10, 30, 40]
d) None of the mentioned
Answer: (a)
14. Which one of the following is not the attributes of the file?
a) closed
b) soft space
c) rename
d) mode
Answer: (c)
15. 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
Answer: (a)
16. 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
Answer: (a)
17. 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
Answer: (a)
18. What will be the output of the following Python code?
- fo = open(“foo.txt”, “rw+”)
- print “Name of the file: “, fo.name
- # Assuming file has following 5 lines
- # This is 1st line
- # This is 2nd line
- # This is 3rd line
- # This is 4th line
- # This is 5th line
- for index in range(5):
- line = fo.next()
- print “Line No %d – %s” % (index, line)
- # Close opened file
- fo.close()
a) Compilation Error
b) Syntax Error
c) Displays Output
d) None of the mentioned
Answer: (c)
19. 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
Answer: (a)
20. What is the use of 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
Answer: (a)
21. 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
Answer: (d)
22. What will be the output of the following Python code? (If entered name is sanfoundry)
- import sys
- print ‘Enter your name: ‘,
- name = ”
- while True:
- c = sys.stdin.read(1)
- if c == ‘\n‘:
- break
- name = name + c
- print ‘Your name is:’, name
a) sanfoundry
b) sanfoundry, sanfoundry
c) San
d) None of the mentioned
Answer: (a)
23. What will be the output of the following Python code?
- import sys
- sys.stdout.write(‘ Hello\n‘)
- sys.stdout.write(‘Python\n‘)
a) Compilation Error
b) Runtime Error
c) Hello Python
d) Hello
Python
Answer: (d)
24. Which of the following mode will refer to binary data?
a) r
b) w
c) +
d) b
Answer: (d)
25. 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
Answer: (a)
26. 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
Answer: (b)
27. 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
Answer: (b)
28. What will be the output of the following Python code?
- fo = open(“foo.txt”, “wb”)
- print “Name of the file: “, fo.name
- fo.flush()
- fo.close()
a) Compilation Error
b) Runtime Error
c) No Output
d) Flushes the file when closing them
Answer: (d)
29. Correct syntax of file.writelines() is?
a) file.writelines(sequence)
b) fileObject.writelines()
c) fileObject.writelines(sequence)
d) none of the mentioned
Answer: (c)
30. Correct syntax of file.readlines() is?
a) fileObject.readlines( sizehint );
b) fileObject.readlines();
c) fileObject.readlines(sequence)
d) none of the mentioned
Answer: (a)
31. In file handling, what does this terms means “r, a”?
a) read, append
b) append, read
c) write, append
d) none of the mentioned
Answer: (a)
32. What is the use of “w” in file handling?
a) Read
b) Write
c) Append
d) None of the mentioned
Answer: (b)
33. What is the use of “a” in file handling?
a) Read
b) Write
c) Append
d) None of the mentioned
Answer: (c)
34. Which function is used to read all the characters?
a) Read()
b) Readcharacters()
c) Readall()
d) Readchar()
Answer: (a)
35. Which function is used to read single line from file?
a) Readline()
b) Readlines()
c) Readstatement()
d) Readfullline()
Answer: (b)
36. Which function is used to write all the characters?
a) write()
b) writecharacters()
c) writeall()
d) writechar()
Answer: (a)
37. Which function is used to write a list of string in a file?
a) writeline()
b) writelines()
c) writestatement()
d) writefullline()
Answer: (a)
38. Which function is used to close a file in python?
a) Close()
b) Stop()
c) End()
d) Closefile()
Answer: (a)
39. Is it possible to create a text file in python?
a) Yes
b) No
c) Machine dependent
d) All of the mentioned
Answer: (a)
40. 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+
Answer: (a)
41. Which of the following is not a valid mode to open a file?
a) ab
b) rw
c) r+
d) w+
Answer: (b)
42. 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
Answer: (b)
43. 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__()
Answer: (a)
44. 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
Answer: (d)
45. How do you close a file object (fp)?
a) close(fp)
b) fclose(fp)
c) fp.close()
d) fp.__close__()
Answer: (c)
46. How do you get the current position within the file?
a) fp.seek()
b) fp.tell()
c) fp.loc
d) fp.pos
Answer: (b)
47. 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)
Answer: (b)
48. How do you delete a file?
a) del(fp)
b) fp.delete()
c) os.remove(‘file’)
d) os.delete(‘file’)
Answer: (c)
49. 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
Answer: (a)
50. 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
Answer: (d)