CPSC 124, Fall 2001
Quiz #2, September 17

This is the second quiz in CPSC 124: Introductory Programming.

Question 1: Suppose that x and y are variables of type int and that the value of x is 31 and the value of y is 7. What is the value of each of the following Java expressions?

         a) x % y

         b) (x / 10) * 10

         c) (x > y)? 1 : 0
         d) "00" + y


     a)   x % y has value  3    (The remainder when 7 is divided into 31.)

     b)   (x / 10) * 10 has value 30   (When the computer divides two ints,
                                        it gets an int for the answer,
                                        discarding the fractional part.  So,
                                        x / 10 is 3, not 3.33333333333.)

     c)   (x > y)? 1 : 0  has value  1   (The ?: operator returns one of
                                          two values, depending on whether
                                          a condition is true or false.  Since
                                          x > y is true, the first value,
                                          1, is returned.)
     d)   "00" + y has value  "007"   ("00" is a String.  When the + operator is
                                       used with a String, it means concatenation,
                                       not addition.  That is, the value of y is
                                       appended onto the end of "00", giving 
                                       the string "007".)

Question 2: Assume that a and b have been declared to be variables of type int. Show the exact output produced by the following Java code segment.

        x = 2;
        y = 1;
        while (x < 10) {
           y = 2*y;
           x = x + y;
           System.out.println("x = " + x);
           System.out.println("y = " + y);

Answer: The output is as follows:

          x = 4
          y = 2
          x = 8
          y = 4
          x = 16
          y = 8

(Note that I asked for the exact output, which includes the strings "x = " and "y = ". A common mistake is to stop after the first four lines, thinking that the loop ends as soon as the test x<10 becomes false. It doesn't. The loop can only end when the computer actually makes the test at the beginning of the loop. After x becomes equal to 16, the computer does the two output statements before it gets back to the start of the loop to make the test. Some people had x=3 the first time through the loop, which is an interesting error. Apparently, they looked at x=x+y and computed 2+1. However, by the time the computer gets to x=x+y, the statement y=2*y has already been executed, so the value of y is 2. The computer uses this new value in the formula x+y, giving a value of 4 for x.)

Question 3: Write a complete Java program. The program will first get two integers from the user. Then it will print the smaller of the two integers. For example, if the user enters 42 and 17, then the program will print 17.

        public class Minimum



        public class Minimum

           public static void main(String[] args) {
              int x, y;  // The two numbers entered by the user
              // Get the user's number.
              System.out.print("Enter an integer:  ");
              x = TextIO.getlnInt();
              System.out.print("Enter another integer:  ");
              y = TextIO.getlnInt();
              // Test which number is smaller and print it.
              // (If the numbers are the same, the common value
              // is printed.)
              if (x < y) {
                 System.out.println("The smaller number is " + x);
              else {
                 System.out.println("The smaller number is " + y);
           } // end main()
        } // end class Minimum

Question 4: Explain what is meant by pseudocode and how it can be used in a process of stepwise refinement to develop algorithms.

Answer: Pseudocode refers to writing algorithms in informal language, rather than in the strict syntax of programming languages. To use pseudocode and stepwise refinement, start with a short outline of an algorithm, written in pseudocode. Then gradually fill in details in the algorithm until it is possible to translate it directly into a formal program.

David Eck, eck@hws.edu