QuestionQ81

Object-Oriented Programming

Which of the following code lines will work correctly when placed independently inside the add_new() method so that the snippet outputs [0, 1, 2]?

Question Image

Choose two
  • A self.queue.append(self.queue[-1] + 1)
  • B self.queue.append(self.get_last() + 1)
  • C self.queue.append(get_last() + 1)
  • D queue.append(self.get_last() + 1)
Explanation

self.queue is the instance list, initially [0, 1]. Its last value is 1, so appending that value plus 1 adds 2. Both direct instance-list access and the instance method call through self correctly access that value.

Community Discussion

No comments yet. Be the first to start the discussion!