Test Series - python

Test Number 99/108

Q: Which is/are the basic I/O connections in file?
A. Standard Input
B. Standard Output
C. Standard Errors
D. All of the mentioned
Solution: Standard input, standard output and standard error. Standard input is the data that goes to the program. The standard input comes from a keyboard. Standard output is where we print our data with the print keyword. Unless redirected, it is the terminal console. The standard error is a stream where programs write their error messages. It is usually the text terminal.
Q: Which of the following mode will refer to binary data?
A. r
B. w
C. +
D. b
Solution: Mode Meaning is as explained below:
r Reading
w Writing
a Appending
b Binary data
+ Updating.
Q: 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
Solution: Pickle is the standard mechanism for object serialization. Pickle uses a simple stack-based virtual machine that records the instructions used to reconstruct the object. This makes pickle vulnerable to security risks by malformed or maliciously constructed data, that may cause the deserializer to import arbitrary modules and instantiate any object.
Q: 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
Solution: We have been working with simple textual data. What if we are working with objects rather than simple text? For such situations, we can use the pickle module. This module serializes Python objects. The Python objects are converted into byte streams and written to text files. This process is called pickling. The inverse operation, reading from a file and reconstructing objects is called deserializing or unpickling.
Q: 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
Solution: Open() function correct syntax with the parameter details as shown below:
file object = open(file_name [, access_mode][, buffering])
Here is parameters’ detail:
file_name: The file_name argument is a string value that contains the name of the file that you want to access.
access_mode: The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r).
buffering: If the buffering value is set to 0, no buffering will take place. If the buffering value is 1, line buffering will be performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action will be performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior).
Q: 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
Solution: The method flush() flushes the internal buffer. Python automatically flushes the files when closing them. But you may want to flush the data before closing any file.
Q: Correct syntax of file.writelines() is?
A. file.writelines(sequence)
B. fileObject.writelines()
C. fileObject.writelines(sequence)
D. none of the mentioned
Solution: The method writelines() writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value.
Syntax
Following is the syntax for writelines() method:
fileObject.writelines( sequence ).
Q: Correct syntax of file.readlines() is?
A. fileObject.readlines( sizehint );
B. fileObject.readlines();
C. fileObject.readlines(sequence)
D. none of the mentioned
Solution: The method readlines() reads until EOF using readline() and returns a list containing the lines. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read.
Syntax
Following is the syntax for readlines() method:
fileObject.readlines( sizehint );
Parameters
sizehint — This is the number of bytes to be read from the file.

You Have Score    /8