PCAP Exam QuestionsBrowse all questions from this exam

PCAP Exam - Question 80


What is the expected behavior of the following code?

Show Answer
Correct Answer: C

C

Discussion

13 comments
Sign in to comment
stuartzOption: A
Jun 15, 2022

A if ':' after except ArithmeticError else SyntaxError. 1/0 will raise ZeroDivision error which will be caught by the ArithmeticError at both levels with a m sum of 3.

aferiverOption: B
Apr 12, 2023

B, the question is incorrect. m = 0 def foo(n): global m assert m != 0 try: return 1/n # 1/0 is ZeroDivisionError except ArithmeticError: raise ValueError try: foo(0) except ArithmeticError: m += 2 except: m += 1 # Because is ZeroDivisionError, 0+1= 1 print(m) # Print 1

it_man_531Option: C
Feb 2, 2022

"C" is correct answer. colon is missing in except ArithmeticError hence it will return a SyntaxError

vlobachevskyOption: A
Oct 9, 2021

A is correct

mazimir
Oct 20, 2021

agree, but only in case: except ArithmeticError had ":" on the end. Without ":" it will return SyntaxError

DTL001Option: C
Dec 18, 2021

Answer is C The real exam has : except ArithmeticError: m = 0 def foo(n): global m assert m != 0 try: return 1/n except ArithmeticError: raise ValueError try: foo(0) except ArithmeticError: m +=2 except: m +=1 print(m) # Print 1

codedatakageOption: A
Dec 15, 2023

I have implemented the code, and I get the answer "A". I write here the code to help to test it. m = 0 def foo(n): global m assert m==0 try: return 1/n except ArithmeticError: m+=1 raise try: foo(0) except ArithmeticError: m+=2 except: m+=1 print(m)# print: 3

JnanadaOption: A
Aug 18, 2022

If it wasnt for Type Error, Answer is A.

ahujaOption: A
Jan 18, 2022

Answer is A: 1, it throws AssertionError, hence goes to m+=1

MallieOption: A
Jan 2, 2023

A. Two arithmetic errors. The 'raise' in the example doesn't specify an error so it continues handling a 'ZeroDivisionError'. (This all assumes the second colon is put in of course)

swatiphadtareOption: D
May 5, 2023

If code doesnt have type then answer is D m = 0 def foo(n): global m assert m == 0 try: return 1/n except ArithmeticError: m=m+1 raise ValueError try: foo(0) except ArithmeticError: m +=2 except: m +=1 print(m) # Prints 2

emanuelcar990Option: C
Aug 16, 2023

the awnser its C SyntaxError: 'return' outside function and its 1 /n and the its global m = 0 and not n

Ujjal_dOption: C
Aug 22, 2023

Which is correct?

zantrzOption: A
Jan 31, 2024

The answer is A. m = 0 def foo(n): global m assert m == 0 #raises an AssertionError if m is equal to 0. This is a check to ensure that m is zero. try: return 1/n except ArithmeticError: #there is ZeroDivisionError so m will be 0+1 = 1 m+=1 raise #if we add "raise" we get the exception error try: #the outer try block calls the foo(0) function, which will raise an ArithmeticError (precisily ZeroDivisionError) foo(0) except ArithmeticError: #this block will be executed m +=2 # so we add 1 +2 = 3 except: m +=1 #If any other exception occurs, m is incremented by 1, and the current value of m is printed. In case of foo(0) this block will not be executed. print(m) # Prints 3