Open In App

Output of Python programs | Set 10 (Exception Handling)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Pre-requisite: Exception Handling in Python
Note: All the programs run on python version 3 and above.

1) What is the output of the following program?




data = 50
try:
    data = data/0
except ZeroDivisionError:
    print('Cannot divide by 0 ', end = '')
else:
    print('Division successful ', end = '')
  
try:
    data = data/5
except:
    print('Inside except block ', end = '')
else:
    print('GFG', end = '')


a) Cannot divide by 0 GFG
b) Cannot divide by 0
c) Cannot divide by 0 Inside except block GFG
d) Cannot divide by 0 Inside except block

Ans. (a)
Explanation: The else block of code is executed only when there occurs no exception in try block.

2) What is the output of the following program?




data = 50
try:
    data = data/10
except ZeroDivisionError:
    print('Cannot divide by 0 ', end = '')
finally:
    print('GeeksforGeeks ', end = '')
else:
    print('Division successful ', end = '')


a) Runtime error
b) Cannot divide by 0 GeeksforGeeks
c) GeeksforGeeks Division successful
d) GeeksforGeeks

Ans. (a)
Explanation: else block following a finally block is not allowed in python. Python throws syntax error when such format is used.

3) What is the output of the following program?




value = [1, 2, 3, 4]
data = 0
try:
    data = value[4]
except IndexError:
    print('GFG', end = '')
except:
    print('GeeksforGeeks ', end = '')


a) GeeksforGeeks
b) GFG
c) GFG GeeksforGeeks
d) Compilation error

Ans. (b)
Explanation: At a time only one exception is caught, even though the throw exception in the try block is likely to belong to multiple exception type.

4) What is the output of the following program?




value = [1, 2, 3, 4]
data = 0
try:
    data = value[3]
except IndexError:
    print('GFG IndexError  ', end = '')
except:
    print('GeeksforGeeks IndexError  ', end = '')
finally:
    print('Geeks IndexError  ', end = '')
  
data = 10
try:
    data = data/0
except ZeroDivisionError:
    print('GFG ZeroDivisionError  ', end = '')
finally:
    print('Geeks ZeroDivisionError  ')


a) GFG ZeroDivisionError GFG ZeroDivisionError
b) GFG ZeroDivisionError Geeks ZeroDivisionError
c) Geeks IndexError GFG ZeroDivisionError Geeks ZeroDivisionError
d) Geeks IndexError GFG ZeroDivisionError

Ans. (c)
Explanation: finally block of code is always executed whether the exception occurs or not. If exception occurs, the except block is executed first followed by finally block.

5) What is the output of the following program?




value = [1, 2, 3, 4, 5]
try:
    value = value[5]/0
except (IndexError, ZeroDivisionError):
    print('GeeksforGeeks ', end = '')
else:
    print('GFG ', end = '')
finally:
    print('Geeks ', end = '')


a) Compilation error
b) Runtime error
c) GeeksforGeeks GFG Geeks
d) GeeksforGeeks Geeks

Ans. (d)
Explanation: An else block between finally block between try is defined in python. If there is no exception in try block then else is executed and then the finally block. An except block can be defined to catch multiple exception.



Last Updated : 10 Feb, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads