AP Computer Science Chapter 9 (MC)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

21) A stack could be simulated with a priority queue.

True Explanation: Consider a priority queue where each item's priority is its time of arrival into the queue, and later times are given higher priority. This produces LIFO processing which is what a stack uses.

5) A linked list that contains 6 Nodes will have 6 reference pointers.

False Explanation: In order to access the linked list, there needs to be a 7th reference pointer at least, one that references the first item in the list.

1) An Abstract Data Type is a data structure, that is, it is the listing of the instance data and the visibility modifiers for those instance data.

False An Abstract Data Type includes the data structure, but also includes the methods implemented to access/manipulate the data structure.

20) A priority queue uses FIFO processing

False Explanation: A priority queue uses priorities to determine which element comes off in a dequeue operation.

25) In a doubly linked list, every node has a pointer to both the first node in the list and the last node in the list.

False Explanation: In a doubly linked list every node points to the node before it and the node after it.

11) To simulate people waiting in a line you could use a stack.

False Explanation: The FIFO access of a Queue is the same access of people waiting in line, the first person to enter will be the first to leave. So, simulations of any kind of line structure will use the Queue data structure. The Stack uses LIFO (last in, first out) access which does not make sense for a line.

For questions 12 - 14, assume a Stack class stores int values. Consider the following sequence of instructions. Stack s = new Stack( ); s.push(16); s.push(12); s.push(19); int x = s.pop( ); s.push(5); s.push(9); s.push(4); int y = s.pop( ); int z = s.pop( ); 12) After the instructions execute, x has the value 12.

False Explanation: The Stack offers a last-in first-out access, so at the point when s.pop( ) is first executed, the item removed is the last item pushed onto s, or 19.

6) The linked list is superior to the array in all ways when it comes to implementing a list.

False Explanation: The advantage of the linked list is that it is dynamic while the array is static. However, the array supports random access (the ability to access any element of the array easily) whereas a linked list does not support this (to access the 8th element, the first 7 elements must be traversed). So, the linked list has a major disadvantage, and even though is dynamic, many programmers choose to use an array to implement a list because of this drawback.

4) An array is a List Abstract Data Type.

False Explanation: The array is a data structure that can be used to store a List of values, but the array does not have operations already implemented to perform List operations such as add to the end, or delete a given value.

For questions 15 - 17, consider the following operations on a Queue data structure that stores int values. Queue q = new Queue( ); q.enqueue(3); q.enqueue(5); q.enqueue(9); System.out.println(q.dequeue( )); q.enqueue(2); //d1 q.enqueue(4); System.out.println(q.dequeue( )); //d2 System.out.println(q.dequeue( )); //d3 q.enqueue(1); q.enqueue(8); 16) The value 5 is returned by the last dequeue operation (denoted above with a d3 in comments).

False Explanation: The dequeue operations remove the next item in the Queue, so the dequeue in d1 removes 3, the dequeue in d2 removes 5 and the dequeue in d3 removes 9.

24) A priority queue could not be implemented using an array.

False Explanation: There are trade-offs between different implementations, but it is certainly possible to implement a priority queue using an array.

9) All classes are considered Abstract Data Types.

False Explanation: To be considered an Abstract Data Type, the type must define a data structure and the methods to manipulate the data structure. However, a programmer does not have to set up a data structure in this way. For instance, if the instance data are made public, then there is no need to implement methods to manipulate those instance data, they can be directly modified from other classes. Therefore, just because a class exists does not mean that the class is an ADT.

2) The push and enqueue operations are essentially the same operations, push is used for Stacks and enqueue is used for Queues.

False While both operations are "add" or "insert" operations for their respective Abstract Data Types, they differ in that push always adds at the top (or front) of the Stack while enqueue always adds at the rear of the Queue.

22) A priority queue is a dynamic data structure.

True Explanation: A priority queue can grow and shrink during use, so it is a dynamic data structure.

