0% found this document useful (0 votes)
15 views11 pages

Chapter15 TB

This document discusses linked data structures and contains multiple choice questions, true/false questions, and short answer/essay questions about linked lists and nodes. It includes diagrams of linked lists with nodes containing city names and pollution indexes.

Uploaded by

Anoush Azarfar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

Chapter15 TB

This document discusses linked data structures and contains multiple choice questions, true/false questions, and short answer/essay questions about linked lists and nodes. It includes diagrams of linked lists with nodes containing city names and pollution indexes.

Uploaded by

Anoush Azarfar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter 15 Linked Data Structures 1

Chapter 15
Linked Data Structures

◼ Multiple Choice
1) The first node in a linked list is commonly referred to as the ________ node.
(a) head
(b) tail
(c) predecessor
(d) successor
Answer: A

2) In Java, a node is a/an:


(a) String
(b) Integer
(c) Object
(d) Exception
Answer: C

3) A node contains:
(a) data item(s)
(b) reference(s) to another node
(c) both A and B
(d) none of the above
Answer: C

4) In Java, you indicate the end of a linked list be setting the link instance variable of the last node in
the linked list to __________.
(a) 0
(b) -1
(c) 1
(d) null
Answer: D
Chapter 15 Linked Data Structures 2

5) If the head instance variable of a linked list contains a reference to null, this means the list is:
(a) full
(b) empty
(c) lost
(d) none of the above
Answer: B

6) Making the Node class a private inner class of a linked data structure is an example of:
(a) polymorphism
(b) encapsulation
(c) inheritance
(d) all of the above
Answer: B

7) A common exception that occurs when using linked lists is the:


(a) NodeOutOfBoundsException
(b) NodeEmptyException
(c) NullPointerException
(d) NullNodeOccurredException
Answer: C

8) A ____________ copy of an object is a copy that has no references in common with the original
object.
(a) bit copy
(b) deep copy
(c) shallow copy
(d) none of the above
Answer: B

9) A _____________ copy of an object is a copy that has references in common with the original
object.
(a) bit copy
(b) deep copy
(c) shallow copy
(d) none of the above
Answer: C

10) To use the Java Iterator Interface you must import the ____________ package.
(a) java.tools
(b) java.linkedlist
(c) java.iterator
(d) java.util
Answer: D
Chapter 15 Linked Data Structures 3

11) Java contains a mechanism that automatically reclaims memory. This mechanism is called:
(a) Garbage elimination
(b) Garbage collection
(c) Taking out the trash
(d) None of the above
Answer: B

12) A ____________ linked list has nodes that contain two references to Nodes.
(a) circular
(b) sequential
(c) doubly
(d) one-way
Answer: C

13) The _________ node is the first node in the tree data structure.
(a) leaf
(b) root
(c) sibling
(d) branch
Answer: B

14) A binary tree has exactly _________ link instance variables.


(a) zero
(b) one
(c) two
(d) three
Answer: C

15) Recursively visiting the root node, left subtree and then the right subtree describes:
(a) preorder processing
(b) inorder processing
(c) postorder processing
(d) none of the above
Answer: A

16) Recursively visiting the left subtree, right subtree and then the root describes:
(a) preordering processing
(b) inorder processing
(c) postorder processing
(d) none of the above
Answer: C
Chapter 15 Linked Data Structures 4

17) A _________________ maps a data value such as a String into a number:


(a) recursive function
(b) key function
(c) hash function
(d) none of the above
Answer: C

◼ True/False
1) A linked data structure contains nodes and links.
Answer: True

2) Java does not come with a LinkedList library class.


Answer: False

3) When using a linked list, you do not need to know when the end of the list has been reached.
Answer: False

4) Forgetting to set the reference instance variable of the last node in a linked list to null will cause a
logic error.
Answer: True

5) Linked lists introduce the possibility of a privacy leak occurring.


Answer: True

6) A deep copy of an object is a copy that has references in common with the original object.
Answer: False

7) A copy constructor and a clone method should normally make a deep copy whenever possible.
Answer: True

8) If you define a clone method, the class should implement the Cloneable interface.
Answer: True

9) An iterator is any object that allows you to step through the list one item at a time.
Answer: True

