What is the expected behavior of the following code?

Posted by: Pdfprep Category: PCAP-31-03 Tags: , ,

What is the expected behavior of the following code?

A . it outputs 2
B . the code is erroneous and it will not execute
C . it outputs 1
D . it outputs 3

Answer: D

Comments (2)

  • egv Reply

    Correct answer is C, not D, since the assert condition returns False end then an AssertionError exception is raised just after foo(0) is executed.

    February 17, 2023 at 6:31 pm
    • Aman Sandhu Reply

      Choice “D” is wrong. The correct choice is “C”.

      Explanation : Since m = 0, try foo(0) fails and raises “AssertionError” not “ArithmeticError” which is the last block. Hence, m’s value becomes “m = m +1” that is 0 + 1 = 1.

      October 14, 2024 at 3:42 am

Leave a Reply

Your email address will not be published.