Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.

Unlimited Access

Get Unlimited Contributor Access to the all ExamTopics Exams!
Take advantage of PDF Files for 1000+ Exams along with community discussions and pass IT Certification Exams Easily.

Exam PCAP topic 1 question 80 discussion

Actual exam question from Python Institute's PCAP
Question #: 80
Topic #: 1
[All PCAP Questions]

What is the expected behavior of the following code?

  • A. it outputs 3
  • B. it outputs 1
  • C. the code is erroneous and it will not execute
  • D. it outputs 2
Show Suggested Answer Hide Answer
Suggested Answer: C 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
stuartz
Highly Voted 1 year, 11 months ago
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.
upvoted 5 times
...
zantrz
Most Recent 3 months, 2 weeks ago
Selected Answer: A
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
upvoted 1 times
...
codedatakage
5 months ago
Selected Answer: A
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
upvoted 3 times
...
Ujjal_d
8 months, 4 weeks ago
Selected Answer: C
Which is correct?
upvoted 1 times
...
emanuelcar990
9 months ago
the awnser its C SyntaxError: 'return' outside function and its 1 /n and the its global m = 0 and not n
upvoted 1 times
...
swatiphadtare
1 year ago
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
upvoted 1 times
...
aferiver
1 year, 1 month ago
Selected Answer: B
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
upvoted 4 times
...
Mallie
1 year, 4 months ago
Selected Answer: A
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)
upvoted 1 times
...
Jnanada
1 year, 9 months ago
If it wasnt for Type Error, Answer is A.
upvoted 2 times
...
it_man_531
2 years, 3 months ago
Selected Answer: C
"C" is correct answer. colon is missing in except ArithmeticError hence it will return a SyntaxError
upvoted 3 times
...
ahuja
2 years, 4 months ago
Answer is A: 1, it throws AssertionError, hence goes to m+=1
upvoted 1 times
...
DTL001
2 years, 5 months ago
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
upvoted 3 times
...
vlobachevsky
2 years, 7 months ago
A is correct
upvoted 3 times
mazimir
2 years, 7 months ago
agree, but only in case: except ArithmeticError had ":" on the end. Without ":" it will return SyntaxError
upvoted 2 times
...
...
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.

SaveCancel
Loading ...