Review For Final - Answers


1.
   a. 0
   b. 5
   c. 10
   d. invalid - cannot divide by zero
   e.  0


2.
   a.   (a+d*c)/2 + b
   b.   (a+b)/c


3.
   a.  T
   b.  T
   c.  T


4.
   a.  invalid (assignment used instead of equality)
   b.  0
   c.  1
       2
       3
       4
   d.  infinite loop
   e.  2
       3
       4
       5


5.   12


6.   4
     5


7.   HELP


8.   for (count = -5, sum = 0 ; count <= 15 ; count++)
        sum += count;


9.   for (i = 0, sum = 0; i <= 100 ; i ++)
        sum += i;


10.  File file = new file("datain.dat");
     Scanner inp = new Scanner(file);
     int i, value, sum=0;
     double average;
     for(i = 1 ; i <= 25; i++) {
        value = inp.nextInt();
        sum += value;
     }
     average = sum / 25.0;


11.
     import java.util.Scanner;
     public class Review {
        public static void main(String[] args) {
           int sales_ID;
           double amount_sold;
           Scanner keyboard = new Scanner(System.in);

           sales_ID=keyboard.nextInt();
           amount_sold=keyboard.nextInt();
           System.out.print(sales_ID + "  ");
           if (amount_sold >= 50000)
              System.out.println("GETS AWARD");
           else
              if (amount_sold >= 25000)
                 System.out.println("DOING OKAY");
              else
                 System.out.println("NEEDS PEP TALK");
        }
     }


12.
     public static double Average() {
        File file = new File("datain.dat");
        Scanner inp = new Scanner(file);
        int count = 0, sum = 0, value;
        value = inp.nextInt();
        while (value != -100) {
           count++;
           sum += value;
           value=inp.nextInt();
        }
        if (count != 0)
           return (double) sum/ count);
        else
           return 0.0;
     }


13.
     File file = new File("datain.dat");
     Scanner inp = new Scanner(file);
     int count=0, sum=0, value, i;
     for (i = 1 ; i <= 50 ; i++) {
        value=inp.nextInt():
        if (value ==0)
           count++;
     }
     System.out.println(count);


14.
     import java.util.Scanner;
     import jave.io.*;
     public static void int High() {
        File file = new File("datain.dat");
        Scanner inp = new Scanner(file);
        int value, highest;
        highest = Integer.MIN_VALUE;   // or   highest = some very low number  (-500)
        while (inp.hasNext()) {
           value=inp.nextInt();
           if (value>highest)
              highest = value;
        }
        return highest;
     }


15.
     public static int SUMARRAYS(int[] LIST1, int[] LIST2) {
        int i, sum=0;
        for (i = 0 ; i < LIST1.length ; i++)
           sum = sum + LIST1[i] + LIST2[i];
        return sum;
     }


16.
     int total = SUMARRAYS(ARR1, ARR2);
     System.out.println("The total is " + total);


17.
     public static int NEGATIVE(int[] LIST) {
        int i;
        for (i = 0 ; i < LIST.length ; i++)
           if (LIST[i] < 0)
              return i;
        return -1;
     }


18.
     largest = LIST[0];
     smallest = LIST[0];
     for (i = 1 ; i < LIST.length ; i++) {
        if (LIST[i] < smallest)
           smallest = LIST[i];
        if (LIST[i] > largest)
           largest = LIST[i];
     }
     System.out.println("The value of smallest in LIST is " + smallest);
     System.out.println("The value of largest in LIST is " + largest);


19.
     void REVERSE(int[] arr, int size) {
        int i;
        int j;
        int temp;
        for (i = 0, j = size - 1 ; i < j ; i++, j--) {
           temp = arr[i];
           arr[i] = arr[j];
           arr[j] = temp;
        }
     }


20.  int[] nums = int[100];
     File inf = new File("datain.dat");
     Scanner input = new Scanner(inf);
     int i = 0;
     while (input.hasNext()) {
        nums[i] = input.nextInt();
        i++;
     }
     REVERSE(nums,i);


21.
     largest = TABLE[0][0];
     smallest=TABLE[0][0];
     for (i = 0 ; i < M ; i++)
        for (j=0;j largest)
              largest = TABLE[i][j];
     }
     System.out.println("The value of smallest in TABLE is " + smallest);
     System.out.println("The value of largest in TABLE is " + largest);


22.
     public static int FIND(int[][] TABLE, int ROW) {
        int j;
        for (j = 0 ; j < TABLE[ROW].length ; j++)
           if (TABLE[ROW][j] == 100)
              return j;
        return -1;
     }


23.  public static int FIND(int[][] TABLE, int COL) {
        int i;
        for (i = 0 ; i < TABLE.length ; i++)
           if (TABLE[i][COL] == 100)
              return i;
        return -1;
     }


24.
     public static boolean YES_OR_NO(int[][] TABLE) {
        int i,j;
        for (i = 0 ; i < TABLE.length ; i++)
           for(j = 0 ; j < TABLE[i].length ; j++)
              if (TABLE[i][j] == 100)
                 return true;
        return false;
     }


25.
     if (YES_OR_NO(TABLE))
        System.out.println("100 occurs in the array");
     else
        System.out.println("100 does not occur in the array");



Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2013