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
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
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
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
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)
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.
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
agree, but only in case:
except ArithmeticError had ":" on the end. Without ":" it will return SyntaxError
upvoted 2 times
...
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
it_man_531
Highly Voted 2 years, 9 months agoaferiver
Highly Voted 1 year, 7 months ago46a1b89
Most Recent 1 month agozantrz
9 months, 3 weeks agocodedatakage
11 months, 2 weeks agoUjjal_d
1 year, 3 months agoemanuelcar990
1 year, 3 months agoswatiphadtare
1 year, 6 months agoMallie
1 year, 10 months agoJnanada
2 years, 3 months agostuartz
2 years, 5 months agoahuja
2 years, 10 months agoDTL001
2 years, 11 months agovlobachevsky
3 years, 1 month agomazimir
3 years, 1 month ago