MCQ Fn&file
MCQ Fn&file
MCQ Fn&file
def calc(x,y=20):
3. If the following statement is used to read the contents of a text file object F:
x=F.readlines( )
4. Which of the following command is used to open a file “c:\pat.dat” for writing as well as reading in
binary format only?
f=open(“Rhymes.txt”)
l=f.readlines()
x=[“the”,”ock”]
for i in l:
for w in i.split():
if w in x:
print(w,end=”*”)
(a) the* (b) the*the* (c) Dock*the*clock* (d) error
total={}
def insert(items):
if items in total:
total[items] += 1
else:
total[items] = 1
insert('Apple')
insert('Ball')
insert('Apple')
print (len(total))
A. 0 B. 1 C. 2 D. 3
Ans : C 2
7. To read the entire remaining contents of the file as a string from a file object infile, we use
____________ 1
a. infile.read(2)
b. infile.read()
c. infile.readline()
d. infile.readlines()
Ans : b infile.read()
8. What is the difference between r+ and w+ modes? 1
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
Ans: b in r+ the pointer is initially placed at the beginning of the file and the pointer
is at the end for w+
9. Which of the following is the correct syntax of file object ‘fobj’ to write sequence data type using
writelines() function? (1)
(a) file.writelines(sequence) (b) fobj.writelines()
(c) fobj.writelines(sequence) (d) fobj.writeline()
10. The correct syntax of seek() is:
(a) file_object.seek(offset [, reference_point])
(b) seek(offset [, reference_point])
(c) seek(offset, file_object)
(d) seek.file_object(offset)
Ans: (a) file_object.seek(offset [, reference_point])
11. Which of the following mode in file opening statement results or generates an error if the file
does not exist?
(a) r+ (b) a+ (c) w+ (d) None of the above
Ans: (a) r+
12. Which of the following statements correctly explain the function of tell() method?
(a) tells the current position within the file.
(b) tell the name of file.
(c) move the current file position to a different location.
(d) it changes the file position only if allowed to do so else returns an error
Ans: (a) tells the current position within the file.
13. When the file content is to be retained, we can use the ____________ mode
(a) r (b) w (c) a (d) w+
Ans: (c) a
14. .Identify the output of the following python statements 1
import random
for n in range(2,5,2):
print(random.randrange(1,n,end=’*’)
(a)1*3 (b) 2*3 (c) 1*3*4 (d)1*4
Ans: (a)1*3
15. tell() is a method of:
a) pickle module b) csv module c) file object d) seek()
Ans: . c) file-object
Assertion(A): The file access mode ‘a’ is used to append the data in the file.
Reason(R): In the access mode ‘a’ the text will be appended at the end of the existing file. If the file
does not exist, Python will create a new file and write data into it.
Ans. (a) Both A and R are true and R is the correct explanation for A
Reason(R): When you use keyword arguments in a function call, the caller identifies the arguments
by the values passed.
Ans: (b) Both A and R are true and R is not the correct explanation for A
Assertion - In a function header, any parameter cannot have a default value unless all
Assertion - The with statement simplifies the working with files but does not support
exception handling.
Reason – The with statement will automatically close the file after the nested block ofcode.
Assertion (A): Function can take input values as parameters, execute them and return output (if
required) to the calling function with a return statement. (1)
Note: return multiple values: return a,b,c - return it as tuple . its not revised in the class
Assertion (A): Pickle in Python is primarily used in serializing and deserializing a Python object
structure.
Reason (R): pickle.dump() method is used to write the object in file and pickle.load() method is used
to read the object from pickled file
Reasoning (R):- When a file is opened in write mode the file pointer is present at the beginning
position of the file
Ans: (a) Both A and R are true and R is the correct explanation for A
Note: When we opened in r+ mode ,eventhough file pointer is at the beginning, when we write after
opening it will not overwrite
Assertion(A):-Built in functions are predefined in the language that are used directly.
Ans:(b) Both A and Rare true and R is not the correct explanation for A
Reason(R):CSV files are common file format for transferring and storing data
Ans: (b) Both A and R are true and R is not the correct explanation for A
Explanation: The ability to read , manipulate and write data to and from CSV files using python is a
key skill to master for any data Scientist and Business analysis.
Assertion: The default value of an argument will be used inside a function if we do not pass a value
to that argument at the time of the function call.
Reason: the default arguments are optional during the function call. It overrides the default value if
we provide a value to the default arguments during function calls.
Ans: (a)Both A and R are true and R is the correct explanation for A
x. a=20
def call():
global a
b=20 20
a=a+b 40
return a
print(a)
call()
print(a)
xi. p,q=8, [8]
def sum(r,s=5): 34@53@[13, 24]
p=r+s
q=[r,s]
print(p, q, sep='@')
sum(3,4)
print(p, q, sep='@')