For questions 12 - 14, assume a Stack class stores int values. Consider the following sequence of instructions. Stack s = new Stack( ); s.push(16); s.push(12); s.push(19); int x = s.pop( ); s.push(5); s.push(9); s.push(4); int y = s.pop( ); int z = s.pop( ); 13) After the instructions execute, z has the value 9.

True Explanation: At that point that the int z = s.pop( ); executes, a previous s.pop( ); will have removed the most recently pushed item (4), so this pop removes the next most recently pushed item (9).

10) All Abstract Data Types are defined as classes in Java.

True Explanation: In Java, all data are either primitives or classes. An ADT is a data structure, so it is more than just a primitive type, but a structure of different types. Therefore, the ADT must be defined in a class. Not all classes are necessarily ADTs, but all ADTs are defined as classes.

19) The LinkedList class in the Java standard class library represents a list and is implemented using a linked list.

True Explanation: The LinkedList class implements the List interface using a linked list implementation.

7) Queues and Stacks can be implemented using either arrays or linked lists.

True Explanation: The Queue and the Stack are both Abstract Data Types, their method of implementation is immaterial, it is the operations (enqueue, dequeue, push, pop) that define them. So, both ADTs can be implemented using either arrays or linked lists.

8) In order to input a list of values and output them in order, you could use a Queue. In order to input a list of values and output them in opposite order, you could use a Stack.

True Explanation: The Queue provides First-in First-out access, so the items would be accessed in the order that they were entered. The Stack provides Last-in First-out access, so the items would be accessed in the opposite order that they were entered.

For questions 15 - 17, consider the following operations on a Queue data structure that stores int values. Queue q = new Queue( ); q.enqueue(3); q.enqueue(5); q.enqueue(9); System.out.println(q.dequeue( )); q.enqueue(2); //d1 q.enqueue(4); System.out.println(q.dequeue( )); //d2 System.out.println(q.dequeue( )); //d3 q.enqueue(1); q.enqueue(8); 15) After the code above executes, 4 elements remain in q.

True Explanation: The constructor creates an initially empty Queue. Each enqueue operation adds one int to the Queue and each dequeue operation removes one int from the Queue. There are 7 enqueue operations and 3 dequeue operations, leaving 4 int values in q at the end.

3) The Abstract Data Type (ADT) is thought of as abstract because the operations that are to be implemented are separated from the actual implementation, that is, an ADT can be implemented in more than one way and that implementation is separate from how we might use the ADT.

True Explanation: The importance of the ADT is that the operations are implemented as we would expect so that the operation is performed correctly even if we do not know how the operation is implemented. In this way, we assume that a Queue adds at the rear and removes from the front so that we have a FIFO access even though we do not know how the Queue is implemented itself.

For questions 15 - 17, consider the following operations on a Queue data structure that stores int values. Queue q = new Queue( ); q.enqueue(3); q.enqueue(5); q.enqueue(9); System.out.println(q.dequeue( )); q.enqueue(2); //d1 q.enqueue(4); System.out.println(q.dequeue( )); //d2 System.out.println(q.dequeue( )); //d3 q.enqueue(1); q.enqueue(8); 17) If we replace each System.out.println statement (denoted in comments as d1, d2 and d3) with the statement q.enqueue(q.dequeue( )); q would contain the following values in the order given: 3, 2, 4, 5, 9, 1, 8

True Explanation: The original order of int values in q is 3, 5, 9. At d1, the first item is dequeued and then enqueued resulting in 5, 9, 3. Two more enqueues result in 5, 9, 3, 2, 4. At d2 and d3, the first two items are dequeued and then enqueued resulting in 3, 2, 4, 5, 9. Finally, 1 and 8 are enqueued leaving q as 3, 2, 4, 5, 9, 1, 8.

23) There is no way to go backwards in a singly linked list.

True Explanation: The pointers only point forward in a singly linked list. You would need a doubly linked list in order to go backwards.

