Programming and Problem Solving Through Python Language Theory Question

File Processing - Set 2

  1. 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)
    
    1. Welco
    2. me to
    3. Welcome to
    4. 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'.
  2. Which function is used to read all the characters ?
    1. readall()
    2. read()
    3. readcharacters()
    4. readchar()
    Answer : B
    Explain : read() function is used to read entire data of a file.
  3. Which one is not the attribute of a file ?
    1. softspace
    2. mode
    3. closed
    4. 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
  4. A function used for writing data in binary format ::
    1. write
    2. output
    3. send
    4. 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.
  5. Which of the following is not a correct mode to open a file ?
    1. ab
    2. rw
    3. a+
    4. r+
    Answer : A
    Explain : ab is not any mode in file handling.
  6. which function returns the string ?
    1. readline()
    2. read()
    3. Both of the above
    4. None of above
    Answer : A
    Explain : readline return one line as a string.
  7. What is full form of CSV ?
    1. Common seperation value
    2. Comma seperation values
    3. Comma Seperated Value
    4. None of these
    Answer : C
    Explain :The Full Form Of CSV is Comma Separated Value. CSV (Comma Separated Values) stores tabular data.
  8. What will be the output of the following code ?
    f=open("demo.txt","r")
    print(f.tell())
    1. 1
    2. -1
    3. 0
    4. 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.
  9. Which function returns the strings ?
    1. readline( )
    2. read ( )
    3. readlines()
    4. None of the above
    Answer : C
    Explain : The readline() method read each line and give return as string.
  10. 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)
     
    1. Welco
    2. me to
    3. Welcome to Python
    4. 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.
  11. The syntax of seek() is: file_object.seek(offset [, reference_point])
    What does the reference_point indicate?
    1. reference_point indicates the current position of the file object
    2. reference_point indicates the starting position of the file object
    3. reference_point indicates the ending position of the file object
    4. None of above
    Answer :
    Explain :
  12. The correct syntax of seek() method is :
    1. file_name.()seek
    2. file_name.seek(offset [, reference_point])
    3. seek(offset [file_name, reference_point])
    4. seek(offset [file_name, reference_point])
    Answer : B
    Explain : seek() is used to change and modify the position of cursor in file.
  13. Which one is not the attribute of a file?
    1. softspace
    2. mode
    3. closed
    4. rename
    Answer : D
    Explain : The file object attributes in python : closed , mode, name and softspace.
  14. Which of the function takes two arguments ?
    1. dump( )
    2. load( )
    3. Both of the abov
    4. None of the abov
    Answer : A
    Explain : dump() function requires two arguments.
  15. Which of the following is not a correct mode to open a file?
    1. ab
    2. rw
    3. a+
    4. 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.
  16. If we open a file in write mode and file does not exists, which of the error will generate ?
    1. File Found Error
    2. File Not Exist Error
    3. File Not Found Error
    4. None of these
    Answer : C
    Explain : If file or directory not found then the error raise is : File Not Found Error.

Next Set

1 3