Exercises from Chapter 3 of the Java Developer's Resource

The exercises here are taken from my book The Java Developer's Resource.

Quiz

  1. What does the following program print?
    
    // This is the Hello Rusty program in Java
    
    class HelloRusty {
    
        public static void main (String args[]) {
          
          String name = "Rusty";
    
          /* Now let's say hello */
          System.out.println("Hello + name");
    
      }
    
    } 
    
  2. What's wrong with this program?
    
    // This is the Hello program in Java
    
    class Hello {
    
        public static void main (String args[]) {
        
          int i;
        
          System.out.print("Hello ");  // Say Hello
          i = 0;                       // Initialize loop counter
          while (i <= args.length) {  // Test and Loop
            System.out.print(args[i] + " ");  
            i = i + 1;                 // Increment Loop Counter
          }
          System.out.println();        // Finish the line
      }
    
    }
    
  3. What happens if you don't give Program 3.10 any command line arguments? You aren't testing the number of command line arguments anymore so why isn't an ArrayIndexOutOfBoundsException thrown?

  4. For math whizzes only: I lied. In certain interpretations of certain number systems the statement i = i + 1 does have a valid solution for i. What is it?

Exercises

  1. Write a program that prints all the integers between 0 and 36.

  2. Imagine you need to open a standard combination dial lock but don't know the combination and don't have a pair of bolt cutters. Write a program that that prints all possible combinations so you can print them on a piece of paper and check off each one as you try it. Assume the numbers on the dial range from zero to thirty-six and three numbers in sequence are needed to open the lock.

  3. Suppose the lock isn't a very good one and any number that's no more than one away from the correct number will also work. In other words if the combination is 17-6-32 then 18-5-31 will also open the lock. Write a program that prints the mininum number of combinations you need to try to guarantee opening the lock.

  4. The Fibonacci numbers are defined as follows:

    The zeroth Fibonacci number is 1.
    The first Fibonacci number is also 1.
    The second Fibonacci number is 1 + 1 = 2.
    The third Fibonacci number is 1 + 2 = 3.

    In other words, except for the first two numbers each Fibonacci number is the sum of the two previous numbers. Write a program that prints the first 20 Fibonacci numbers.


[ Exercises | Cafe Au Lait | Books | Trade Shows | Links | FAQ | Tutorial | User Groups ]

Copyright 1996, 1997 Elliotte Rusty Harold
elharo@sunsite.unc.edu
Last Modified January 18, 1997