Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.

Unlimited Access

Get Unlimited Contributor Access to the all ExamTopics Exams!
Take advantage of PDF Files for 1000+ Exams along with community discussions and pass IT Certification Exams Easily.

Exam PCAP topic 1 question 63 discussion

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

What is the expected behavior of the following code?


It will -

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

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
CC_DC
8 months, 2 weeks ago
Technically all choices are wrong since the code will cause an IndentationError just like some other question here but B if lined up correctly.
upvoted 2 times
...
macxsz
1 year, 11 months ago
Selected Answer: B
if indentation was correct: B. print 1 2
upvoted 2 times
...
Norasit
1 year, 11 months ago
There is no correct answer because of IndentationError. Assume this code is right indent the answer will be B. print 1 2
upvoted 2 times
...
rocky48
2 years ago
If the indentation of Line 2 & 3 is correct, then 1, 2 is the correct answer.
upvoted 1 times
rocky48
2 years ago
Otherwise it will cause a runtime exception.
upvoted 1 times
...
...
Backy
2 years, 1 month ago
// 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
upvoted 2 times
...
ruydrigo
2 years, 3 months ago
I run it but I don't understand why yield return 1,2
upvoted 1 times
techdawgs
2 years, 3 months ago
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.
upvoted 3 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 ...