CPSC 124, Fall 1998

Quiz Number 2


This is the second quiz given in CPSC 124: Introductory Programming, Fall 1998. See the information page for that course for more information.

The answers given here are sample answers that would receive full credit. However, they are not necessarily the only correct answers. In some cases, I give more detailed answers here than would be necessary to get full credit.


Question 1: One of the primitive types in Java is boolean. What is the boolean type? Where are boolean values used? What are its possible values?

Answer: The only values of type boolean are true and false. Expressions of type boolean are used in places where true/false values are expected, such as the conditions in while loops and if statements.

Note: A "type" represents a set of possible values. When you specify that a variable has a certain type, you are saying what values it can hold. When you say that an expression is of a certain type, you are saying what values the expression can have. The value in a boolean variable must be either true or false. A boolean-valued expression represents one of the values true or false. Boolean and relational operators such as &&, <, and != are used to construct boolean-valued expressions.


Question 2: Briefly explain what is meant by the syntax and the semantics of a programming language. Give an example to illustrate the difference between a syntax error and a semantics error.

Answer: The syntax of a language is its grammar, and the semantics is its meaning. A program with a syntax error cannot be compiled. A program with a semantic error can be compiled and run, but gives an incorrect result. A missing semicolon in a program is an example of a syntax error, because the compiler will find the error and report it. A semicolon in the wrong place could be a semantic error. For example:

                for (int i = 0; i < 6; i++);
                   System.out.println("Hello World!");

will only print "Hello World!" once, because of the semicolon at the end of the first line, but the programmer probably meant to print "Hello World!" six times.


Question 3: Show the exact output produced by the following main() routine:

              public static void main(String[] args) {
                 int x = 5;
                 int y = 1;
                 while (x > 0) {
                    x = x - 1;
                    y = y * x;
                    System.out.println(y);
                 }
              }

Answer: The output is shown below on the right. On the left is a table that shows the values of the variables x and y as the program is being executed.

        value of x   |   value of y                 OUTPUT
       --------------|--------------             -------------
             5       |     1  [ before loop]
             4       |     4  [ = 1*4 ]               4
             3       |    12  [ = 4*3 ]               12
             2       |    24  [ = 12*2 ]              24
             1       |    24  [ = 24*1 ]              24
             0       |     0  [ = 24*0 ]              0

Question 4: Fill in the following main() routine so that it will ask the user to enter an integer, read the user's response, and tell the user whether the number entered is even or odd. (You can use TextIO.getInt() to read the integer. Recall that an integer n is even if n % 2 == 0.)

Answer: The problem already gives an outline of the program. The last step, telling the user whether the number is even or odd, requires an if statement to decide between the two possibilities.

            public static void main (String[] args) {

               int n;  // the number read from the user

               TextIO.put("Type an integer: ");  // ask the use to enter an integer

               n = TextIO.getInt();   // read the user's response

               if (n % 2 == 0)        // tell the user whether the number is even or odd
                  System.out.println("That's an even number.");
               else
                  System.out.println("That's an odd number.");

            }

David Eck, 24 September 1998