Output of Python programs | Set 10 (Exception Handling)
Last Updated :
06 Sep, 2024
Pre-requisite:
Exception Handling in PythonNote:
All the programs run on python version 3 and above.
1) What is the output of the following program?
Python
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?
Python
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?
Python
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?
Python
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?
Python
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.
Similar Reads
Output of Python Program | Set 1 Predict the output of following python programs: Program 1:Python r = lambda q: q * 2 s = lambda q: q * 3 x = 2 x = r(x) x = s(x) x = r(x) print (x) Output:24Explanation : In the above program r and s are lambda functions or anonymous functions and q is the argument to both of the functions. In firs
2 min read
Output of python program | Set 2 Difficulty level : IntermediatePredict the output of following Python Programs. Program 1: Python3 class Acc: def __init__(self, id): self.id = id id = 555 acc = Acc(111) print (acc.id) Output: 111 Explanation: Instantiation of the class "Acc" automatically calls the method __init__ and passes the o
2 min read
Output of Python Program | Set 3 Difficulty level : Intermediate Predict the output of following Python Programs. Program 1: Python3 class Geeks: def __init__(self, id): self.id = id manager = Geeks(100) manager.__dict__['life'] = 49 print (manager.life + len(manager.__dict__)) Output:51 Explanation : In the above program we are cr
2 min read
Output of Python Program | Set 4 Difficulty level : Intermediate Predict the output of the following Python Programs. Program 1: Python nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv'] print nameList[1][-1] Output: k Explanation: The index position -1 represents either the last element in a list or the last character in a String. In
2 min read
Output of Python program | Set 5 Predict the output of the following programs: Program 1: Python def gfgFunction(): "Geeksforgeeks is cool website for boosting up technical skills" return 1 print (gfgFunction.__doc__[17:21]) Output:coolExplanation: There is a docstring defined for this method, by putting a string
3 min read
Output of Python program | Set 6 (Lists) Prerequisite - Lists in Python Predict the output of the following Python programs. These question set will make you conversant with List Concepts in Python programming language. Program 1 Python list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0]:
3 min read
Output of Python programs | Set 7 Prerequisite - Strings in Python Predict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language. Program 1Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ", var1[0] # statement 1 print "var2[1:5
3 min read
Output of Python programs | Set 8 Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) Output: 6Explanation: The beauty of python list datatype is that within a list, a programmer can nest another list,
3 min read
Output of Python programs | Set 9 (Dictionary) Prerequisite: Dictionary 1) What is the output of the following program? Python dictionary = {'GFG' : 'geeksforgeeks.org', 'google' : 'google.com', 'facebook' : 'facebook.com' } del dictionary['google']; for key, values in dictionary.items(): print(key) dictionary.clear(); for key, values in diction
3 min read
Output of Python programs | Set 10 (Exception Handling) Pre-requisite: Exception Handling in PythonNote: All the programs run on python version 3 and above. 1) What is the output of the following program?Python data = 50 try: data = data/0 except ZeroDivisionError: print('Cannot divide by 0 ', end = '') else: print('Division successful ', end = '') try:
3 min read