Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

  • Difficulty level: Easy

Note: All programs run on Python version 3 and later.

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('HY', end = ' ')
Copy the code

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

A: (a) Note: The else code block is executed only if the try block does not have an exception.

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('Haiyong ', end = ' ')
else:
	print('Division successful ', end = ' ')
Copy the code

a) Runtime error

b) Cannot divide by 0 Haiyong

c) Haiyong Division successful

d) Haiyong

A: (a) Explanation: Python does not allow else blocks after finally blocks. When using this format, Python throws syntax errors.

3) What is the output of the following program?

value = [1.2.3.4]
data = 0
try:
	data = value[4]
except IndexError:
	print('HY', end = ' ')
except:
	print('Haiyong ', end = ' ')
Copy the code

A) Haiyong B) HY C) HY Haiyong D) Error

Answer: (b) Explanation: Catch only one exception at a time, even though the throw in the try block is likely to belong to more than one 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('HY IndexError ', end = ' ')
except:
	print('Haiyong IndexError ', end = ' ')
finally:
	print('Hai IndexError ', end = ' ')

data = 10
try:
	data = data/0
except ZeroDivisionError:
	print('HY ZeroDivisionError ', end = ' ')
finally:
	print('Hai ZeroDivisionError ')
Copy the code

a) HY ZeroDivisionError HY ZeroDivisionError

b) HY ZeroDivisionError Hai ZeroDivisionError

c) Hai IndexError HY ZeroDivisionError Hai ZeroDivisionError

d) Hai IndexError HY ZeroDivisionError

A: (c) Explanation: The finally code block is always executed whether or not an exception occurs. If an exception occurs, an except block is executed, followed by a 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('Haiyong ', end = ' ')
else:
	print('HY', end = ' ')
finally:
	print('Hai ', end = ' ')
Copy the code

a) Compilation error

b) Runtime error

c) Haiyong HY Hai

d) Haiyong Hai

Python defines an else block between finally blocks between tries. If there are no exceptions in the try block, else is executed, followed by the finally block. An EXCEPT block can be defined to catch multiple exceptions.

If you find anything wrong, let me know in the comments section below, learn from each other and make progress together!