Quiz 1 Answers

When a recursive method calls itself repeatedly, the variables' values local to the method are kept track of on the call stack. As the name implies, the data structure used to hold this information is a stack. Why is a stack used for this and not a queue?

Answer: When a method calls another method, all local variable values in the calling method must be saved and restored when the called method returns. Since we want the most recent calling function restored on returning, this information should be stored in a stack, which is a last-in, first-out data structure.


Here is a Node class that is used by a LinkedList class that has a head and tail reference.

public class Node {
  public char item;
  public Node next;
  public Node(char i, Node n) {
    item = i;
    next = n;
  }
}

Assume the nodes in the diagram are all created and their member variables have correct values, and that head and tail are of type Node and have correct values. Write one to three Java statements that insert the character 'a' at the front of the list.

Answer:

head = new Node('a',head);

To place the new node at the end of the list, do

Node newNode = new Node('a',null);
tail.next = newNode;
tail = newNode;

Write all strings of length 3 or less represented by the grammar

Answer: , ab, ac


Complete this inductive proof that . Circle the step where the inductive hypothesis is used.

  • Basis: : TRUE.
  • Inductive Hypothesis: :
  • Inductive Step: Prove

Answer:


Make changes to the following code so that MyStuff is a generic class with data being of generic type.

Answer:

public class MyStuff<T>   {

    T  data;

    int     count;

    public static void main(String [] args) {

        MyStuff<String>  x   =   new   MyStuff<String>   ();
    }
}
Recent changes RSS feed CC Attribution-Share Alike 3.0 Unported Driven by DokuWiki