wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Exception & File Handling

Total questions: 40

Worksheet time: 21mins

Name
Class
Date
1.

The unexpected event happened during program execution should be handled by

a)

EXCEPTION

b)

EXPERIMENT

c)

ASSERTION

d)

EXTRAORDINARY

2.

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

a)

5.0

b)

Something Went Wrong

c)

Arithmetic Error

d)

2.0

3.

The suspicious code is put inside the

a)

Try Block

b)

Finally Block

c)

Else Block

d)

Except Block

4.

try:

a=10/5

print (a)

except ArithmeticError:

print ("This statement is raising an exception" )

finally:

print (" final block executed " )

a)

2.0

final block executed

b)

This statement is raising an exception

final block executed

c)

final block executed

d)

2.0

5.

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

a)

Great, you successfully entered an integer!

b)

Only Positive Numbers are Allowed

c)

Only Positive Numbers are Allowed

Great, you successfully entered an integer!

d)

None of the above Option

6.

When will the else part of try-except-else be executed?

a)

a) always

b)

b) when an exception occurs

c)

c) when no exception occurs

d)

d) when an exception occurs in to except block

7.

Is the following Python code valid?


try:

# Do something

except:

# Do something

else:

# Do something

a)

no, there is no such thing as else

b)

no, else cannot be used with except

c)

no, else must come before except

d)

yes

8.

What happens when ‘1’ == 1 is executed?

a)

we get a True

b)

we get a False

c)

an TypeError occurs

d)

a ValueError occurs

9.

Which of the following is not an exception handling keyword in Python?

a)

accept

b)

finally

c)

try

d)

except

10.

What will be the output of the following Python code?


lst = [1, 2, 3]

lst[3]

a)

a) NameError

b)

b) ValueError

c)

c) IndexError

d)

d) TypeError

11.

What will be the output of the following Python code


print(4 + '3')

a)

TypeError

b)

NameError

c)

IndexError

d)

ValueError

12.

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)

a)

ValueError

b)

ValueError(“Invalid”)

c)

Invalid

d)

6

13.

Which of the following is not a standard exception in Python?

a)

NameError

b)

IOError

c)

AssignmentError

d)

ValueError

14.

How many except statements can a try-except statement have?

a)

None

b)

One

c)

Two

d)

Multiple

15.

When is the finally block executed?

a)

when an exception occurs

b)

when no exception occurs

c)

never

d)

always

16.

------------------ and -------------------- statements are used to prevent an exception from terminating a program

a)

try,catch

b)

try,except,

c)

try

d)

try,else

17.

What is the output of the attached python code?

a)

100

b)

0

c)

Zero Division Error Occurred

d)

Another error will occur, program will "crash"

18.

Can one except block handle multiple exceptions?

a)

Yes

b)

No

c)

May be

d)

Insufficient information

19.

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","__")

a)

r

b)

a

c)

w

d)

x

20.

Function used to write data in binary format

a)

write()

b)

output()

c)

load()

d)

dump()

21.

How do you change the file position to an offset value from the start?

a)

fp.seek(offset, 0)

b)

fp.seek(offset, 1)

c)

fp.seek(offset, 2)

d)

none of the mentioned

22.

Q.3 Which module is used to work with binary files?

a)

CSV module

b)

pickle module

c)

math module

d)

binary module

23.

What will be the output of the following Python code?

a)

True

b)

False

c)

None

d)

error

24.

An absolute path name begins at the

a)

leaf

b)

stem

c)

root

d)

current directory

25.

The readlines() method returns

a)

str

b)

a list of lines

c)

a list of single characters

d)

a list of integers

26.

4. What will be the ouput -

f=open('abc.txt','w')

x=f.read()

f.close()

print(x)

a)

Error(not readable)

b)

Error(not writable)

c)

It will read all the data of the file.

d)

None

27.

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.

a)

Only A

b)

A and B

c)

A, D and E

d)

A, B and E

28.

Which of the following commands can be used to read “n” number of characters from a file using the file object <file>?

a)

file.read(n)

b)

n = file.read()

c)

file.readline(n)

d)

file.readlines()

29.

What will be the output of the following code snippet?

a)

Runtime Error

b)

Hello World how are you today

c)

[‘Hello’, ‘World’, ‘how’, ‘are’, ‘you’, ‘today’]

d)

Hello

30.

What will be the output of the following code snippet?

a)

red

yellow

blue

b)

[‘red\n’, ‘yellow\n’, ‘blue\n’]

c)

Error: I/O operation on closed file

d)

Compilation error

31.

What is the use of tell() method in python?

a)

tells you the current position within the file

b)

tells you the end position within the file

c)

tells you the file is opened or not

d)

none of the mentioned

32.

When reading text files, what are the hidden characters typically at the end of each line?

a)

\n

b)

»

c)

#

d)

@

33.

What is the best way to read an entire file into a single string?

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

34.

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)) ?

a)

I ca

b)

I can

I c

I can't write one song that's not about you

Can't drink without thinkin' about

c)

I c

d)

you

Is it too late to tell you that

Everything means nothing if I can't have you?

35.

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]) ?

a)

I can't write one song that's not about you

b)

Can't drink without thinkin' about you

I can't write one song that's not about you

Can't drink without thinkin' about

c)

you

Is it too late to tell you that

Everything means nothing if I can't have you?

d)

I

36.

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.

37.

Which of the following command is used to open a file “c:\temp.txt” in append-mode?

a)

outfile = open(“c:/temp.txt”, “a”)

b)

outfile = open(“c:\\temp.txt”, “rw”)

c)

outfile = open(“c:\temp.txt”, “w+”)

d)

outfile = open(“c:\\temp.txt”, “r+”)

e)

outfile = open(“c:\\temp.txt”, “a”)

38.

EOL stands for

a)

End Of Line

b)

End Of List

c)

End of Lines

d)

End Of Location

39.

What is the use of seek() method in files?

a)

sets the file’s current position at the offset

b)

sets the file’s previous position at the offset

c)

sets the file’s current position within the file

d)

none of the mentioned

40.

which function is used to read records from a binary file?

a)

pickle.load(f)

b)

pickle.read()

c)

pickle.open()

d)

pickle.dump(l,f)