CSE142 Sample Midterm #2
                       





1. Expressions, 10 points.  For each expression in the left-hand column,
   indicate its value in the right-hand column.  Be sure to list a constant of
   appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes).

        Expression                                Value

        3 * (5 - 2) - 3 - 2 * 2                __________

        4 * 7 % 8 + 132 % 10 + 3 % 4           __________

        27 / 5 / 2 + 3.4 * 2 - 1.1 * 2         __________

        9 + 9 + "9 + 9" + 9 + 9                __________

        19 / 2 / 2.0 + 2.5 * 6 / 2 + 0.5 * 4   __________







2. Parameter Mystery, 12 points.  Consider the following program.

    public class Mystery {
        public static void main(String[] args) {
            String she = "it";
            String it = "her";
            String her = "you";
            String you = "she";

            saying(you, it, you);
            saying(it, her, she);
            saying(she, "you", her);
            saying(it, "him", "fred");
        }

        public static void saying(String it, String her, String she) {
            System.out.println(she + " can't take " + it + " with " + her);
        }
    }

   List below the output produced by this program.


3. If/Else Simulation, 12 points.  Consider the following method.

        public static void ifElseMystery(int a, int b) {
            int c = 2;
            if (a + c < b) {
                c = c + 8;
            } else {
                b = b + 10;
            }
            if (a + c < b) {
                c = c + 8;
            } else {
                b = b + 10;
            }
            System.out.println(b + " " + c);
        }

   For each call below, indicate what output is produced.

        Method Call               Output Produced

        ifElseMystery(4, 15);     _______________

        ifElseMystery(7, 17);     _______________

        ifElseMystery(12, 5);     _______________

        ifElseMystery(16, 8);     _______________






4. While Loop Simulation, 12 points.  Consider the following method:

        public static void mystery(int n) {
            int x = 1;
            int y = 1;
            while (n > 2) {
                x = x + y;
                y = x - y;
                n--;
            }
            System.out.println(x);
        }

   For each call below, indicate what output is produced.

        Method Call             Output Produced

        mystery(1);             _______________

        mystery(3);             _______________

        mystery(5);             _______________

        mystery(7);             _______________



5. Assertions, 15 points.  You will identify various assertions as being either
   always true, never true or sometimes true/sometimes false at various points
   in program execution.  The comments in the method below indicate the points
   of interest.

        public static int numOddDigits(int n) {
	    int x = 0;
            // Point A
            while (n != 0) {
                // Point B
                if (n % 2 == 1) {
                    x++;
                    // Point C
                }
		n = n / 10;
		// Point D
            }
            // Point E
	    return x;
        }

   Fill in the table below with the words ALWAYS, NEVER, or SOMETIMES (you may
   abbreviate them as A, N, or S).

                    n == 0              n % 2 == 1              x == 0
            +---------------------+---------------------+---------------------+
    Point A |                     |                     |                     |
            +---------------------+---------------------+---------------------+
    Point B |                     |                     |                     |
            +---------------------+---------------------+---------------------+
    Point C |                     |                     |                     |
            +---------------------+---------------------+---------------------+
    Point D |                     |                     |                     |
            +---------------------+---------------------+---------------------+
    Point E |                     |                     |                     |
            +---------------------+---------------------+---------------------+


6. Programming, 15 points.  Write a static method called quadrant that takes as
   parameters a pair of double values representing an (x, y) point and that
   returns the quadrant number for that point.  Recall that quadrants are
   numbered as integers from 1 to 4 with the upper-right quadrant numbered 1
   and the subsequent quadrants numbered in a counter-clockwise fashion:

                             ^ y-axis
                             |
                             |
                             |
                 Quadrant 2  |  Quadrant 1
                             |
        <--------------------+--------------------> x-axis
                             |
                 Quadrant 3  |  Quadrant 4
                             |
                             |
                             |
                             V

   Notice that the quadrant is determined by whether the x and y coordinates
   are positive or negative numbers.  If a point falls on the x-axis or the
   y-axis, then the method should return 0.  Below are sample calls on the
   method.

        Method                  Value          Method                  Value
         Call                  Returned         Call                  Returned
        -------------------------------        -------------------------------
        quadrant(12.4, 17.8)      1            quadrant(0.0, 0.0)        0
        quadrant(-2.3, 3.5)       2            quadrant(12.5, 0.0)       0
        quadrant(-15.2, -3.1)     3            quadrant(0.0, 2.3)        0
        quadrant(4.5, -4.5)       4

   Write your solution to method quadrant below.


7. Programming, 15 points.  Write a static method called printTwoDigit that
   takes a Random object and an integer n as parameters and that prints a
   series of n randomly generated two-digit numbers.  The method should use the
   Random object to select numbers in the range of 10 to 99 inclusive where
   each number is equally likely to be chosen.  The method should indicate
   whether the number 42 was selected.  For example, given the following lines
   of code:

        Random r = new Random();
        printTwoDigit(r, 4);
   
   You would expect output like the following:

        next = 52
        next = 10
        next = 96
        next = 86
        no 42

   As the final line of output above indicates, that particular sequence did
   not include the number 42.  Suppose that we then call the method as follows:

        printTwoDigit(r, 6);

   We might get output like the following where 42 is included in the sequence:

        next = 83
        next = 29
        next = 42
        next = 22
        next = 36
        next = 73
        We saw a 42

   You may assume the integer value passed to your method is greater than or
   equal to 0.  You are to exactly reproduce the format of these logs.  Write
   your solution to printTwoDigit below.


8. Programming, 9 points.  Write a static method called digitsInARow that takes
   an integer n as a parameter and that returns the highest number of digits
   that appear in a row in the base-10 representation of n.  For many numbers
   the answer will be 1 because they don't have adjacent digits that match.
   But for a number like 3555585, the answer is 4 because there are four
   occurrences of the digit 5 that appear in a row.  Below are sample calls on
   the method.

        Method                  Value          Method                  Value
         Call                  Returned         Call                  Returned
        -------------------------------        -------------------------------
        digitsInARow(0)           1            digitsInARow(8823)        2
        digitsInARow(18)          1            digitsInARow(777)         3
        digitsInARow(394)         1            digitsInARow(82888)       3
        digitsInARow(99)          2            digitsInARow(7111171)     4
        digitsInARow(8229)        2            digitsInARow(233333888)   5

   You are NOT allowed to use a String to solve this problem.  You may assume
   that the value passed to the method is greater than or equal to 0.

   Write your solution to method digitsInARow below.