18) The first node in a linked list often requires special handling

True Explanation: When inserting or deleting a node from the beginning of the linked list, a special case is required.

For questions 12 - 14, assume a Stack class stores int values. Consider the following sequence of instructions. Stack s = new Stack( ); s.push(16); s.push(12); s.push(19); int x = s.pop( ); s.push(5); s.push(9); s.push(4); int y = s.pop( ); int z = s.pop( ); 14) After the instructions execute, y has the value 4.

True Explanation: When int y = s.pop() is executed, the most recently pushed item is 4, so 4 is popped off assigned to y.

4) Abstract Data Types have which of the following object-oriented features? a) Information hiding b) Inheritance c) Polymorphism d) Message Passing e) All of the above

a Explanation: All of these answers are types of object-oriented features. An Abstract Data Type encapsulates a data structure and the methods to manipulate the data structure such that information hiding is preserved. Therefore, all ADTs make use of information hiding so that the data structure cannot be manipulated directly from outside of the ADT, but the other object-oriented features are not required.

14) Which of the following methods could be used to sum all of the items in the linked list? a) public int sumIt(Node temp) { while(temp != null) { sum += temp.info; temp = temp.next; } return sum; } b) public int sumIt(Node temp) { while(temp != null) { sum += temp.info; } return sum; } c) public int sumIt(Node temp) { while(temp != null) { sum++; temp = temp.next; } return sum; } d) public int sumIt(Node temp) { while(temp != head) { sum += temp.info; temp = temp.next; } return sum; } e) public int sumIt(Node temp) { while(temp != null) { if(next != null) sum += temp.info; temp = temp.next; } return sum; }

a Explanation: Answer b does not advance temp to reference the next Node, and is therefore an infinite loop. Answer c counts the number of items in the list but does not sum up the values of these items. Answer d has the wrong loop condition and answer e counts all of the items in the list except for the last one.

21) One operation that we might want to implement on a Stack and a Queue is full, which determines if the data structure has room for another item to be added. This operation would be useful a) only if the Queue or Stack is implemented using an array b) only if the Queue or Stack is implemented using a linked list c) only for a Queue d) only for a Stack e) none of the above, a full operation is not useful at all

a Explanation: Since the array is a static sized object, if it becomes filled, then any add type of operation, whether it is a List insert, a Queue enqueue or a Stack push, should be prevented. This can be determined by first checking to see if the structure is full or not. This is not necessary if the data structure is implemented using a linked list since (we assume that) there will always be dynamic memory available to add a new element.

For questions 15 - 18, assume that a linked list consists of Node objects, where Node has two instance data, int info and Node next. The linked list stores in the info data, 20, 11, 13, 19, 12, 14 in that order. Assume that Node head references the first item in the list list. 16) What is returned by return head.info; ? a) 20 b) 11 c) 13 d) 19 e) 6

a Explanation: The variable head references the first item, and so head.data is the instance data info of the first item, which stores 20.

12) Assume Node2 is defined as follows: int data; Node2 a, b; where a refers to the Node2 before this one in a linked list and b refers to the Node2 after this one in a linked list. following variations of a linked list? a) singly linked list b) doubly linked list c) singly linked list with a header node d) singly linked list with a top node e) circularly linked list

b Explanation: Because each item in the linked list has a reference to both the next node and the previous node, the list is known as a doubly linked list. With only a reference to the next node, the list would be known as a singly linked list. A list with a header node has a special node which has references to both the first node in the list and the last node in the list (often refered to as a head and a tail).

For questions 8 - 11, assume that a linked list is implemented using the Node class where a Node contains instance data of int info; and Node next; where next references the next Node in the linked list. Also assume that head references the first Node in the list. 9) Assume Node temp references the last element of the linked list. Which of the following conditions is true about temp? a) (temp.info = = 0) b) (temp.next = = null) c) (temp = = head) d) (temp = = null) e) (temp.next = = null && temp.info = = null)

