CPSC 124, Fall 1998

Quiz Number 3


This is the third 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: What is the meaning of each of the following operators in Java?

Answer:


Question 2: Write a for statement that will print out the numbers 2, 4, 6, 8, 10, 12, 14, 16, 18, 20.

Answer: Here are four different possible solutions:

   1)      for (int N=2; N <= 20; N = N+2)
               System.out.println(N);
               

   2)      for (int N=1; N <= 10; N++)
               System.out.println(2*N);
               

   3)      for (int N=0; N < 10; N++)
               System.out.println(2*(N+1));
               

   4)      for (int N=1; N <= 20; N++) {
               if (N % 2 == 0)   // if N is even
                  System.out.println(N);
           }


Question 3: Explain what is meant by an animation and how a computer displays an animation.

Answer: An animation consists of a series of "frames." Each frame is a still image, but there are slight differences from one frame to the next. When the images are displayed rapidly one frame after another, the eye perceives motion. A computer displays an animation by showing one image on the screen, then replacing it with the next image, then the next, and so on.


Question 4: What output is produced by the following program segment? Why? (Recall that name.charAt(i) is the i-th character in the string, name.)

         String name = "Richard M. Nixon";
         boolean startWord = true;
         for (int i = 0; i < name.length(); i++) {
            if (startWord)
               System.out.println(name.charAt(i));
            if (name.charAt(i) == ' ')
               startWord = true;
            else
               startWord = false;
         }

Answer: The output from this program consists of the three lines:

             R
             M
             N

As the for loop in this code segment is executed, name.charAt(i) represents each of the characters in the string "Richard M. Nixon" in succession. The statement System.out.println(name.charAt(i)) outputs the single character name.charAt(i) on a line by itself. However, this output statement occurs inside an if statement, so only some of the characters are output. The character is output if startWord is true. This variable is initialized to true, so when i is 0, startWord is true, and the first character in the string, 'R', is output. Then, since 'R' does not equal ' ', startWorld becomes false, so no more characters are output until startWord becomes true again. This happens when name.charAt(i) is a space, that is, just before the 'M' is processed and again just before the 'N' is processed. In fact whatever the value of name, this for statement would print the first character in name and every character in name that follows a space. (It is almost true that this for statement prints the first character of each word in the string, but something goes wrong when there are two spaces in a row. What happens in this case?)


David Eck, 1 October 1998