Answers to Self-Test Questions

  1. 1.

    B
    R
    R
    R

    Note that the 'B' is the first output, not the last output.

  2. 2.

    R
    R
    R
    B

    Note that the 'B' is the last output.

  3. 3.

    /**
    Precondition: n >= 1.
    Displays the symbol '#' n times on one line
    and then advances to the next line.
    */
    public static void displaySharps(int n)
    {
        if (n <= 1)
           System.out.println('#');
        else
        {
           System.out.print('#');
           displaySharps(n − 1);
        }
    }
  4. 4. 6

  5. 5.

    /**
    Precondition: n >= 0
    Returns 10 to the power n.
    */
    public static int computeTenToThe(int n)
    {
        if (n <= 0)
           return 1;
        else
           return computeTenToThe(n − 1) * 10;
    }
  6. 6.

    /**
    Precondition: n can be any int.
    Returns 10 to the power n.
    */
    public static int computeTenToThe(int n)
    {
        if (n == 0)
           return 1;
        else if (n > 0)
           return computeTenToThe(n − ...

Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.