PT-1 Assignment (CS) AK
PT-1 Assignment (CS) AK
2. Given a function that does not return any value, What value is thrown by default when
executed in shell.
a) int b) bool c) void d) None
3. Following set of commands are executed in shell, what will be the output?
>>>str="hello"
>>>str[:2]
a) he b) lo c) olleh d) hello
6. In python we do not specify types,it is directly interpreted by the compiler, so consider the
following operation to be performed.
>>>x = 13 ? 2
objective is to make sure x has a integer value, select all that apply (python 3.xx)
a) x = 13 // 2 b) x = int(13 / 2) c) x = 13 % 2 d) All of the mentioned
1|Page
11. What is the output of the following?
i=1
while True:
if i%3 == 0:
break
print(i)
i + = 1 # Space not allowed i + = 1
a) 1 2 b) 1 2 3 c) error d) none of the mentioned
2|Page
18. What is the output of the below program?
def C2F(c):
return c * 9/5 + 32
print C2F(100)
print C2F(0)
a) 212 32 b) 314 24 c) 567 98 d) None of the mentioned
26. 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()
3|Page
27. In which mode the file must exist already, otherwise python raises an error?
a. read mode b. write mode c. binary mode d. None of these
29. In which mode______ if the file does not exist, then the file is created?
a. read write mode b. read mode c. write mode d. All of these
34. In which format does the readlines( ) function give the output?
a. Integer type b. list type c. string type d. tuple type
35. In which format does the read( ) function give the output?
a. Integer type b. string type c. list type d. tuple type
4|Page
39.In file handling, what does means “r”, “a”?
a. append, read b. read, append c. read, add d. None of the mentioned
42. A file maintains a __________ which tells the current position in the file where writing or
reading will take place.
a. line b. file pointer c. list d. order
43. Which of the following statements is true regarding the opening modes of a file?
a. While opening a file for reading, if the file does not exist, an error occurs.
b. While opening a file for writing ,if the file does not exist, an error occurs.
c. While opening a file for reading, if the file does not exist, a new file is created.
d. None of the above.
44.To force python to write the contents of file buffer on to storage file,........method may be
used.
a. buffer() b. flush() c. close() d. write()
46. Which of the following functions do you use to write data in the binary format?
A. write B. output C. dump D. send
47. Which of the following file mode will refer to the BINARY mode?
A. binary B. b C. bin D. w
48. The process of converting the structure to a byte stream before writing to the file is known
as ______.
a. Pickling b. Unpickling c. Dump d. load
49. The _____ file mode is used when user want to write data into binary file.
a. rb b. wb c. r+ d. w+
5|Page
50. Which one of the following is correct statement?
a) import – pickle b) pickle import c) import pickle d) All of the above
51. Ms. Sejal is working on the sports.dat file but she is confused about how to read data from
the binary file. Suggest a suitable line for her to fulfill her wish.
53. In which file, no delimiters are used for line and no translations occur?
a. Text file b. Binary file c. csv file d. None of the above
6|Page
54. Priti of class 12 is writing a program to create a CSV file “emp.csv”. She has written the
following code to read the content of file emp.csv and display the employee record whose
name begins from “S‟ also show no. of employee with first letter “S‟ out of total record. As a
programmer, help her to successfully execute the given task. Consider the following CSV file
(emp.csv):
1,Peter,3500
2,Scott,4000
3,Harry,5000
4,Michael,2500
5,Sam,4200
7|Page
55. Given a binary file “emp.dat” has structure (Emp_id, Emp_name, Emp_Salary). Write a
function in Python countsal() in Python that would read contents of the file “emp.dat” and
display the details of those employee whose salary is greater than 20000.
def Search_record():
file = open('Stu.dat', 'rb' )
SRoll=input("Enter Roll Number for search: ")
try:
while True:
d=pickle.load(file)
8|Page
if d[0]==SRoll:
print(d)
except EOFError:
print("Done")
file.close()
9|Page