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

Exam PCAP All Questions

View all questions & answers for the PCAP exam

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 (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
clacot
Highly Voted 4 years, 3 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
4 years, 3 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
4 years, 3 months ago
clear now. Thanks!
upvoted 1 times
...
...
...
natlal
Most Recent 10 months, 1 week 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
1 year, 2 months ago
D.prints 3
upvoted 1 times
...
macxsz
2 years, 7 months ago
Selected Answer: D
D. print 3
upvoted 1 times
...
wolverin3
4 years 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 ...