CS 51510 Exam 2 Practice Problems The exam is on Wednesday, October 30. The exam will be over the following sections. Chapter 10, Sections 10.1, 10.2, and 10.3 from the course textbook. Chapter 11, Sections 11.1, 11.2, and 11.3 from the course textbook. Sections 16.1, 16.2, 16.3, and 16.4 from https://bcs.wiley.com/he-bcs/Books?action=mininav&bcsId=3663&itemId=0470105550&assetId=110292&resourceId=10137 The data structures and abstract data types covered are linked-list, ArrayList, LinkeList, Dictionary, hash-table, binary tree. You should be familiar with the implementations of these in http://cs.pnw.edu/~rlkraft/cs51510/for-class/linked-list_DS.zip http://cs.pnw.edu/~rlkraft/cs51510/for-class/List_ADT.zip http://cs.pnw.edu/~rlkraft/cs51510/for-class/hashing.zip http://cs.pnw.edu/~rlkraft/cs51510/for-class/tree_DS.zip Below are several practice problems that are similar to the kinds of problems that might be on the exam. Problem 1) The following line generates a compiler error. What is wrong? How can you fix it? java.util.List list = new java.util.List<>(); Problem 2) While String is a sub-type of Object (that is, every String object "is a" Object object), it is not true that ArrayList is a sub-type of ArrayList. Explain why. Hint: If B "is a" A, then everything you can do with an A object you can also do with a B object. What can you do with an object of type ArrayList that you cannot do with an object of type ArrayList? Problem 3) What is wrong with declaring a method like this? What limitation is there in this method? What would be a better declaration? public static void doSomethingToList(ArrayList list) { } Problem 4) Here is a simple implementation of a Node class for linked lists of integers. class Node { private int data; private Node link; public Node(int data, Node link) { this.data = data; this.link = link; } public int getData( ) { return data; } public Node getLink( ) { return link; } public void setData(int data) { this.data = data; } public void setLink(Node link) { this.link = link; } } Using this implementation of Node, suppose you are given reference variables head and cursor that are initialized with this picture. Node Node Node Node Node +-----+ +-----+ +-----+ +-----+ +-----+ head | o--|------>| 6 | +-->| 4 | +-->| 0 | +-->| 8 | +-----+ +-----+ | +-----+ | +-----+ | +-----+ | o---|--+ | o---|--+ | o---|--+ | null| +-----+ +-----+ +-----+ +-----+ /|\ Node | +-----+ | cursor | o--|------------------------------------+ +-----+ (a) Starting with the above picture, write a single line of code that creates this picture. Node Node Node Node Node Node +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ head | o--|------>| 6 | +-->| 2 | +-->| 4 | +-->| 0 | +-->| 8 | +-----+ +-----+ | +-----+ | +-----+ | +-----+ | +-----+ | o---|--+ | o---|--+ | o---|--+ | o---|--+ | null| +-----+ +-----+ +-----+ +-----+ +-----+ /|\ Node | +-----+ | cursor | o--|-------------------------------------------------+ +-----+ (b) Starting again with the original picture, write a single line of code that creates this picture. Node Node Node Node Node Node +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ head | o--|------>| 6 | +-->| 4 | +-->| 0 | +-->| 7 | +-->| 8 | +-----+ +-----+ | +-----+ | +-----+ | +-----+ | +-----+ | o---|--+ | o---|--+ | o---|--+ | o---|--+ | null| +-----+ +-----+ +-----+ +-----+ +-----+ /|\ Node | +-----+ | cursor | o--|------------------------------------+ +-----+ (c) Starting again with the original picture, write a single line of code that creates this picture. Node Node Node +-----+ +-----+ +-----+ head | o--|------>| 0 | +-->| 8 | +-----+ +-----+ | +-----+ | o---|--+ | o---| +-----+ +-----+ /|\ Node | +-----+ | cursor | o--|----------+ +-----+ (d) Starting again with the original picture, write a single line of code that creates this picture. Node Node Node Node Node +-----+ +-----+ +-----+ +-----+ +-----+ head | o--|------>| 6 | +-->| 9 | +-->| 0 | +-->| 8 | +-----+ +-----+ | +-----+ | +-----+ | +-----+ | o---|--+ | o---|--+ | o---|--+ | null| +-----+ +-----+ +-----+ +-----+ /|\ Node | +-----+ | cursor | o--|------------------------------------+ +-----+ Problem 5) Here is an implementation of a generic Node class. class Node { private E data; private Node link; public Node(E data, Node link) { this.data = data; this.link = link; } public E getData( ) { return data; } public Node getLink( ) { return link; } public void setData(E data) { this.data = data; } public void setLink(Node link) { this.link = link; } } Use this generic Node class to implement the following static methods. (a) Write an implementation of the static method public static int countZeros(Node head) that will count the number of zeros that occur in the given linked list of ints. (b) Write an implementation of the static method public static String list2String(Node head) that returns a String representation of the linked list referred to by the parameter head. If the linked list is empty, the String representation should be "[]" (two square brackets next to each other). If the linked list is not empty, the String representation should look like this, "[ 3 52 0 2 -4 16 ]", with a space before and after each entry of the list. (c) Write an implementation of the static method public static Node getThirdNode(Node head) that returns a reference to the second node after the node referred to by the parameter head (you can assume that node must exist). (d) Write an implementation of the static method public static void duplicateNode(Node head) that inserts into the linked list a copy of the node referred to by head right after head. (e) Write an implementation of the static method public static void set(E element, int i, Node head) that modifies the linked list referred to by the parameter head so that the i'th node in the linked list has its data changed to element (the 0'th node is the node referred to by head). If there is no i'th node in the linked list, then the list is not modified. Problem 6) Once again using the generic Node class from the previous problem, consider the following three lines of code. Node hd = new Node<>(4,new Node<>(7,new Node<>(5,new Node<>(3,null)))); Node ptr = hd.getLink().getLink(); hd.getLink().setLink( new Node<>(22, null) ); (a) Draw a picture of Java's memory after the first line above has been executed. Be sure to include what data is in each node. (b) Draw a picture of Java's memory after the first and second lines above have been executed. (c) Draw a picture of Java's memory after all three lines above have been executed. (d) What would be a String representation for the linked list referred to by hd? (e) What would be a String representation for the linked list referred to by ptr? (f) What would be a String representation for the linked list referred to by ptr after executing the following line (which should be executed after the above three lines)? ptr.getLink().setLink( hd.getLink() ); Problem 7) Here is a version of the List abstract class. public abstract class List { public abstract int size(); public abstract E get(int index); public abstract void set(int index, E item); public abstract void add(int index, E item); public abstract E remove(int index); public abstract void clear(); public abstract int indexOf(E item); public abstract int lastIndexOf(E item); /** Add all the elements from thatList to the end of this list. */ public void append(List thatList) { } /** Remove from this list any element from thatList that is in this list. More precisely, for each element of thatList, if that element is in this list, remove one occurrence of that element from this list. */ public void remove(List thatList) { } } (a) Use the abstract methods in the interface to implement the append() method which adds all the elements from thatList to the end of this list. The List thatList should not be modified. (b) Use the abstract methods in the interface to implement the remove(List thatList) method which removes from this list any element from thatList that is in this list. Problem 8) The names that the textbook gives to the three main methods in its Dictionary abstract-data-type are search, insert, and delete. a) Give two other common names for insert. What name does Java use? b) Give three other common names for search. What name does Java use? c) Give one other common name for delete. What name does Java use? d) Give three other common names for Dictionary. Which names does Java use? Use the following implementation of HashTable to answer the next few questions. http://cs.pnw.edu/~rlkraft/cs51510/for-class/HashTableDemo.java Problem 9) Below is a picture of a HashTable with 8 buckets and 3 key-value pairs. Both the keys and the values in this hash table are strings. The hash function is the length of the key string. The hash table is using open addressing as its collision policy. StringHashTable names = new StringHashTable<>(8); names.insert("Ed", "cat"); names.insert("Bradley", "dog"); names.insert("Harry", "cat"); System.out.println(names); [0] [1] [2] [3] [4] [5] [6] [7] Draw a picture of what this hash table looks like after the following three insertions. names.insert("Katherine", "dog"); names.insert("Larry", "cat"); names.insert("Juliet", "dog"); System.out.println(names); (Note: These three insertions will not cause a rehash of the table. The next insertion will cause the rehash.) Problem 10) The following code creates a HashTable with 4 buckets and 3 key-value pairs. Both the keys and the values in this hash table are strings. The hash function is the length of the key string. The hash table is using open addressing as its collision policy. StringHashTable names = new StringHashTable<>(4); names.insert("Sue", "cat"); names.insert("Bradley", "cat"); names.insert("Fred", "dog"); System.out.println(names); (a) Draw a picture of what this hash table looks like after executing the three insertions. (Note: These three insertions will not cause a rehash of the table. The next insertion will cause a rehash.) (b) Draw a picture of what this hash table looks like after the following insertion. Note: This insertion will cause the hash table to be rehashed. Remember that the new insertion is done after the rehashing. names.insert("Nina", "dog"); System.out.println(names); Problem 11) Below is a picture of a HashTable with 4 buckets and 3 key-value pairs. Both the keys and the values in this hash table are strings. The hash function is the length of the key string. The hash table is using chaining as its collision policy. StringHashTable names = new StringHashTable<>(4); names.insert("Ed", "cat"); names.insert("Juliet", "dog"); names.insert("Harry", "cat"); System.out.println(names); [0] null [1] [2] ==> [3] null (a) Draw a picture of what this hash table looks like after the following three insertions. Note: The first of these insertions will cause the hash table to be rehashed. names.insert("Samantha", "dog"); names.insert("Larry", "cat"); names.insert("Alyssa", "dog"); System.out.println(names); (b) The following insertion will cause the hash table to be rehashed again. Draw a picture of what the hash table looks like after this insertion. names.insert("Susannah", "dog"); System.out.println(names); Problem 12) Write the code that would create the following hash table. Both the keys and the values in this hash table are strings. The hash function is the length of the key string. The hash table is using open addressing as its collision policy. [0] [1] [2] [3] [4] [5] Problem 13) Write the code that would create the following hash table. Both the keys and the values in this hash table are strings. The hash function is the length of the key string. The hash table is using chaining as its collision policy. [0] null [1] ==> [2] ==> [3] null [4] null [5] null /* Use this definition of a binary tree node to solve the next few problems. */ class Node // Generic binary tree node. { public E data; public Node left; public Node right; public Node(E data, Node left, Node right) { this.data = data; this.left = left; this.right = right; } public Node(E data) // Leaf node constructor. { this.data = data; this.left = null; this.right = null; } @Override public String toString() { return (null==left && null==right)? "" + data : "(" + data + ", " + ((null == left)? "()" : left.toString()) + ", " // recursion + ((null == right)? "()" : right.toString()) + ")"; // recursion } } Problem 14) Use the above Node class to write a single line of code that instantiates this binary tree. 10 / \ 12 14 \ 16 Node tree1 = System.out.println( tree1 ); Problem 15) Use the above Node class to write a single line of code that instantiates the binary tree that produces this string when the tree is printed. (6 (9 5 3) 2) Node tree2 = System.out.println( tree2 ); Problem 16) Given this binary tree, 2 / \ 8 3 / \ 5 9 write a single line of code that manipulates that tree to become the following tree. 2 / \ 8 7 / 3 / \ 5 9 Node tree3 = new Node<>(2, new Node<>(8, new Node<>(3, new Node<>(5), new Node<>(9))); System.out.println( tree3 ); tree3. System.out.println( tree3 );