Quiz 04 sample questions

Here are examples of the kinds of questions you may be asked:

  • Write a method static <K, V> void addToMultiMap(Map<K, List<V>> map, K key, V value). addToMultiMap must add value to the end of the list associated with the given key, creating the list if necessary.

    For full credit, your method must include generic types, and must not contain unnecessary method calls or loops, even if they do not otherwise impact correctness. You may assume Map, List, HashMap, and ArrayList are correctly imported from java.util. You may not import other classes.

  • Draw a undirected graph with four vertices. The graph must be connected but not complete.

  • Some graphs are dense (that is, most possible edges are present) and some are sparse (most possible edges are absent). For a sparse graph, which representation uses more space on average, an adjacency list or an adjacency matrix?
  • Is depth-first search guaranteed to find a path to a goal in a finite graph? Is the path it finds guaranteed to be (one of) the shortest such paths?
  • Suppose you have an empty stack of integers, and on that stack you execute the following operations:

    • push 1
    • push 2
    • pop
    • push 1
    • push 3
    • push 4
    • pop
    • push 1

    Show the contents of the stack. Clearly indicate the top of the stack.

  • Suppose we have an array-based implementation of a Stack<E>.

    It has two instance variables. The first is a generic array E[] array, which contains the elements of the stack, if any, with the top of the stack in the rightmost cell (that is, the cell with the largest index). For example, if the stack holds two values, E[0] holds one, and E[1] holds the other, and the value in E[1] is at the top of the stack. The other is int top, which contains the index of the current top of the stack, or -1 if the stack is empty.

    Write a push method with the signature public void push(E e) throws StackOverflowException. You can assume the existence of an isFull method and StackOverflowException class.

    You can check your answers here.