Multiple Choice Multiple Choice Sections 4.1-4.3 Linked List Fundamentals The In
ID: 3537394 • Letter: M
Question
Multiple Choice
Multiple ChoiceSections 4.1-4.3
Linked List Fundamentals The IntNode definition:
public class IntNode { int data; IntNode link; ... } Suppose cursor refers to a node in a linked list (using the IntNode class with instance variables called data and link). What statement changes cursor so that it refers to the next node? A. cursor++; B. cursor = link; C. cursor += link; D. cursor = cursor.link; Suppose cursor refers to a node in a linked list (using the Node class with instance variables called data and link). What boolean expression will be true when cursor refers to the tail node of the list? A. (cursor == null) B. (cursor.link == null) C. (cursor.data == null) D. (cursor.data == 0.0) E. None of the above. Which boolean expression indicates whether the numbers in two nodes (p and q) are the same. Assume that neither p nor q is null. A. p == q B. p.data == q.data C. p.link == q.link D. None of the above. Suppose that p is a reference variable that contains the null reference. What happens at runtime if the program tries to activate a method of p? A. IllegalArgumentException B. IllegalStateException C. NullPointerException D. The results are unpredictable. Suppose that a method has one node as a parameter and it returns two references to nodes. What's the best header for the method? A. IntNode foo(IntNode p) B. IntNode, IntNode foo(IntNode p) C. IntNode[ ] foo(IntNode p) D. void foo(IntNode p, IntNode answer1, IntNode answer2) Multiple Choice Section 4.4 The Bag ADT with with a Linked List In the linked list version of the Bag class an instance variable manyNodes is used to keep track of how long the linked list is. Why not just make a call to the IntNode method listLength()? A. The listLength() method is O(n) and the alternative is O(1). B. The listLength() method is private. C. The listLength() method results in an infinite loop for circular lists. D. The listLength() method works only for lists of integers. Suppose that the Bag is implemented with a linked list. Which of these operations are likely to have a constant worst-case time? A. add B. countOccurrences C. remove D. None of (A), (B), and (C) have a constant worst-case time E. TWO of (A), (B), and (C) have a constant worst-case time F. ALL of (A), (B), and (C) have a constant worst-case time What is the expression for generating a pseudorandom number in the range 1...N? A. (int) (Math.random() * N); B. (int) (Math.random() / N); C. (int) (Math.random() * (N + 1)); D. (int) (Math.random() / (N + 1)); E. (int) (Math.random() * N) + 1; Which expression computes a pseudorandom integer between -10 and 10 using rand() from stdlib.h? A. (int) (Math.random( ) * 20) - 10 B. (int) (Math.random( ) * 21) - 10 C. (int) (Math.random( ) * 22) - 10 D. (int) (Math.random( ) * 20) - 11 E. (int) (Math.random( ) * 21) - 11 Multiple Choice Section 4.5 The Sequence ADT with a Linked List For the linked-list version of the Sequence ADT, which operations are constant time operations in the worst case? A. addAfter is constant time, but not addBefore B. addBefore is constant time, but not addAfter C. Both addAfter and addBefore are constant time D. Neither addAfter nor addBefore are constant time Suppose that the Sequence is implemented with a linked list. Which of these operations are likely to have a constant worst-case time? A. addBefore B. countOccurrences C. remove D. None of (A), (B), and (C) have a constant worst-case time E. TWO of (A), (B), and (C) have a constant worst-case time F. ALL of (A), (B), and (C) have a constant worst-case time What is the output of these statements, using your Sequence ADT implemented as a linked list: DoubleLinkedSeq x = new DoubleLinkedSeq( ); DoubleLinkedSeq y = new DoubleLinkedSeq( ); x.addBefore(41); // Inserts 41 into the list x x.addBefore(42); // Inserts 42, so that x is now 42, 41 with cursor at front y = (DoubleLinkedSeq) x.clone( ); x.addAfter(43); // Attaches 43 so that x is now 42, 43, 41 with cursor at 43 y.advance( ); System.out.println("y size is " + y.size( )); System.out.println(" and y current item is " + y.current( )); A. y size is 2 and y current item is 41. B. y size is 2 and y current item is 43. C. y size is 3 and y current item is 41. D. y size is 3 and y current item is 43. E. None of the above. Suppose that you forgot to write the extra work after super.clone in your implementation of the sequence clone method. What is the most likely output from the previous question? A. y size is 2 and y current item is 41. B. y size is 2 and y current item is 43. C. y size is 3 and y current item is 41. D. y size is 3 and y current item is 43. E. None of the above. Multiple Choice Section 4.6 Arrays vs. Linked Lists vs. Doubly Linked Lists What kind of list is best to answer questions such as "What is the item at position n?" A. Lists implemented with an array. B. Doubly-linked lists. C. Singly-linked lists. D. Doubly-linked or singly-linked lists are equally best Multiple ChoiceSections 4.1-4.3
Linked List Fundamentals The IntNode definition:
public class IntNode { int data; IntNode link; ... }
Explanation / Answer
1) D
2) B
since it is not pointing to another NoDE element.
node-> node -> node -> node(tail)
A is wrong since
node-> node -> node(actual tail) -> null... if i'm not mistaken there's a programming term for the type of problem for this one... it is not garbage though.
C does not really tell anything because it only talks about the data. The node might have no data but the Link variable might be referencing another node.
D again refer to explanation in C it should be more or less same explanation except in this case 0 is the value.
3) B As we are comparing the data of both of the nodes, we have to check for the data, a member of nodes p, q. Also we have to use relational operator( ==).
4) C
5) C
6) A
7) A
8) E. (Recall that (int) truncates and that the value returned by Math.random is always less than 1)
9) B
10) A
11) A
12) A
13) B
14) A
I have collected these answers from various sourses. So all of these are right.
pls rate
Thank you :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.