Q. 1

Is the following code is vaild ?

try:
    1 / 0
except:
    print('exception')
else:
    print('else')
finally:
    print('finally')

 

Options:

Explanation

Correct answer is : A

Yes. This is a valid 

Discuss It

Q. 2

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

Options:

Explanation

Correct answer is : C

try must have one except statement and you can use more than one except statement

Discuss It

Q. 3

When is the finally block statements executed?

Options:

Explanation

Correct answer is : D

finally block is executed everytime irrespective of exception occurs 

Discuss It

Q. 4

What is the output of the following code ? 

try:
    if '2' != 2:
        raise ValueError
    else:
        print('same')
except ValueError:
    print('ValueError')
except NameError:
    print('NameError')

 

Options:

Explanation

Correct answer is : A

The "2" is not same as 2 if block will be executed and it will raise ValueError.

As we kept except ValueError to catch the exception, print('ValueError') will be executed 

 

Discuss It

Q. 5

What is the output of the following code ? 

try:
    print('try')
except ValueError:
    print('ValueError')
finally:
    print('finally')

 

Options:

Explanation

Correct answer is : D

try block will be executed first and it will print "try"

As finally block is always executed, it will print "finally" also

Discuss It

Q. 6

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

Options:

Explanation

Correct answer is : B

When there is no exception occurs in try block , else block is executed.

 

Discuss It

Q. 7

What is the output of the following code ? 

a = 20
b = 30
assert b - a , 'a is smaller than b'
print(b)

 

Options:

Explanation

Correct answer is : A

assert keyword raises AssertionError if the statement after assert becomes false.

b - a results in 10, its value is True. So there will be no AssertionError and print(b) will be executed.

As there is no change in value of b, it will print 30

Discuss It

Q. 8

If there is an error in opening a file, which exception is raised ?

Options:

Explanation

Correct answer is : B

If there is an error in opening a file, which IOError exception is raised.

If file is not available in the location, then FileNotFoundError exception is raised

Discuss It

Q. 9

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

Options:

Explanation

Correct answer is : D

AssignmentError is not a built in standard error in Python

Discuss It

Q. 10

What is the output of the following code ? 

list1 = [4, 5, 6]
print(list1[4])

 

Options:

Explanation

Correct answer is : D

Index value 4 is not available in the list, it will raise IndexError

Discuss It
Submit Your Answers

Interview Questions

on
Errors and Exceptions

Tutorial

on
Errors and Exceptions

Programs

on
Errors and Exceptions
Click any Link
to navigate to certain page easily
Write a line to us
Your Email
Title
Description