Python Files

Python Files. Multiple Choice Questions on the topic of Files and Functions related to Files in Python Programming.

Python Files Interview Questions: Files in Python language

Python Files Interview Questions. Multiple Choice Questions And Answers on Files in Python programming.

 

Python Files Interview Questions

 

 

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

 

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+

 

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

 

Is it possible to create a text file in python ??

a) Yes
b) No
c) Machine dependent
d) All of the mentioned

 

How do you delete a file ??

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

 

Which function is used to close a file in python ??

a) Close()
b) Stop()
c) End()
d) Closefile()

 

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)

 

Which function is used to write a list of string in a file ??

a) writeline()
b) writelines()
c) writestatement()
d) writefullline()

 

How do you get the current position within the file ??

a) fp.seek()
b) fp.tell()
c) fp.loc
d) fp.pos

 

Which function is used to write all the characters ??

a) write()
b) writecharacters()
c) writeall()
d) writechar()

 

How do you close a file object (fp) ??

a) close(fp)
b) fclose(fp)
c) fp.close()
d) fp.__close__()

 

Which function is used to read single line from file ??

a) Readline()
b) Readlines()
c) Readstatement()
d) Readfullline()

 

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

 

Which function is used to read all the characters ??

a) Read()
b) Readcharacters()
c) Readall()
d) Readchar()

 

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__()

 

What is the use of “a” in file handling ??

a) Read
b) Write
c) Append
d) None of the mentioned

 

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

 

What is the use of “w” in file handling ??

a) Read
b) Write
c) Append
d) None of the mentioned

 

Which of the following is not a valid mode to open a file ??

a) ab
b) rw
c) r+
d) w+

 

In file handling, what does this terms means “r, a” ??

a) read, append
b) append, read
c) write, append
d) none of the mentioned

 

Files In Python: MCQs on Files in Python Language

Files In Python. Multiple Choice Questions on the topic of Files in Python language.

Files In Python

 

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

 

To read two characters from a file object infile we use ??

a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

 

What is the 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

 

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

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

 

Files In Python

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

 

Which of the following mode will refer to binary data ??

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

 

What will be the output ??

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

 

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”)

 

Files In Python

What will be the output of the following ??

import sys
sys.stdout.write(‘ Hello\n’)
sys.stdout.write(‘Python\n’)

a) Compilation Error

b) Runtime Error

c) Hello Python

d) Hello
Python

 

What will be the output of this ??

str = raw_input(“Enter your input: “);
print “Received input is : “, str

a) Enter your input: Hello Python
Received input is : Hello Python

b) Enter your input: Hello Python
Received input is : Hello

c) Enter your input: Hello Python
Received input is : Python

d) None of the mentioned

 

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”)

 

What will be the output of the following Python ??

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) myandroid
b) myandroid, myandroid
c) myId
d) None of the mentioned

 

Files In Python

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

 

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”)

 

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

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

Python Files MCQs: MCQs on Files in Python Language

Python Files MCQs. MCQs on Files in Python Programming language.

Python Files MCQs

 

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

 

The readlines() method returns ??

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

 

Correct syntax of file.readlines() is ??

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

 

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

 

Python Files MCQs

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()

 

Correct syntax of file.writelines() is ??

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

 

What will be the output of the following code ??

fo = open(“foo.txt”, “rw+”)
print “Name of the file: “, fo.name

for index in range(5):
line = fo.next()
print “Line No %d – %s” % (index, line)
fo.close()

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

 

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()

 

Python Files MCQs

What will be the output of the following ??

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

 

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

 

What will be the output ??

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

 

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

 

Python Files MCQs

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

 

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()

 

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

error: Content is protected !!