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 28 discussion

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

What is the expected behavior of the following snippet?

It will:

  • A. cause a runtime exception on line 02
  • B. cause a runtime exception on line 01
  • C. cause a runtime exception on line 03
  • D. print 3
Show Suggested Answer Hide Answer
Suggested Answer: D 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
clacot
Highly Voted 3 years, 8 months ago
I know we shouldn't use function name same as variable name. But if I run the code the result is 3. Why?
upvoted 5 times
lasagne394
3 years, 8 months ago
Because during an assignment, the RHS is evaluated first and the result is assignment to the given identifier. In this case, the `x` on the RHS was a function and the result (1 + x() = 1 + 2) was REASSIGNED/BOUND to the same identifier. Post reassignment, x is bound to a int literal, while before it was bound to a function. I hope this snippet will help: https://prnt.sc/u1u8ki
upvoted 12 times
clacot
3 years, 8 months ago
clear now. Thanks!
upvoted 1 times
...
...
...
natlal
Most Recent 3 months ago
Selected Answer: D
def x(): #x_function return 2 x=1+x() #x_value=use x_function print(x) #-->3 def x(): #x_function return 2 x=10 #x_value=int x=1+x() #x_value=use not exist x_function print(x) #-->TypeError: 'int' object is not callable
upvoted 1 times
...
34_trt
6 months, 4 weeks ago
D.prints 3
upvoted 1 times
...
macxsz
1 year, 11 months ago
Selected Answer: D
D. print 3
upvoted 1 times
...
wolverin3
3 years, 5 months ago
def x(i): print(i) return i x=x(1)+x(2) Expression is evaluated from left to right, function return takes more precedence then followed by operator and finally value is assigned which is of least precedence. Above example would illustrate it.
upvoted 1 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 ...