Exam PCAP All QuestionsBrowse all questions from this exam
Question 63

What is the expected behavior of the following code?

It will -

    Correct Answer: B

    B

Discussion
CC_DCOption: B

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

macxszOption: B

if indentation was correct: B. print 1 2

NorasitOption: B

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

BackyOption: A

// 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

rocky48Option: B

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

rocky48

Otherwise it will cause a runtime exception.

ruydrigoOption: D

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

techdawgs

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.