Review for Final



1.  Compute the value of each of the following expressions or write INVALID

   a.  20 % 4
   b.  20/4
   c.  8 % 16 + 16/8
   d.  5/(4%4)
   e.  1/2

2.  Write the following expressions as valid Java expressions

   a.  a + d * c
       ---------   + b
          2

   b.  a + b
       -----
         c

3.  Evaluate each of the following logical expressions and indicate if they are TRUE (T)
    or FALSE (F).

   a.  (5>2) && !(2==5)             T       F

   b.  (5>2) || !(2==5)             T       F

   c.  3!=3 && 7<8 || 2<3           T       F




4.  What will the following code segments print?
   a.
      int1=120;
      int2=30;
      if ((int1>100) && (int2 =50))
         int3 = int1 + int2;
      else
         int3= int1 - int2;
      System.out.println(int1 + " " + int2 + " " + int3);

   b.
      angle=0;
      if (angle > 5)
         angle = angle * 5;
      else
         if (angle > 2)
            angle = angle + 10;
      System.out.println(angle);

   c.
      count=1;
      while (count <=4) {
         System.out.println(count);
         count++;
      }

   d.
      count=1;
      while (count <=4)
         System.out.println(count);
 	     count++;

   e.
      count=1;
      while (count <=4) {
         count++;
         System.out.println(count);
      }


5.  Given the method definition what is the value of the expression Mystery(4.2)?

      public static int Mystery(double someVal) {
         if (someVal > 2.0)
            return 3 * (int)someVal;
         else
         return 0;
      }



6.  What is the output of the following code fragment.
      int1=4;
      switch (int1) {
         case 1:  System.out.println(1);
                  break;
         case 2:  System.out.println(2);
                  break;
         case 3:  System.out.println(3);
                  break;
         case 4: System.out.println(4);
         case 5: System.out.println(5);
      }



7.  What is the output of the following code fragment?

      limit = 8;
      System.out.print("H");
      loopcount = 10;
      do {
         System.out.print("E");
         loopcount++;
      }
      while (loopcount <= limit);
      System.out.println("LP");

8.  Write the for loop that is equivalent to the following while loop.

      count = -5;
      sum=0;
      while (count <=15) {
         sum = sum + count;
         count++;
      }


9.  Write the for loop that will sum the integers between 0 and 100, inclusive.


10.  Write the for loop that will read 25 integer values from the file "datain.dat".
     Compute the average of the values read.


11.  Write a complete Java  program that reads a salesman’s ID number and amount sold.
     Write out the ID, followed by "GETS AWARD" if amount sold is at least $50,000,
     "DOING OK" if amount sold is between $25,000 and $50,000 and "NEEDS PEP TALK"
     if amount sold is $25,000 or less.


12.  Write a method that reads a file "datain.dat", of integers until a value of -100 is
     found.  Return the average of the integers read.


13.  Write a program segment that counts and displays the number of times a zero occurs
     in a file "datain.dat" of 50 integers.


14.  Write a method that reads a file "datain.dat" of integer values representing temperatures
     and returns the highest temperature read.


15.  Write a method SUMARRAYS that receives two one-dimensional parallel arrays LIST1 and
     LIST2 of integers as parameters and returns the sum of the all of the values in LIST1
     plus the sum of all of the values in LIST2.


16.  Write the statements to call SUMARRAYS. Pass arrays ARR1 and ARR2 as actual parms,
     and print the sum that is returned by SUMARRAYS.


17.  Write a method NEGATIVE that receives an array of integers, LIST, as a parameter and
     returns the index of the first negative number in array LIST.  If there are no negative
     numbers in the array, return -1.


18.  Write a segment of code to find and display the largest and smallest value in the integer
     array LIST.


19.  Write a method REVERSE that has two parms: an array of ints and an int for the number of
     values stored in the array. REVERSE should reverse the values in the array. For example,
     if the array contains
          2 4 6 8 10
     the method should change it to
          10 8 6 4 2


20.  Write the statements to create an array of 100 ints, create a Scanner for the file
     "datain.dat", and read the values from the file and store them in the array. Read
     until end of file. You can assume that the file will not contain more than 100 values.
     After reading, call the method REVERSE written in the previous problem.


21.  Write a segment of code to find and display the largest and smallest values in a
     two-dimensional M X N integer array TABLE.


22.  Write the method FIND with parms TABLE (a 2D integer array) and ROW (an int). FIND
     will return the column subscript of the first occurrence of 100 in row ROW of TABLE.
     If 100 does not occur in the row ROW, return -1.


23.  Write the method FIND with parms TABLE (an M x N integer array) and COL (an int). FIND
     will return the row subscript of the first occurrence of 100 in column COL of TABLE.
     If 100 does not occur in the column COL, return -1.


24.  Write the method YES_OR_NO to return true if 100 occurs in the 2D integer array TABLE,
     and false, otherwise.


25.  Write a statement to call method YES_OR_NO defined above.


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

© Copyright Emmi Schatz 2013