CSE142 Sample Midterm
			  

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

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

        54 % 10 + 8 * 3 % 9                    __________

        3 * 2 + 4 + "+" + 2 + 3 * 4            __________

        2.3 * 3 + 19 / 5 / 2 + 6.0 / 5         __________

        108 / 20 * 3 / 4 / 2.0 + 1.0 / 2       __________

2. Parameter Mystery, 12 points.  What output is produced by the following
   program?

    public class Mystery {
        public static void main(String[] args) {
            String hear = "bad";
            String song = "good";
            String good = "hear";
            String walk = "talk";
            String talk = "feel";
            String feel = "walk";
            claim(feel, song, feel);
            claim(good, hear, song);
            claim(talk, "song", feel);
            claim("claim", talk, walk);
        }
    
        public static void claim(String hear, String good, String song) {
            System.out.println("to " + hear + " the " + song + " is " + good);
        }
    }

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

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

   For each call below, indicate what output is produced.

        Method Call               Output Produced

        ifElseMystery(10, 3);     _______________

        ifElseMystery(6, 6);      _______________

        ifElseMystery(3, 4);      _______________

        ifElseMystery(4, 20);     _______________

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

        public static void mystery(int n) {
            int x = 1;
            int y = 2;
            while (y < n) {
                if (n % y == 0) {
                    n = n / y;
                    x++;
                } else {
                    y++;
                }
            }
            System.out.println(x + " " + n);
        }

   For each call below, indicate what output is produced.

        Method Call             Output Produced

        mystery(2);             _______________

        mystery(5);             _______________

        mystery(24);            _______________

        mystery(28);            _______________

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 void mystery(int x, int y) {
            int z = 0;
            // Point A
            while (x != y) {
                // Point B
                z++;
                if (x > y) {
                    // Point C
                    x = x / 10;
                } else {
                    // Point D
                    y = y / 10;
                }
            }
            // Point E
            System.out.println(x + " " + y + " " + z);
        }

   Fill in the table below with the words ALWAYS, NEVER or SOMETIMES.

                     x > y                 z == 0                x == y
            +---------------------+---------------------+---------------------+
    Point A |                     |                     |                     |
            +---------------------+---------------------+---------------------+
    Point B |                     |                     |                     |
            +---------------------+---------------------+---------------------+
    Point C |                     |                     |                     |
            +---------------------+---------------------+---------------------+
    Point D |                     |                     |                     |
            +---------------------+---------------------+---------------------+
    Point E |                     |                     |                     |
            +---------------------+---------------------+---------------------+

6. Programming, 15 points.  Write a method flip that takes a Random object as a
   parameter and that prints information about a coin-flipping simulation.

   Your method should use the Random object to produce a sequence of simulated
   coin flips, printing whether each flip comes up "heads" or "tails".  Each
   outcome should be equally likely.  Your method should stop flipping when you
   see three heads in a row.  It should return the total number of flips.

   For example, if we construct a Random object and make the following calls;

        Random r = new Random();
        flip(r);
        flip(r);

   We expect to get a log of execution like this:

        heads
        tails
        heads
        heads
        heads
        3 heads in a row after 5 flips
        
        heads
        heads
        tails
	heads
	tails
	tails
        heads
        heads
        heads
        3 heads in a row after 9 flips
        
   You must exactly reproduce the format of the log above.

7. Programming, 15 points.  Write a method hasMidpoint that takes three
   integers as parameters and that returns true if one of the numbers is the
   midpoint of the other two and that returns false otherwise.  A number is
   considered the midpoint of two numbers if it appears halfway between them.
   For example, 12 is the midpoint of 10 and 14.  The numbers might be in any
   order, so any one of the three might be the midpoint of the other two.

8. Programming, 9 points.  Write a method sameDashes that takes two strings as
   parameters and that returns whether or not they have dashes in the same
   places (returning true if they do and returning false otherwise).  For
   example, below are four pairs of strings of equal length that have the same
   pattern of dashes.  Notice that the last pair has no dashes at all.

        string1:    "hi--there-you."    "-15-389"    "criminal-plan"    "abc"
        string2:    "12--(134)-7539"    "-xy-zzy"    "(206)555-1384"    "9.8"

   To be considered a match, the strings must have exactly the same number of
   dashes in exactly the same positions.  The strings might be of different
   length.  For example, the following calls should each return true:

        sameDashes("1st-has-more characters", "2nd-has-less")
        sameDashes("1st-has-less", "2nd-has-more characters")

   because the strings each have two dashes and they are in the same positions.
   But the following calls should each return false because the longer string
   has a third dash where the shorter string does not:

        sameDashes("1st-has-more-characters", "2nd-has-less")
        sameDashes("1st-has-less", "2nd-has-more-characters")

Last modified: Fri Feb 6 13:05:04 PST 2009