b Explanation: Since temp is the last Node in the list, there is no next node, so temp.next is null. The answer in e is incorrect because temp references the last Node and this Node does have a value, so temp.info will equal some int value.

For questions 15 - 18, assume that a linked list consists of Node objects, where Node has two instance data, int info and Node next. The linked list stores in the info data, 20, 11, 13, 19, 12, 14 in that order. Assume that Node head references the first item in the list list. 18) What will the statement head.next.next = head.next.next.next; accomplish? a) it will result in the list ending after 13 b) it will result in the value 13 being deleted from the list c) it will result in the list ending after 19 d) it will result in the value 19 being deleted from the list e) it will result in the list ending after 12

b Explanation: The reference head.next.next is of the second Node in the list's next instance data, or 11's next. By setting this Node's next field to be head.next.next.next, this Node now references the Node storing 19 rather than 13, so it deletes 13 from the list.

For questions 15 - 18, assume that a linked list consists of Node objects, where Node has two instance data, int info and Node next. The linked list stores in the info data, 20, 11, 13, 19, 12, 14 in that order. Assume that Node head references the first item in the list list. 17) What is the result to the linked list of the following instructions? Assume that newNode is a Node, already constructed. newNode.data = 1; newNode.next = head.next; head.next = newNode; a) The value 1 is inserted into the linked list before 20 b) The value 1 is inserted into the linked list after 20 and before 11 c) The value 1 is inserted into the linked list after 11 and before 13 d) The value 1 is inserted into the linked list after 13 and before 19 e) The value 1 is inserted into the linked list after 20 and the rest of the list is lost

b Explanation: The statement newNode.next = head.next; sets the new node's next field to equal the second item in the list, so the new node, which stores 1, is inserted before 11. The statement head.next = newNode resets the first Node's next field to reference the new node, so 1 is now inserted after 20 and before 11.

2) A collection whose items are of different types is referred to as a(n) _______ type. a) homogeneous b) heterogeneous c) dynamic d) abstract e) vector

b Explanation: The term heterogeneous means that the elements are different types. In Java, classes can store heterogeneous types, for instance one instance data might be an int and another a String and a third a double. Arrays on the other hand are homogeneous types because every element stored in the array is the same type.

For questions 8 - 11, assume that a linked list is implemented using the Node class where a Node contains instance data of int info; and Node next; where next references the next Node in the linked list. Also assume that head references the first Node in the list. 10) Assume that the linked list has at least two Nodes in it. Which of the following instructions will return the second int value in the list? a) return head.info; b) return head.next.info; c) return head.next.next.info; d) return head.next.next.next.info; e) It is not possible to return the second int value in the list using head.

b Explanation: The variable head references the first Node in the list, so that head.info is the first int value. The statement head.next references the second Node in the list, so head.next.info references the second Node's data item.

For questions 8 - 11, assume that a linked list is implemented using the Node class where a Node contains instance data of int info; and Node next; where next references the next Node in the linked list. Also assume that head references the first Node in the list. 11) Assume Node temp is currently set equal to head. Which of the following while loops could be used to iterate through each element of a linked list? a) while (head != null) head = temp.next; b) while (temp != null) temp = temp.next; c) while (head != null) temp = temp.next; d) while (head != null) head = head.next; e) while (temp != null) head = head.next;

b Explanation: To iterate through the list, we will use a different reference variable than head so that we do not lose our reference to the list, so we use temp instead. To iterate through the list, temp starts at head and continues until temp becomes null (it no longer references a node). To move to the next item in the list, temp is updated to be temp.next. The answer in d is correct but it loses the reference to the list and so is not what we would desire to do. The other loops have incorrect logic resulting in either infinite loops, or loops that cause run-time Exceptions.

