Output of Python programs | Set 10 (Exception Handling)

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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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 = '')

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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 = '')

chevron_right


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.

This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.