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

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

What is the expected behavior of the following code?

It will:

  • A. print 4321
  • B. print <generator object f at (some hex digits)>
  • C. cause a runtime exception
  • D. print 1234
Show Suggested Answer Hide Answer
Suggested Answer: B 🗳️

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
hackadocka
Highly Voted 3 years, 10 months ago
yield keyword expression is I (capital i), while for loop variable is i (small I). Function is erroneous.
upvoted 6 times
Efren
3 years, 7 months ago
It works for me even is i and I, check my code up
upvoted 2 times
...
koyuul
3 years, 7 months ago
the capital I would cause an error if you were to iterate through actual generator obj, for example in [for i in f(2)] there would be a NameError. However, in this code the obj is never ran so no error occurs.
upvoted 4 times
...
...
Efren
Highly Voted 3 years, 7 months ago
>>> def f(n): ... for i in range (1,n+1): ... yield I ... >>> print(f(2)) <generator object f at 0x0000013BA21A82A0> ANswer is correct
upvoted 6 times
...
zantrz
Most Recent 9 months, 2 weeks ago
Selected Answer: B
def f(n): for i in range(1,n+1): yield i print(f(2)) #output: <generator object f at 0x000001C77ED4D000> generator=f(2) print(next(generator)) #output: 1 print(next(generator)) #output: 2 print(next(generator)) #output: StopIteration
upvoted 3 times
...
Oracleist
9 months, 3 weeks ago
yield control the flow of a generator. than if we use I instead of the variable name i, it will return an object that represent a generator.
upvoted 1 times
...
natlal
10 months, 1 week ago
Selected Answer: B
def f(n): for i in range(1,n+1): yield i print(f(2)) #<generator object f at 0x00000220062CB140> for x in f(2): print(x, end='') #12 def f(n): for i in range(1,n+1): yield I for x in f(2): print(x, end=' ') #NameError: name 'I' is not defined
upvoted 1 times
...
Ello2023
1 year, 5 months ago
Yield I is not the same as the variable name i. Which should mean runtime error.
upvoted 3 times
...
mlsc01
1 year, 9 months ago
Selected Answer: B
The NameError is not raised because the generator is not executed at all. It's defined and only its reference is used. If it is executed in a loop or comprehension or by using next() then only the NameError will be raised.
upvoted 2 times
...
CAPTAINKURK
1 year, 10 months ago
yield automatically creates, __iter__() and next() function. assuming it was yeild i. then to print, we would need print(next(f(2)))
upvoted 1 times
...
ivanbicalho
2 years, 1 month ago
Selected Answer: B
This question is SO TRICKY. yield I, or yield X or yield ANYTHING, doesn't matter because in the code the undefined variable "I" is never reached. As the answer below from TheNetworkStudent, the answer is B.
upvoted 2 times
...
PremJaguar
2 years, 4 months ago
Selected Answer: C
answer is c because variable names are case sensitive
upvoted 1 times
...
macxsz
2 years, 6 months ago
Selected Answer: B
B. print <generator object f at (some hex digits)>
upvoted 1 times
...
TheNetworkStudent
2 years, 8 months ago
Selected Answer: B
if you try to loop through the generator, it will error. This won't happen because it's simply printed. Code is erroneous, but won't result in an error if executed in this manner. Answer B is correct.
upvoted 4 times
...
vidts
3 years, 7 months ago
answer is correct
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 ...