13) Which of the following methods could be used to count the number of items in the linked list? a) public int countIt(Node temp) { while(temp != null) { count += temp.info; temp = temp.next; } return count; } b) public int countIt(Node temp) { while(temp != null) { count++; } return count; } c) public int countIt(Node temp) { while(count != null) { count++; temp = temp.next; } return count; } d) public int countIt(Node temp) { while(temp != head) { count++; temp = temp.next; } return count; } e) public int countIt(Node temp) { while(temp != null) { if(next != null) count++; temp = temp.next; } return count; }

c Explanation: Answer a sums all items in the array. Answer b does not advance temp to reference the next Node, and is therefore an infinite loop. Answer c properly counts each Node and moves on to the next Node. Answer d has the wrong loop condition and answer e counts all of the items in the list except for the last one.

19) A variation of a linked list is a circular linked list where the last Node in the list has next = head rather than next = null. One problem with this type of list is that a) it wastes memory space since head already points at the first Node, so the last one does not need to b) there is no ability to add a new Node at the end of the list since the last Node points at the first Node c) it is more difficult to traverse the list since the old terminating condition, (next = = null), is no longer true for the last node d) a header Node for this type of list is more complex e) all of the above

c Explanation: Answers a, b and d are not true. This list uses no more memory than an ordinary linked list, adding a Node at the end requires the same amount of effort as before, and a header Node would be no different than before. However, answer c is true, the decision of whether there is another Node in the list or not can no longer be determined by testing (next = = null) since the last Node's next field is equal to head instead of null.

For questions 8 - 11, assume that a linked list is implemented using the Node class where a Node contains instance data of int info; and Node next; where next references the next Node in the linked list. Also assume that head references the first Node in the list. 8) Which of the following instructions would create an initially empty linked list? a) Node head = new Node( ); b) Node head = Node; c) Node head = null; d) Node head = new Node(0); e) Node head = list;

c Explanation: The initial linked list will be empty, and so head should be null to indicate that there are no Nodes in the list yet. The answer in b is syntactically invalid, and the answers in a and d may be invalid depending on what parameter(s) the Node constructor expects, but in any event, the answers in a and d will create a Node, and therefore the initial list will not be empty.

For questions 23 - 24, assume Node is a class consisting of an int info and a Node next and header is a HeaderNode that has Node front, rear and int count where front references the first item in the list, rear references the last item in the list and count is the number of elements in the list. 24) This type of linked list is referred to as a a) singly linked list b) doubly linked list c) singly linked list with a header node d) doubly linked list with a header node e) singly linked list with a head and rear references

c Explanation: The list is singly linked because there is only a next reference in each Node rather than a next and previous. Further, header is a header node for the list. The header node does contain a reference to head and rear, making answer e partly correct, but the references are part of a header node that includes the count (number of Nodes) in the list, so answer c is more correct.

5) Which of the following criticisms of an array is applicable to a Java array? a) It is an inefficient structure to access random elements b) It only supports First-in First-out types of accesses c) It is fixed in size (static) d) It cannot be used to create an Abstract Data Type such as a Queue or Stack e) All of the above

c Explanation: The size of any array in Java is fixed when the array is instantiated. If, in adding elements to the array, it becomes filled, the array itself cannot change in size. A new array could be created with the old array elements moved into the new array, but this is inefficient.

20) Which of the following lists of operations is used to see the top item of a stack without removing it from the stack? a) push b) pop c) peak d) see e) top

c Explanation: The three commands associated with a Stack are push, pop, or peak. Push adds a new element to the top of the Stack, pop removes the top element off of the Stack and peak returns the top of the Stack without removing it.

For questions 15 - 18, assume that a linked list consists of Node objects, where Node has two instance data, int info and Node next. The linked list stores in the info data, 20, 11, 13, 19, 12, 14 in that order. Assume that Node head references the first item in the list list. 15) What will be returned by return head.next.next.next.info; ? a) 20 b) 11 c) 13 d) 19 e) 12

d Explanation. The variable head references the first item, which stores 20, head.next references the second item, which stores 11, head.next.next references the third item, which stores 13, and head.next.next.next references the fourth item, which stores 19.

