CPSC 124, Fall 1996

Quiz Number 2


This is the second quiz given in CPSC 124: Introductory Programming, Fall 1996. 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.


Question 1: Explain what is meant by a "variable" and what the computer does when it executes a variable declaration statement.

Answer: A variable is a "box", or location, in the computer's memory that has a name. The box holds a value of some specified type. A variable declaration statement is a statement such as

int x;

which creates the variable x. When the computer executes a variable declaration, it creates the box in memory and associates a name with that box. Later in the program, that variable can be referred to by name.


Question 2: Explain briefly what is meant by "pseudocode" and how is it useful in the development of algorithms.

Answer: Pseudocode refers to informal descriptions of algorithm written in a language that imitates the structure of a programming language, but without the strict syntax. Pseudocode can be used in the process of developing an algorithm with stepwise refinement. You can start with a brief pseudocode description of the algorithm and then add detail to the description through a series of refinements until you have something that can be translated easily into a program written in an actual programming language.


Question 3: Give the meaning of each of the following Java operators:

a) ++

b) &&

c) !=

Answer: The operator ++ is used to add 1 to the value of a variable. For example, "count++" has the same effect as "count = count + 1".

The operator && represents the word and. It can be used to combine two boolean values, as in "(x > 0 && y > 0)", which means, "x is greater than 0 and y is greater than 0."

The operation != means "is not equal to", as in "if (x != 0)", meaning "if x is not equal to zero.".


Question 4: Show the exact output that would be produced by the following main() routine:

         public static void main(String[] args) {
             Console console = new Console();
             int N = 1;
             while (N <= 32) {
                N = 2 * N;
                console.putln(N);   
             }
         }

Answer: The exact output printed on the console by this program is:

                 2
                 4
                 8
                 16
                 32
                 64

(The value of N doubles each time through the loop. For the final execution of the loop, N starts out with the value 32, but N is doubled to 64 before it is printed.)


Question 5: Fill in the following main() routine so that it will ask the user to enter a number, x, and will then print out the square of that number, x*x. The number entered can be a real number, such as 3.7.

          public static void main(String[] args) {
             Console console = new Console();
             
             
          }

Answer: The following statements will have the specified behavior:

              double x;         // number to be input by the user
              double square;    // the square of the number x
              console.put("Please enter a number: ");
              x = console.getDouble();
              square = x * x;
              console.putln("The square of that number is " + square);

David Eck