PCAP Exam QuestionsBrowse all questions from this exam

PCAP 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.)

Show Answer
Correct Answer: AC

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

9 comments
Sign in to comment
vlobachevskyOptions: AC
Oct 11, 2021

AC is correct

ruydrigoOptions: AC
Dec 14, 2021

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

macxszOptions: AC
May 4, 2022

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

dicksonpwcOptions: AC
Apr 25, 2023

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

Noarmy315Options: AC
Jan 16, 2022

AC is correct

JnanadaOptions: AC
Aug 19, 2022

Correct answer should be AC

9prayerOptions: AC
Feb 5, 2023

AC is correct

9prayerOptions: AC
Feb 5, 2023

AC is correct

andr3Options: AC
Mar 25, 2023

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