PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Easy

Question 18

Q235 - Error Handling


What is the expected output of the following code?



  • A
  • B
  • C
  • D
Reveal correct answer

Correct answer: D

Explanation

Topic: try except finally

Try it yourself:

Explanation:

The snippet will try to execute print('try') and succeed.

The finally block always gets execute.

(There are similar questions Q127 and Q351.)


Q235 (Please refer to this number, if you want to write me about this question.)

A. The 'finally' block is always executed, whether an exception occurs or not. In this case, since there is no exception caught in the 'except' block, the output will be 'finally' followed by 'except'.

B. The 'finally' block is guaranteed to execute, regardless of whether an exception occurs or not. In this code snippet, the 'try' block will be executed first, printing 'try', followed by the 'finally' block, printing 'finally'. Therefore, the expected output is 'finally' followed by 'try'.

C. In this scenario, the 'except' block will not be triggered as there are no specific exceptions specified to catch. The 'finally' block will always execute after the 'try' block, printing 'finally'. Therefore, the expected output is 'except' followed by 'finally'.

D. The code will first try to execute the 'try' block, which will print 'try'. Then, regardless of whether an exception occurs or not, the 'finally' block will always be executed, printing 'finally'. This is why the expected output is 'try' followed by 'finally'.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need