Programming and Problem Solving Through Python Language Theory Question
File Processing - Set 2
What will be the output of following code ?
f=open("demo.txt","w+")
f.write("Welcome to Python")
f.seek(5)
a=f.read(5)
print(a)
Welco
me to
Welcome to
e to
Answer : B
Explain :f.seek(5) it change the position of cursor to 5. After that read 5 character and print that character which is 'me to'.
Which function is used to read all the characters ?
readall()
read()
readcharacters()
readchar()
Answer : B
Explain : read() function is used to read entire data of a file.
Which one is not the attribute of a file ?
softspace
mode
closed
rename
Answer : D
Explain : mode tells in which mode file is open, closed return true if file is closed, softspace attribute was a feature related to printing. It was used to determine whether a space character should be inserted before the printed value
A function used for writing data in binary format ::
write
output
send
dump
Answer : D
Explain : For binary file we use dump() and load(). dump() is use for write and load() is used for display or read data in binary.
Which of the following is not a correct mode to open a file ?
ab
rw
a+
r+
Answer : A
Explain : ab is not any mode in file handling.
which function returns the string ?
readline()
read()
Both of the above
None of above
Answer : A
Explain : readline return one line as a string.
What is full form of CSV ?
Common seperation value
Comma seperation values
Comma Seperated Value
None of these
Answer : C
Explain :The Full Form Of CSV is Comma Separated Value. CSV (Comma Separated Values) stores tabular data.
What will be the output of the following code ? f=open("demo.txt","r") print(f.tell())
1
-1
0
2
Answer : C
Explain : tell() method returns current position of file object. So the file open in read mode and the cursor is at beginning where it value is 0.
Which function returns the strings ?
readline( )
read ( )
readlines()
None of the above
Answer : C
Explain : The readline() method read each line and give return as string.
What will be the output of the following code ?
f=open("demo.txt","w+")
f.write("Welcome to Python")
f.seek(5)
a=f.read(5)
print(a)
Welco
me to
Welcome to Python
e to
Answer : B
Explain : w+ means- To write and read data. It write data in file 'Welcome to Python'. Then seek(5) means the cursor moves to 5 letter. Now a=f.read(5) - read 5 letter which is 'me to' and at last print this.
The syntax of seek() is: file_object.seek(offset [, reference_point]) What does the reference_point indicate?
reference_point indicates the current position of the file object
reference_point indicates the starting position of the file object
reference_point indicates the ending position of the file object
None of above
Answer :
Explain :
The correct syntax of seek() method is :
file_name.()seek
file_name.seek(offset [, reference_point])
seek(offset [file_name, reference_point])
seek(offset [file_name, reference_point])
Answer : B
Explain : seek() is used to change and modify the position of cursor in file.
Which one is not the attribute of a file?
softspace
mode
closed
rename
Answer : D
Explain : The file object attributes in python : closed , mode, name and softspace.
Which of the function takes two arguments ?
dump( )
load( )
Both of the abov
None of the abov
Answer : A
Explain : dump() function requires two arguments.
Which of the following is not a correct mode to open a file?
ab
rw
a+
r+
Answer : B
Explain : ab - add data in binary file, a+ - append and read data from the file and r+ - open the file in reading and writing the pointer at the begin.
If we open a file in write mode and file does not exists, which of the error will generate ?
File Found Error
File Not Exist Error
File Not Found Error
None of these
Answer : C
Explain : If file or directory not found then the error raise is : File Not Found Error.