Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please refer to picture for answer choices (1-3) and question 4 and answer choic

ID: 3700860 • Letter: P

Question

Please refer to picture for answer choices (1-3) and question 4 and answer choices are here on text

Suppose you want to write the method addAll(LinkedList<E> list2) for the LinkedList<E> class. This method should add all elements in list2 to the end of the calling list, list2 should not be modified. For example if list1 contains 1,3,5 in that order and list2 contains 3,8,1 in that order calling list1.addAll(list2) should make list1 contain 1,3,5,3,8,1 in that order. Here's your first attempts at this method:

public void addAll(LinkedList<E> list2) {

Node<E> temp = head;

while(temp.next != null)

temp = temp.next;

for(Node<E> temp2 = list2.head; temp2 != null; temp2 = temp2.next) {

Node<E> newNode = new Node<>(temp2.data, null);

temp.next = newNode;

temp = temp.next;

}

}

1. What is the purpose of the while loop in lines 3-4?

2. Suppose addAll is called when both the calling list and list2 are empty, what happens?

3. Suppose addAll is called when the calling list is empty and list2 is non-empty, what happens?

4. Suppose addAll is called when the callinglist is non empty and list2 is empty. What happens?

a) A NullPointerException is thrown in line 3

b)  A NullPointerException is thrown in line 6

c) Lline 7 runs one too many times

d) the head of the calling list is not correctly updated

e) the method workd correctly

please solve

Suppose you want to wnte the method addAjLinkedListsE> list2) for the etercised in lecture. This method should add all clements in Wst2 to the not be modliled. For example, ist1 comtains 1, 3, 5 in that order and lit ating NstladdAnpist2) should make list contain 1, 3.5 3, 8, ? in that order end of the tains 3,8, 1 this method Neta) f void addAllLinkedlist NodecE> temp a head 1 public while (temp.nest nut temp- temp.next NodecE> newNode- new Nodeoftemp2.data, nulil); temp.nextnewNode temp temp.next: 5for (NodecE> temp2- list2.head; temp2 l- null; temp2 - temp2.next) ( Unfortunately, "most" is not god enough -it needs to work for all cases This code works for most cases. 11. What is the purpose of the while loop in Nnes 3-4 (A) To determine the length of the caling ist (B) To move temp to the first non-nuli node of the calling list (C) To move temp to the tal of the calling Dst tEThe while oopt oing ahigetul and should be removed 12 Suppose addAll s ca'iled when both the caling fist and listz an are empty. What happens? A) A NulPointerException is thrown in ne 3 (B) A NuPointerException is thrown in c) Line 7 runs one too many times (D) Line 7 runs one too few times (E) The method works correctly tine 6 when the calling istis empty and list2 is non-empty Exception ts thrown in tine 3 13. Suppose addAll ls called the A NullPointer ANulIP?InterException is thrown Line 7 runs one too many times Line 7 runs one too fev t Y. What happens? (B) (D) E) The method works correctiy

Explanation / Answer

11)

obtion B

in while loops it checks the temp is empty or not

if empty it enter the loop and move the temp to the next non empty node.

12)

Just the code executed corretly

there is no exception handler to throw and also there is no loop if both are empty.

So it just compiled corrctly

i.e., option D

13)

Option D

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote