CPSC 124, Winter 1998

Quiz Number 2


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


Question 1: Explain briefly how pseudocode and stepwise refinement can be used to develop an algorithm for performing a given task.

Answer: A pseudocode algorithm is written in informal language, closer to English, rather than in the strict syntax of a programming language. It is possible to start with a brief outline of the algorithm, and then refine it step-by-step by expanding and filling in details. Eventually, there will be enough details that the algorithm can be easily translated into a programming language.


Question 2: Explain what is meant by an assignment statement, and give an example. What are assignment statements used for?

Answer: An assignment statement computes a value and stores that value in a variable. Examples include:

         x = 17;          // Assign a constant value to the variable, x.
         newRow = row;    // Copy the value from the variable, row,
                          //             into the variable, newRow.
         ans = 17*x + 42; // Compute the value of the expression 
                          //     17*x + 42, and store that value in ans.

An assignment statement is used to change the value of a variable as the program is running. Since the value assigned to the variable can be another variable or an expression, assignments statements can be used to copy data from one place to another in the computer, and to do complex computations.


Question 3: Write a program segment that declares two int variables, gets values for the two variables from the user, and then outputs the larger of the two values. (That is, if x is larger than y, the program should output x. Otherwise, it should output y.)

Answer: Note that this problem asks only for a "program segment" not for a complete program. Here is one possible solution:

       int x,y;
       console.put("Give me a number: ");
       x = console.getlnInt();
       console.put("Give me another number: ");
       y = console.getlnInt();
       if (x > y)
           console.putln("The larger value is " + x);
       else
           console.putln("The larger value is " + y);

Question 4: Show the exact output produced by the following program:

Answer: The output is produced by the console.putln() statement. The first time it is executed, x is 17 and y is 2. Then the values of x and y are changed to 16 and 4, and another line of output is produced. The values become 15 and 8, and another line is output. At this point, the values of x and y become 14 and 16. when the computer tests whether x > y, the answer is false, and the while loop terminates without producing any further output. The exact output produced by the program is:

          x is 17 and y is 2
          x is 16 and y is 4
          x is 15 and y is 8

David Eck, 22 January 1998