Review Exercises - Class 5

For questions 1-3 describe the output of each of the Java code segments. Try and figure this out with pencil and paper and check your answers by running them in DrJava.
  1.      int i;
         i = 0;
         while (i < 5) {
            StdOut.print(i);
            i = i + 1;
         }
       
  2.      int i;
         i = 0;
         while (i < 10) {
            StdOut.println(i);
            i = i + 2;
         }
       
  3.      int i;
         i = 0;
         while (i < 10) {
            StdOut.print(i + "  ");
            StdOut.println((int) Math.pow(i,2));
            i = i + 1;
         }  
       
  4. This code tries to print out all the integers up to 100. Something is wrong. What is the bug?
         int i;
         i = 0;
         while (i <= 100) {
            StdOut.println(i);
         }
       
  5. How many asterisks get printed by the following code segment?
          int i;
          i = 10;
          while (i <= 77) {
             StdOut.print("*");
             i = i + 1;
          }
       
  6. Write a Java while-loop that prints numbers in reverse starting from 50 going down to -2.
  7. Answer questions about the following code segment.
        int num, count, sum;
        count = 0;
        sum   = 0;
        num = StdIn.readInt();
        
        while (num != -999) {
          StdOut.print("Enter a number: ");
          sum = sum + num;
          count = count + 1;
          num = StdIn.readInt();
        }    
        StdOut.print((double) sum/count);
    
  8. Use a while loop to generate 100 filled circles in random locations in a 500-by-500 window. Circles should have random colors as well as random diameters between 20 and 50 pixels.