Computer Science 2310
Spring 2005
Solutions to practice questions for quiz 1

This will be a closed book quiz. You may bring one 8.5x11 page of prepared notes, written on both sides.

  1. Write a clearly legible T to the left of each of the following statements that is true and a clearly legible F to the left of each that is false.

    1. A function/method contract usually cannot be written until after the function is written. False.
    2. The data type of real numbers is called real in Java. False. It is called double.
    3. The Java type boolean contains just two values, true and false. True.

  2. Function f2 is shown below, with part of its definition not shown, and replaced by three dots. There are no breaks or returns in the part that is not shown, and nothing is printed there. It is known that f2 prints something at the marked line when it runs. What does it print?

    1. i = 24
    2. i = 25
    3. i = 26
    4. It is impossible to say without more information.
          public static void f2()
          {
            int i = 100;
            while(i != 25) {
              ...
              i = i + 1;
            }
            System.out.println("i = " + i);    // This line prints what?
          }
    

  3. The following loop sets variable m to

    1. 1 + ... + 9
    2. 1 + ... + 10
    3. 1 + ... + 11
    4. 1 + ... + 12
         int m,n;
         n = 0;
         m = 0;
         while(n < 10) {
          n++;
          m = m + n;
         }
    

  4. What is the value of Java expression 5+9*8-6?

    1. 28
    2. 71
    3. 83
    4. 106

  5. What is printed by the following Java method when it is run?

           public static void four()
           {
             int x,y,z,w;
             boolean b;
             x = 45;
             y = 2 * x + x * 3;
             z = x / 2;
             w = x % 2;
             b = x < y;
             System.out.println("x = " + x + " y = " + y + " z = " + z
    		            + " w = " + w + " b = " + b);
           }
    

    Answer: x = 45 y = 225 z = 22 w = 1 b = true

  6. The geometric mean of two numbers x and y is the square root of the product of x and y. Write a static Java method called geometricMean that returns the geometric mean of its two parameters. Note. You can use function Math.sqrt to compute square roots. Math.sqrt(x) is the square root of x.

          import java.lang.*;  // Allows Math.sqrt.
    
          public static double geometricMean(double x, double y)  
          {
            return Math.sqrt(x*y);
          }
    

  7. Using method geometricMean from the previous exercise, write a program fragment that sets variable z to the geometric mean of variable r and twice variable s. Assume that r and s have type double.

         z = geometricMean(r, 2*s);
      

  8. Static method f is defined as follows.

         public static int f(int x)
         {
           int k = 1;
           while(k <= x) {
             k = k + k;
           }
           return k;
         }
    
    What are f(3) and f(f(3))?
       f(3)     =   4
    
    
       f(f(3))  =   8