Loops and Recursion Test

  1. Read through the code below. Next to only the lines that the computer will evaluate and in which the value of a variable is changed, write the new value of that variable.

    int a = 7;

    int b = a / 3;

    double c = a / b;

    if(a > c * b) {
       
       a -= c;
       
       c++;
       
    } else {
       
       if(b * c > 0) {
          
          b = b + 9;
          
          c = c * .3;
          
       }
       
    }

    c = Math.sqrt(a + c);

    if(a % b == 0) {
       
       a -= b;
       
       b += a;
       
    } else if(b == c) {
       
       b *= 2;
       
       c /= 2;
       
    } else {
       
       c = a + b + c;
       
    }

  2. In physics, if I know my starting position, |si|, my starting velocity, |vi|, and my acceleration, a, I can find my position s after some time t by using this equation:
    Write a method which, given si, vi, and a, will print the position at each second from zero to ten seconds. So, for example, position(10, -4, 2) should print:

    0 s, 10 m
    1 s, 7 m
    2 s, 6 m
    3 s, 7 m
    4 s, 10 m
    5 s, 15 m
    ...


    public void position(double si, double vi, double a) {
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
    }

  3. The method below is supposed to print out all the numbers which are factors of both a and b.

    public void commonFactors(int a, int b) {
       for(int i = 1; i < a; i++) {
          if(a % i == 0 && b % i == 0) {
             System.out.println(i);
          }
       }
    }

    What change needs to be made to this method
    1. Change the condition of the if statement to a % i == 0 || a % b == 0
    2. Change the condition of the for loop to i < a && i < b
    3. Change the condition of the for loop to i <= a
    4. Change the initialization of the for loop to int i = 0
    5. No changes need to be made; the method works fine

  4. Consider the following three code segments:

    1. int i = 1;
      while(i <= 10) {
         System.out.println(i);
         i += 2;
      }


    2. for(int i = 0; i < 5; i++) {
         System.out.println(2 * i + 1);
      }


    3. for(int i = 0; i < 10; i++) {
         i++;
         System.out.println(2 * i + 1);
      }

    Which of these produce the same output?
    1. I and II only
    2. II and III only
    3. I and III only
    4. All three produce the same output
    5. All three produce different output

  5. Consider the following code segment:

    int n = Console.readInt(); // Get input from the user
    n = Math.abs(n);

    while(n >= 2) {
       n = n / 2 - 1;
    }
    System.out.println(n);

    Which of the following is a complete list of the possible outputs of this code?
    1. 0
    2. -1, 0
    3. 0, 1
    4. -1, 1
    5. -1, 0, 1


    tree(4)

    tree(5)


  6. The "tree" shown above has a "trunk" of the length given as a parameter, which then splits into two branches, each of which itself a tree of one smaller size. So, for example, you should be able to see that tree(5) contains two tree(4)'s. All angles are 120° angles, tree(1) is a mere line, and the turtle returns to the start after drawing the tree.

    Fill in the definition of the tree() method below.

    public void tree(int n) {
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
    }


  7. Consider the following method:

    public void mystery(int a, int b) {
       System.out.println(a + " ");
       if(a <= b) {
          mystery(a + 5, b - 1);
       }
    }

    Which of the folloing is output when mystery(0, 16) is called?
    1. 0
    2. 0 5
    3. 0 5 10
    4. 0 5 10 15
    5. 0 5 10 15 20

  8. In implementing a "cheerleader" class, you write this method, which is supposed to print out "2 4 6 8 Who do we appreciate!"

    1 public void cheer(int n) {
    2    if(i != 8) {
    3       i += 2;
    4       cheer(i);
    5       System.out.print(i + " ");
    6    } else {
    7       System.out.print("Who do we appreciate!");
    8    }
    9 }

    This method does not work as expected when you call cheer(0). What change do you need to make?
    1. replace if(i != 8) with if(i <= 8) on Line 2
    2. replace if(i != 8) with if(i == 8) on Line 2
    3. replace if(i != 8) with while(i != 8) on Line 2
    4. Swap Line 4 and Line 5
    5. Swap Line 3 and Line 4

  9. Consider the method below:

    public void splat(String s) {
       if(s.length() < 8) {
          splat(s + s);
       }
       System.out.println(s);
    }

    (The method length() just returns the number of letters in a String) What is output by splat("XX")?
    1. XX

    2. XXXX

    3. XXXXXXXX

    4. XXXXXXXX
      XX


    5. XXXXXXXX
      XXXX
      XX