WorksheetsException & File Handling
Total questions: 40
Worksheet time: 21mins
The unexpected event happened during program execution should be handled by
EXCEPTION
EXPERIMENT
ASSERTION
EXTRAORDINARY
try:
a=int(input("\n Enter a Value"))
b=int(input("\n Enter a Value "))
c=a/b
print (c)
except:
print("\n Something Went Wrong")
Assume a = 10 and b = 5, then the output will be
5.0
Something Went Wrong
Arithmetic Error
2.0
The suspicious code is put inside the
Try Block
Finally Block
Else Block
Except Block
try:
a=10/5
print (a)
except ArithmeticError:
print ("This statement is raising an exception" )
finally:
print (" final block executed " )
2.0
final block executed
This statement is raising an exception
final block executed
final block executed
2.0
n= int(input("Please enter an integer: "))
if n < 0:
raise Exception (" Only Positive Numbers are Allowed ")
print ("Great, you successfully entered an integer!")
what is the output if the given input is -12
Great, you successfully entered an integer!
Only Positive Numbers are Allowed
Only Positive Numbers are Allowed
Great, you successfully entered an integer!
None of the above Option
When will the else part of try-except-else be executed?
a) always
b) when an exception occurs
c) when no exception occurs
d) when an exception occurs in to except block
Is the following Python code valid?
try:
# Do something
except:
# Do something
else:
# Do something
no, there is no such thing as else
no, else cannot be used with except
no, else must come before except
yes
What happens when ‘1’ == 1 is executed?
we get a True
we get a False
an TypeError occurs
a ValueError occurs
Which of the following is not an exception handling keyword in Python?
accept
finally
try
except
What will be the output of the following Python code?
lst = [1, 2, 3]
lst[3]
a) NameError
b) ValueError
c) IndexError
d) TypeError
What will be the output of the following Python code
print(4 + '3')
TypeError
NameError
IndexError
ValueError
What will be the output of the following Python code?
def getMonth(m):
if m <1 or m>12:
raise ValueError("Invalid")
print(m)
getMonth(6)
ValueError
ValueError(“Invalid”)
Invalid
6
Which of the following is not a standard exception in Python?
NameError
IOError
AssignmentError
ValueError
How many except statements can a try-except statement have?
None
One
Two
Multiple
When is the finally block executed?
when an exception occurs
when no exception occurs
never
always
------------------ and -------------------- statements are used to prevent an exception from terminating a program
try,catch
try,except,
try
try,else
What is the output of the attached python code?
100
0
Zero Division Error Occurred
Another error will occur, program will "crash"
Can one except block handle multiple exceptions?
Yes
No
May be
Insufficient information
Mode in the following program to open file for writing in a mode where new data will be added at the end of old data
file =open("poem.txt","__")
r
a
w
x
Function used to write data in binary format
write()
output()
load()
dump()
How do you change the file position to an offset value from the start?
fp.seek(offset, 0)
fp.seek(offset, 1)
fp.seek(offset, 2)
none of the mentioned
Q.3 Which module is used to work with binary files?
CSV module
pickle module
math module
binary module
What will be the output of the following Python code?
True
False
None
error
An absolute path name begins at the
leaf
stem
root
current directory
The readlines() method returns
str
a list of lines
a list of single characters
a list of integers
4. What will be the ouput -
f=open('abc.txt','w')
x=f.read()
f.close()
print(x)
Error(not readable)
Error(not writable)
It will read all the data of the file.
None
Which of the following statements are true regarding the opening modes of a file?
A. When you open a file for reading, if the file does not exist, an error occurs.
B. When you open a file for writing, if the file does not exist, an error occurs.
C. When you open a file for reading, if the file does not exist, the program will open an empty file.
D. When you open a file for writing, if the file does not exist, a new file is created.
E. When you open a file for writing, if the file exists, the existing file is overwritten with the new file.
Only A
A and B
A, D and E
A, B and E
Which of the following commands can be used to read “n” number of characters from a file using the file object <file>?
file.read(n)
n = file.read()
file.readline(n)
file.readlines()
What will be the output of the following code snippet?
Runtime Error
Hello World how are you today
[‘Hello’, ‘World’, ‘how’, ‘are’, ‘you’, ‘today’]
Hello
What will be the output of the following code snippet?
red
yellow
blue
[‘red\n’, ‘yellow\n’, ‘blue\n’]
Error: I/O operation on closed file
Compilation error
What is the use of tell() method in python?
tells you the current position within the file
tells you the end position within the file
tells you the file is opened or not
none of the mentioned
When reading text files, what are the hidden characters typically at the end of each line?
\n
»
#
@
What is the best way to read an entire file into a single string?
.read()
.readline()
.readlines()
.read(all)
Q1. Given the following file opened as song_file:
I can't write one song that's not about you
Can't drink without thinkin' about you
Is it too late to tell you that
Everything means nothing if I can't have you?
What is the result of
print(song_file.read(4)) ?
I ca
I can
I c
I can't write one song that's not about you
Can't drink without thinkin' about
I c
you
Is it too late to tell you that
Everything means nothing if I can't have you?
Given the following file opened as song_file:
I can't write one song that's not about you
Can't drink without thinkin' about you
Is it too late to tell you that
Everything means nothing if I can't have you?
What is the result of
print(text_file.readlines()[1]) ?
I can't write one song that's not about you
Can't drink without thinkin' about you
I can't write one song that's not about you
Can't drink without thinkin' about
you
Is it too late to tell you that
Everything means nothing if I can't have you?
I
Which of the following statements are true regarding the opening modes of a file?
When you open a file for reading, if the file does not exist, an error occurs.
When you open a file for writing, if the file does not exist, an error occurs.
When you open a file for reading, if the file does not exist, the program will open an empty file.
Which of the following command is used to open a file “c:\temp.txt” in append-mode?
outfile = open(“c:/temp.txt”, “a”)
outfile = open(“c:\\temp.txt”, “rw”)
outfile = open(“c:\temp.txt”, “w+”)
outfile = open(“c:\\temp.txt”, “r+”)
outfile = open(“c:\\temp.txt”, “a”)
EOL stands for
End Of Line
End Of List
End of Lines
End Of Location
What is the use of seek() method in files?
sets the file’s current position at the offset
sets the file’s previous position at the offset
sets the file’s current position within the file
none of the mentioned
which function is used to read records from a binary file?
pickle.load(f)
pickle.read()
pickle.open()
pickle.dump(l,f)
