Self-Review Questions

  1. Consider the following mystery method:
    public static void mystery(int level)
    {
       if (level == 0)
       {
          System.out.print("*");
       }
       else
       {
          System.out.print("[");
    
          mystery(level - 1);
    
          System.out.print(",");
    
          mystery(level - 1);
    
          System.out.print("]");
       }
    

    Show the output that would be produced by the following calls

  2. Examine the following code segment
    public class Demo
    {
       public static void main(String[] ags)
       {
          System.out.println( fun(5) );
       }
    
       public static int fun(int n)
       {
          if (n == 0)
             return 1;
          else
             return n + fun(n/2);
       }
    }
    

    and predict the output

  3. Write a recursive method that prints the following pattern?
    #####
    ####
    ###
    ##
    #
    

  4. Write a recursive method that prints the following pattern?
    #
    ##
    ###
    ####
    #####
    

  5. Write a recursive method that removes all nodes with data equal to keyItem
    public void removeAll(Object keyItem, Node current)

  6. Write a recursive method that removes all nodes with data greater than keyItem
    public void removeAllGreaterItems(Comparable keyItem, Node current)

  7. Write a recursive method (you are allowed to have helper-methods)
    public void pattern(int[] input)

    that takes an array of integers input and prints it out input.length times. The print must be in the following specific order: each line starts at a new element and wraps around to the beginning of the array (if needed) until each element has been printed out.

    This sample output occurs when the input is the array {0, 1, 2, 3, 4}:

    0 1 2 3 4
    1 2 3 4 0
    2 3 4 0 1
    3 4 0 1 2
    4 0 1 2 3