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.
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
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
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.
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.
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.
This section is not available anymore. Please use the main Exam Page.PCAP Exam Questions
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.
hackadocka
Highly Voted 4 years, 3 months agoEfren
4 years agokoyuul
3 years, 12 months agoEfren
Highly Voted 4 years agoDeeksha__Jain
Most Recent 1 month, 3 weeks agozantrz
1 year, 2 months agoOracleist
1 year, 2 months agonatlal
1 year, 3 months agoEllo2023
1 year, 10 months agomlsc01
2 years, 1 month agoCAPTAINKURK
2 years, 2 months agoivanbicalho
2 years, 6 months agoPremJaguar
2 years, 9 months agomacxsz
2 years, 11 months agoTheNetworkStudent
3 years, 1 month agovidts
4 years ago