Exam PCAP All QuestionsBrowse all questions from this exam
Question 104

Which of the following lines of code will work flawlessly when put independently inside the add_new() method in order to make the snippet's output equal to [0,

1, 2]? (Choose two.)

    Correct Answer: A, C

    To achieve the output [0, 1, 2], the add_new() method needs to append the next integer to the queue. Option A, 'self.queue.append(self.get_last() + 1)' calls the get_last() method to fetch the last element in the queue and adds 1 to it, effectively appending the correct next integer. Option C, 'self.queue.append(self.queue[-1] + 1)' directly accesses the last element of the queue using the [-1] index and adds 1 to it. Both options correctly append the next integer, making them valid choices.

Discussion
vlobachevskyOptions: AC

AC is correct

ruydrigoOptions: AC

class MyClass: def __init__(self,size): self.queue = [i for i in range(size)] def get(self): return self.queue def get_last(self): return self.queue[-1] def add_new(self): #self.queue.append(self.get_last()+1) ##[0, 1, 2] #self.queue.append(get_last()+1) ##Error #self.queue.append(self.queue[-1] + 1) ##[0, 1, 2] #queue.append(self.get_last() + 1) ##Error Object = MyClass(2) Object.add_new() print(Object.get()) A & C are correct

dicksonpwcOptions: AC

self.queue.append(self.get_last() + 1) # ans A, return [0, 1, 2] self.queue.append(get_last()+1) # ans B, show NameError at line 108: # name 'get_last' is not defined self.queue.append(self.queue[-1] + 1) # ans C, return [0, 1, 2] queue.append(self.get_last() + 1) # ans D, IndentationError: expected an indented block #after function definition on line 105 Correct answer should be A, C

macxszOptions: AC

A. self.queue.append(self.get_last() + 1) C. self.queue.append(self.queue[-1] + 1)

andr3Options: AC

def add_new(self): self.queue.append(self.get_last() + 1) self.queue.append(self.queue[-1] + 1)

9prayerOptions: AC

AC is correct

9prayerOptions: AC

AC is correct

JnanadaOptions: AC

Correct answer should be AC

Noarmy315Options: AC

AC is correct