Second Summer 2002

Chapter Six: Review Exercises

  1. Which loop statements does Java support? Give simple rules when to use each loop type.

  2. What does the following code print?
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) 
        System.out.print(i * j % 10);

  3. How often (how many times) do the following loops execute? Assume that i is not changed in the loop body.
    for (i =   1; i <= 10; i++) ...
    for (i =   0; i <  10; i++) ...
    for (i =  10; i >   0; i--) ... 
    for (i = -10; i <= 10; i++) ...
    for (i =  10; i >=  0; i++) ...
    for (i = -10; i <= 10; i = i + 2) ... 
    for (i = -10; i <= 10; i = i + 3) ...

  4. Rewrite the following for loop into a while loop.
    int s = 0; 
    for (int i = 1; i <= 10; i++) s = s + 1; 

  5. Rewrite the following do loop into a while loop.
    int n = 1; 
    double x = 0; 
    double s; 
    do { 
      s = 1.0 / (n * n); 
      x = x + s; 
    } while (s > 0.01); 

  6. What is an infinite loop? On your computer, how can you terminate a program that executes an infinite loop?

  7. There are two ways to supply input to -- describe both methods. Explain how the "end of file" is signaled in both cases.

  8. In DOS/Windows and Unix, there is no special "end of file" character stored in a file. Verify that statement by producing a file with known character count, for example, a file consisting of the following three lines:
    Then look at the directory listing. How many characters does the file contain? Remember to count the newline characters. (In DOS you may be surprised that the count is not what you expect. DOS text files store each newline as a two-character sequence. The input readers and output streams automatically translate between this carriage return / line feed sequence used by the files and the '\n' character used by Java programs, so you don't need to worry about it.) Why does this prove that there is no "end of file" character? Why do you nevertheless need to type Ctrl+Z / Ctrl+D to end console input?

  9. How can you read input from

  10. Show how to use a string tokenizer to break up the string
    "Hello, cruel world!"
    into tokens. What are the resulting tokens?

  11. Give a strategy for reading input of the form
    name of bridge length of bridge
    Here the name of the bridge can be a single word ("Brooklyn") or consist of several words ("Golden Gate"). The length is a floating-point number.

  12. What is a "loop and a half". Give three strategies to implement the following "loop and a half":
    loop {
      read employee name 
      if not OK, exit loop 
      read employee salary
      if not OK, exit loop 
      give employee 5% raise 
      print employee data 
    Use a boolean variable, a break statement, and a method with multiple return statemtens. Which of these three approaches do you find clearest?

  13. What is a sentinel value? Give simple rules when it is better to use a sentinel value and when it is better to use the end of the input file to denote the end of the data sequence.
    Hint: consider the number of data sets and the origin of the data (keyboard input vs. file input).

  14. How would you use a random number generator to simulate the drawing of a playing card?

  15. What is an "off by one" error? Give an example from your own programming experience.

  16. Give an example of a for loop in which symmetric bounds are more natural.
    Give an example of a for loop in which asymmetric bounds are more natural.

  17. What are nested loops.
    Give an example where a nested loop is typically used.

Last updated: Jun 16, 2002 by Adrian German for A201