Review Exercises (Set B2)

  1. In the method below, how many parameters are there?

    public static double CountMyParameters(int a) {
        double x = a * 2.0;
        double y = x * 3.0;
        double z = y * 4.0;
        return x+y+z;
    }
    
    1
  2. The method defined below has how many local variables?

    public double funStuff(int num1, int num2, int num3) {
          int sum = num1 + num2 + num3;
          int product = num1 * num2 * num3;
          return sum + product;
    }
    
    2
  3. Explain the error in the code below:

    public boolean isGoodInvestment(double principal) {
        double balance = principal;
        for (int year = 0; year < 7; year++) {
            principal *= 1.10;
            boolean isEnough = (balance > 2*principal); 
        }
        return isEnough;
    }
    
    isEnough is out of scope
  4. What will the following code print?

    public class Main {    
        public static void main(String[] args) {
              int[] a = new int[3];
              for (int i = 0; i < 3; i++) {
                   System.out.print(a[i]);
                   a[i] = 3 - i;
              }
              System.out.println();
              for (int i = 0; i < 3; i++) {
                   System.out.print(a[i]);
              }
        }
    }
    
    000
    321
    
  5. What will the following code print?

    public class Main {    
        public static void main(String[] args) {
            int[][] a = new int[3][3];
            for (int i=0; i<3; i++) {
                for (int j=0; j<3; j++) {
                    a[i][j] = (i+2*j) % 3;
                }
            }
            for (int k=0; k<3; k++) {
                System.out.print(a[0][k]);
            }
            System.out.println(a[0].length);
        }
    }
    
    0213
    
  6. What will the following code print?

    public class Main {    
        
        public static void doSomething(int[] a, int b) {
    
            for (int i = 0; i < 5; i++) {
                a[i]++;
            }
    
            b++;
        }
        
        public static void main(String[] args) {
    
            int[] list = {1, 2, 3, 4, 5};
            int num = 6;
    
            doSomething(list, num);
    
            for (int i = 0; i < 5; i++) {
                System.out.print(list[i]);
            }
            System.out.println();
    
            System.out.println(num);
        }
    }
    
    23456
    6
    
  7. What does the following code print?

    int[] a = {2,3,6,8,5,9,1,7};
    int p = 0;
    for (int i=0; i < a.length; i++) {
        if (a[p] > a[i]) {
            p = i;
        }
    }
    System.out.println(p);
    
    6
  8. The selection sort orders list or array of numbers (or some other comparable type) by performing a number of "swaps". Given the list: 3,5,9,8,4,6,1 -- put the list in order using a selection sort. Show the state of the list after each "swap", from the first swap made to the last, in that order.

    Assuming minimums are swapped to the beginning, you should get the following:

    3,5,9,8,4,6,1
    1,5,9,8,4,6,3
    1,3,9,8,4,6,5
    1,3,4,8,9,6,5
    1,3,4,5,9,6,8
    1,3,4,5,6,9,8
    1,3,4,5,6,8,9
    
  9. The insertion sort orders a list or array of numbers (or some other comparable type) by performing a number of "insertions". Given the list: 3,5,9,8,4,6,1 -- put the list in order using an insertion sort. Show the state of the list after each "insertion", from the first insertion made to the last, in that order.

    3,5,9,8,4,6,1
    3,5,8,9,4,6,1
    3,4,5,8,9,6,1
    3,4,5,6,8,9,1
    1,3,4,5,6,8,9
    
  10. Examine the following method. If there is an error in it, explain what it is. If no such error exists, what will the method return if invoked?

    public boolean isItZero() {
        int someNumber;
        return (someNumber == 0);
    }
    

    The local variable someNumber was not initialized.
  11. Suppose one uses an iterative version of a binary search to determine if 13 is present in the following array {1, 2, 4, 5, 7, 9, 12, 15, 18, 19, 23}. List the values of the low, mid, and high variables seen over the course of the algorithm (in the order that they are seen), as well as the return value for the method.

    public static int binarySearch(int[] list, int key) {
       int low = 0;
       int high = list.length - 1;
    
       while (high >= low) { 
          int mid = (low + high) / 2;  
          if (key < list[mid])          
             high = mid - 1;                                   
          else if (key > list[mid])    
             low = mid + 1;                               
          else
             return mid;               
       }
    
       return -1 - low;  
    }
    
    low   mid   high
     0     5     10
     6     8     7
     7     6     6
           7
    
    returns -8
    
  12. What does the following program print?

    public class Stuff {
    
       public static void f(int x) {
         x = 444;
       }
    
       public static void g(int[] x) {
          x[0] = 777;
       }
    
       public static void main(String[] args) {
          int[] a = {1, 2, 3};
          System.out.println(a[0]);
          f(a[0]);
          System.out.println(a[0]);
          g(a);
          System.out.println(a[0]);
       }
    }
    
    1
    1
    777
    
  13. What does the following code print?

    public class Useful {
       public static double dp(double[] a, double[] b) {
          double r = 0;
          for (int i = 0; i < a.length; i++)
              r = r + a[i]*b[i];
          return r;
       }
    
       public static void main(String[] args) {
          double[] c = {2, 4, 3};
          double[] d = {3, 2, 5};
          System.out.println(dp(c,d));
       }
    }
    
    29.0
  14. What does the following code print?

    public class ItsAMystery {
    
       public static void main(String[] args) {
          int[] a = {1,2,3,4};
          mystery(a, 1);
          for (int i=0; i < a.length; i++) {
             System.out.print(a[i]);
          }
       }
    
       public static void mystery(int[] a, int m) {
          int n = a.length;
    
          for(int i=0; i<n; i++) {
             int tmp = a[i];
             int j = (i+m)%n;
             a[i] = a[j];
             a[j] = tmp;
          }
       }
    }
    
    1342
  15. Overloaded methods can be distinguished by different _____________? (Fill in the blank)

    method signatures
  16. What will the following code print?

    public class ArrayFun {
    
       public static void main(String[] args) {
        int[] a = {1,2,3};
        int[] b = {4,5,6};
        int[] c = {7,8,9};
        int[][] d = {a,b,c};
        int[][] e = new int[3][3];
    
        for (int i = 0; i < 3; i++) {
           e[i][2] = d[i][2];
        }
    
        for (int i = 0; i < 3; i++) {
           for (int j = 0; j < 3; j++) {
              System.out.print(d[i][j] + e[i][j] + " ");
           }
           System.out.println();
        }
       }
    }
    
    1 2 6
    4 5 12
    7 8 18
    
  17. Explain what it means when a method declares its return type as void

    Methods with return type void do not return any output.
  18. What does the following code print?

    public class MoreArrayFun {
    
       public static void main(String[] args) {
        int[] a = {1,2,3};
        int[] b = {4,5,6};
    
        int[] t = a;
        a[0] = 9;
        a = b;
        b = t;
    
        for (int i = 0; i < 3; i++) {
           System.out.println(a[i] + " " + b[i]);
        };
       }
    }
    
    4 9
    5 2
    6 3
    
  19. The selection sort orders list or array of numbers (or some other comparable type) by performing a number of "swaps". Given the list: 1,9,8,3,6,2 -- put the list in order using a selection sort. Show the state of the list after each "swap", from the first swap made to the last, in that order.

    Presuming one swaps minimums to the front, one gets the following:
    1 9 8 3 6 2
    1 2 8 3 6 9
    1 2 3 8 6 9
    1 2 3 6 8 9
    
  20. The insertion sort orders a list or array of numbers (or some other comparable type) by performing a number of "insertions". Given the list: 1,9,8,3,6,2 -- put the list in order using an insertion sort. Show the state of the list after each "insertion", from the first insertion made to the last, in that order.

    1 9 8 3 6 2
    1 8 9 3 6 2
    1 3 8 9 6 2
    1 3 6 8 9 2
    1 2 3 6 8 9
    
  21. Suppose one uses a binary search to determine if 6 is present in the following array {1, 2, 4, 5, 7, 9, 12, 15, 18, 19, 23}. List the values of the low, mid, and high variables seen over the course of the algorithm (in the order that they are seen), as well as the return value for the method.

    public static int binarySearch(int[] list, int key) {
       int low = 0;
       int high = list.length - 1;
    
       while (high >= low) {
          int mid = (low + high) / 2;
          if (key < list[mid])
             high = mid - 1;
          else if (key > list[mid])
             low = mid + 1;
          else
             return mid;
       }
    
       return -1 - low;
    }
    
    lo   mid   high
     0    5     10
     3    2     4
     4    3     3
          4
    
    return value:
    -5