10) A stack cannot be represented as a linked list.


Answer: False

11) A queue is a last-in/first-out structure.


Answer: False
Chapter 15 Linked Data Structures 5

◼ Short Answer/Essay
1) What is the function of the variable head when used with a linked list? What is the data type of the
head variable?
Answer: The head variable references the first node in a linked list. Without this reference variable,
the beginning of the list is lost. The data type of the head variable is of the Node type.

2) Draw a diagram of a linked list that contains nodes with data items of type String that contains the
name of a city and type double that contains a pollution index. Include an instance variable named
head to indicate the beginning of the list. Insert the following nodes: Franklin, 15.7, Chicago, 23.2,
Denver, 7.2.
Answer:

Franklin
Head
15.7

Chicago

23.2

Denver

7.2

NULL

3) Redraw the diagram created in number 2 above after inserting a node containing Chattanooga, 27.6.
Answer:
Chapter 15 Linked Data Structures 6

Chattanooga
Head
27.6

NULL

Franklin

15.7

Chicago

23.2

Denver

7.2

NULL

4) Redraw the diagram created in number 3 above, after deleting the node containing Chicago.
Answer:

Chattanooga
Head
27.6

NULL

Franklin

15.7

Denver

7.2

NULL

5) Redraw the diagram created in number 4 above after deleting the head node.
Answer:
Chapter 15 Linked Data Structures 7

Franklin
Head

15.7

Denver

7.2

NULL

6) Create a generic Node class to represent the linked list depicted in your diagrams above.
Answer:

public class Node

private String cityName;

private double pollutionCount;

private Node link;

public Node()

link = null;

cityName = null;

pollutionCount = 0.0;

}
Chapter 15 Linked Data Structures 8

public Node(String cName, double pCount, Node linkValue)

setData(cName, pCount);

link = linkValue;

public void setData(String cName, double pCount)

cityName = cName;

pollutionCount = pCount;

public void setLink(Node newLink)

link = newLink;

public String getCityName()

return cityName;

}
Chapter 15 Linked Data Structures 9

public double getPollutionCount()

return pollutionCount;

public Node getLink()

return link;

7) Given the Node class created in number 6 above, write Java statements to insert a new node
containing (Chattanooga, 23.7) into an empty list.
Answer:

Node head=null;

Node current=null;

//Create a new node and insert it into an empty list.

current = new Node("Chattanooga", 23.7, null);

head = current;

8) Given the Node class created in number 6 above, write Java statements to delete a node from the
beginning of the list.
Answer:

current = head;
Chapter 15 Linked Data Structures 10

//Check for one element list

if(current.getLink() == null)

head = null;

else

current = current.getLink();

head = current;

current = null;

9) Write a method called displayList that displays the data items in the Node class created in number 6
above.
Answer:

public static void DisplayList(Node h)

Node current = h;

while(current != null)

System.out.println("The node contains " + current.getCityName() + " with

a pollution index of " + current.getPollutionCount());

current = current.getLink();
Chapter 15 Linked Data Structures 11

10) What is the binary search tree storage rule?


Answer:
1. All the values in the left subtree are less than the value in the root node.
2. All the values in the right subtree are greater than or equal to the value in the root node.
3. This rule applies recursively to each of the two subtrees.
(The base case for the recursion is an empty tree, which is always considered to satisfy the rule.)
11) Draw the resulting binary search tree inserting the following values in the given order: 7, 10, 5, 12,
1, 3, 9.
Answer:

5 10

1 3 9 12

12) What is the result of a preorder traversal of the binary search tree created in question 12 above?
Answer: 7, 5, 1, 3, 10, 9, 12

13) What is the result of an inorder traversal of the binary search tree created in question 12 above?
Answer: 1, 3, 5, 7, 9, 10, 12

14) What is the result of a postorder traversal of the binary search tree created in question 12 above?
Answer: 1, 3, 5, 9, 12, 10, 7

15) Discuss the differences between a queue and a stack.


Answer: A stack is a last-in/first-out (LIFO) data structure. Items are removed in the reverse order
in which they were inserted. A queue is a first-in/first-out (FIFO) data structure. It
mimics a line. Items are inserted at the end of the structure and are removed from the front
of the structure.

You might also like