PCAP Exam QuestionsBrowse all questions from this exam

PCAP Exam - Question 63


What is the expected behavior of the following code?

It will -

Show Answer
Correct Answer: B

B

Discussion

6 comments
Sign in to comment
BackyOption: A
Feb 9, 2022

// This is equivalent to for i in range(1,3): print(i, end=' ') // Any function that includes "yield" is called a generator and it works like range(), so that you can use them both in a loop like "for", but there are differences, e.g. you can use a generator inside next() but you cannot use range() inside next() because range() is stateless

NorasitOption: B
Apr 24, 2022

There is no correct answer because of IndentationError. Assume this code is right indent the answer will be B. print 1 2

macxszOption: B
May 3, 2022

if indentation was correct: B. print 1 2

CC_DCOption: B
Jul 12, 2023

Technically all choices are wrong since the code will cause an IndentationError just like some other question here but B if lined up correctly.

ruydrigoOption: D
Dec 3, 2021

I run it but I don't understand why yield return 1,2

techdawgs
Dec 4, 2021

With f(2), n = 2, so the range will be range(1, {2+1}) or range(1,3). The iterations of i would then be 1 and then 2 with yield sending each iteration back to "for i in f(2)". Hope this explains it.

rocky48Option: B
Mar 15, 2022

If the indentation of Line 2 & 3 is correct, then 1, 2 is the correct answer.

rocky48
Mar 15, 2022

Otherwise it will cause a runtime exception.