CSE142 Sample Midterm          handout #16

                          Winter 2006

 

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 * 4 + 5 * 6                          __________

 

      23 % 5 + - 17 % (16 % 10)              __________

 

      "1" + 2 + 3 * 4 + (5 + 6)              __________

 

      1.5 * 2 + 20 / 3 / 4.0 + 6 / 4         __________

 

      345 / 10 / 3  + 10 / (5 / 2.0)         __________

 

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

 

        public class Mystery {

            public static void main(String[] args) {

            String foo = "buzz";

            String sam = "sue";

            String sue = "foo";

            String bill = "hope";

            String hope = "bill";

           

            say(sam, sue, foo);

            say(foo, "bill", sam);

            say(hope, bill, sue);

            say(bill, hope, sam);

            say("sue", "hope", hope);

            }

       

            public static void say(String foo, String sam, String sue) {

            System.out.println(sam + " wants " + sue + " to " + foo + ".");

            }

        }

       

   List below the output produced by this program.

 


3. Simulation, 15 points.  Consider the following method:

 

        public static int mystery(int z) {

            int x = 1;

            int y = 1;

            while (z > 2) {

                y = y + x;

                x = y - x;

                z--;

            }

            return y;

        }

 

   For each call below, indicate what value is returned.

 

      Method Call       Value Returned

 

      mystery(1);       _______________

 

      mystery(3);       _______________

 

      mystery(4);       _______________

 

      mystery(5);       _______________

 

      mystery(6);       _______________

 

 

4. 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 mystery(int x) {

            int y = 1;

            int z = 0;

            // Point A

            while (y <= x) {

                // Point B

                y = y * 10;

                z++;

                // Point C

            }

            // Point D

            z--;

            // Point E

            return z;

        }

 

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

 

                     y > x                 z < 0                 z > 0

            +---------------------+---------------------+---------------------+

    Point A |                     |                     |                     |

            +---------------------+---------------------+---------------------+

    Point B |                     |                     |                     |

            +---------------------+---------------------+---------------------+

    Point C |                     |                     |                     |

            +---------------------+---------------------+---------------------+

    Point D |                     |                     |                     |

            +---------------------+---------------------+---------------------+

    Point E |                     |                     |                     |

            +---------------------+---------------------+---------------------+

 


5. 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.

 

   Write your solution to hasMidpoint below.

 

6. Programming, 15 points.  Write a method printMultiples that takes an integer

   n and an integer m as parameters and that prints a complete line of output

   reporting the first m multiples of n.  For example, the following calls:

 

      printMultiples(3, 5);

      printMultiples(7, 3);

 

   should produce this output:

 

        The first 5 multiples of 3 are 3, 6, 9, 12, 15

        The first 3 multiples of 7 are 7, 14, 21

 

   Notice that the multiples are separated by commas.  You are to exactly

   reproduce this format.  Also notice the order of the parameters: the first

   parameter is the base number and the second parameter is the number of

   multiples to generate.

 

   You may assume that the number of multiples you will be asked to generate is

   greater than or equal to one.  Write your solution to printMultiples below.

 


7. Programming, 10 points.  Write a method printStripped that takes a String

   as an argument and that prints a complete line of output with any comments

   stripped from the String.  Comments are defined to be characters enclosed in

   the characters "<" and ">".  More precisely, text is "normal" until you

   encounter a "<" character.  From that point on the text is considered a

   comment until you encounter a ">" character, at which point you return to

   normal text.  This definition allows for "<" inside a comment and ">"

   outside a comment.  You may assume that there are no unclosed comments in

   the String.

 

   For example, the following sequence of calls:

 

      printStripped("this is plain text");

      printStripped("this has a normal comment <right here> to be removed");

      printStripped("this has multiple less-than in a comment <<<<<see?>");

      printStripped("this > has <comment>greater-than outside a comment >>");

      printStripped("<hi>this has <foo> multiple <bar> comments<baz><>.");

 

   should produce the following output:

 

        this is plain text

        this has a normal comment  to be removed

        this has multiple less-than in a comment

        this > has greater-than outside a comment >>

        this has  multiple  comments.

 

   Write your solution to printStripped below.

Stuart Reges Last modified: Mon Oct 31 19:09:12 PST 2005