3) Which of the following is considered an Abstract Data Type? a) array b) reference variable c) any of the primitive types (e.g., int, double, char) d) ArrayList e) all of the above

d Explanation: An Abstract Data Type comprises a data structure and the methods to manipulate and access the data structure. Of those listed, only the ArrayList combines both of these. The array is a data structure but without the methods (such as an insert method or a search method) while the reference variables and primitive types are data but not data structures.

For questions 23 - 24, assume Node is a class consisting of an int info and a Node next and header is a HeaderNode that has Node front, rear and int count where front references the first item in the list, rear references the last item in the list and count is the number of elements in the list. 23) Previously, to iterate through a linked list, we used a while loop. Which of the following for-loops could replace the previous while loop that would start at head and go until temp == null? a) for (Node temp = header.front, int j = 0; j < header.count; temp = temp.next) { ... } b) for (int j = 0; j < header.count; j++) { ... } c) for (Node temp = header.front; temp != header.rear; temp = temp.next) { ... } d) for (Node temp = header.front, int j = 0; j < header.count; temp = temp.next, j++) { ... } e) for (Node temp = header.front, int j = 0; j < header.count && temp != header.rear; temp = temp.next, j++) { ... }

d Explanation: Since header stores the number of elements in the linked list, we can start with a counter (j) at 0 and iterate while the counter is less than header.count, adding 1 after each iteration. We also need to make sure that we go from Node to Node. Only the loop in answer d accomplishes both of those steps. Note that answer c might seem right, but would stop once the loop has reached the last Node, not once the loop has finished with the last Node.

25) The expression LIFO stands for a) LIst FOundation b) LInk FOrmation c) Last In Front Of d) Last In First Out e) Long Int Float Object

d Explanation: The Stack is a data structure in which items are added at the top end and removed from the top end, so the most recent item added is the first one removed (last in, first out). It is abbreviated as LIFO for convenience.

6) A linked list that stores int values would be comprised of a group of Nodes. We might define the Node by a) class Node { Node next; } b) class Node { int next; } c) class Node { int data; } d) class Node { int data; Node next; } e) class Node { int[ ] data; Node next; }

d Explanation: The linked list stores int values, so each Node requires an int data type. Further, because it is a linked list, each node must contain a reference to the next Node, which is defined as Node next; where next is the reference.

1) An array can be classified as what type of object? a) dynamic b) ordered c) first-in first-out d) heteogeneous e) collection

e Explanation: An array stores a group of items and is dubbed a collection type (as opposed to other types that can store a single item). There are numerous collection types, the array being one composed only of homogeneous items (the array stores multiple items but they all must be of the same type).

22) In order to gain a last-in first-out access to data, which type of structure should be used? a) ArrayList b) Array c) Linked List d) Queue e) Stack

e Explanation: The Stack is a data structure that allows only a last-in first-out (LIFO) access. Queues offer only a first-in first-out (FIFO) access. The Vector, Array and Linked List all offer any form of access desired.

7) The advantage of creating a BookList using a linked list instead of using an array is that the linked list a) offers easier access to a random element in the list b) uses less memory c) is easier to implement and debug d) can store types other than Books unlike the array e) is dynamic and so can be any size needed

e Explanation: The advantage of the linked list over the array to implement such data structures as lists of items is that more items can always be added to it. This is known as a dynamic structure, unlike the fixed-sized array which is known as a static structure. Disadvantages of the linked list are that they are no easy to access randomly, unlike an array, and they are often more difficult to implement and debug than code using arrays. Further, while the linked list stores one Node for each item in the list, each Node requires at least two data items, the information, and a link to the next Node, so they may use more memory than an array.


Set pelajaran terkait

Science Convection and the Mantle

View Set

MKT 3311 Principles of Marketing chapter 15

View Set

bio exam 3 test bank pt 1 questions(chp 17)

View Set