PCAP Exam QuestionsBrowse all questions from this exam

PCAP Exam - Question 29


What is the expected behavior of the following code?

It will:

Show Answer
Correct Answer: B

B

Discussion

14 comments
Sign in to comment
hackadockaOption: C
Jan 8, 2021

yield keyword expression is I (capital i), while for loop variable is i (small I). Function is erroneous.

Efren
Apr 19, 2021

It works for me even is i and I, check my code up

koyuul
Apr 22, 2021

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.

EfrenOption: B
Apr 17, 2021

>>> def f(n): ... for i in range (1,n+1): ... yield I ... >>> print(f(2)) <generator object f at 0x0000013BA21A82A0> ANswer is correct

TheNetworkStudentOption: B
Mar 5, 2022

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.

Ello2023Option: C
Jun 14, 2023

Yield I is not the same as the variable name i. Which should mean runtime error.

zantrzOption: B
Feb 13, 2024

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

vidts
Apr 8, 2021

answer is correct

ivanbicalhoOption: B
Oct 9, 2022

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.

mlsc01Option: B
Feb 27, 2023

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.

macxszOption: B
May 3, 2022

B. print <generator object f at (some hex digits)>

PremJaguarOption: C
Jul 19, 2022

answer is c because variable names are case sensitive

CAPTAINKURKOption: B
Feb 2, 2023

yield automatically creates, __iter__() and next() function. assuming it was yeild i. then to print, we would need print(next(f(2)))

natlalOption: B
Jan 21, 2024

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

OracleistOption: B
Feb 6, 2024

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.

G3n
Jun 18, 2024

The answers in the dumps are good to remember! The bulk of the exam questions are from these exam dumps. The questions with mistakes in them or missing : etc are in the exam but in the correct answer form, do remember the correct answers as well as the answers with the mistakes in the coding. There are a few questions that are slightly different but you can find the answers from the previous or next question/answers! Hope this helps!