[ Chapter Index | Main Index ]

Answers for Quiz on Chapter 3

This page contains sample answers to the quiz on Chapter 3 of Introduction to Programming Using Java. Note that generally, there are lots of correct answers to a given question.

Question 1:

What is an algorithm?


An algorithm is an unambiguous, step-by-step procedure for performing a certain task, which is guaranteed to finish after a finite number of steps. (An algorithm is not the same thing as a program, but it can be the idea behind a program.)

Question 2:

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


Pseudocode refers to informal descriptions of algorithms, 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:

What is a block statement? How are block statements used in Java programs?


A block statement is just a sequence of Java statements enclosed between braces, { and }. The body of a subroutine is a block statement. Block statements are often used in control structures. A block statement is generally used to group together several statements so that they can be used in a situation that only calls for a single statement. For example, the syntax of a while loop calls for a single statement: "while (condition) do statement". However, the statement can be a block statement, giving the structure: "while (condition) { statement; statement; ...}".

Question 4:

What is the main difference between a while loop and a do..while loop?


Both types of loop repeat a block of statements until some condition becomes false. The main difference is that in a while loop, the condition is tested at the beginning of the loop, and in a do..while loop, the condition is tested at the end of the loop. It is possible that the body of a while loop might not be executed at all. However, the body of a do..while loop is executed at least once since the condition for ending the loop is not tested until the body of the loop has been executed.

Question 5:

What does it mean to prime a loop?


The condition at the beginning of a while loop has to make sense even the first time it is tested, before the body of the loop is executed. To prime the loop is to set things up before the loop starts so that the test makes sense (that is, the variables that it contains have reasonable values). For example, if the test in the loop is "while the user's response is yes," then you will have to prime the loop by getting a response from the user (or making one up) before the loop.

Question 6:

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


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 7:

Write a for loop that will print out all the multiples of 3 from 3 to 36, that is: 3 6 9 12 15 18 21 24 27 30 33 36.


Here are two possible answers. Assume that N has been declared to be a variable of type int:

        for ( N = 3;  N <= 36;  N = N + 3 ) {
            System.out.println( N );
        for ( N = 3;  N <= 36;  N++ ) {
            if ( N % 3 == 0 )
                System.out.println( N );
Question 8:

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.)

public static void main(String[] args) {
         // Fill in the body of this subroutine!

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.");
      System.out.println("That's an odd number.");
Question 9:

Write a code segment that will print out two different random integers selected from the range 1 to 10. All possible outputs should have the same probability. Hint: You can easily select two random numbers, but you have to account for the fact that the two numbers that you pick might be the same.


The code for selecting two random integers has to be wrapped in a loop that will end only when the two selected numbers are different. This can be done easily with a do..while loop. A while loop can also be used, but it must be "primed" in some way. Note that by using a loop to choose the numbers, we can be absolutely sure that after the loop ends, the two numbers are different. Here are three possible solutions:

(1)     int x,y; // Two random integers.
        do {
            x = (int)(10*Math.random() + 1);
            y = (int)(10*Math.random() + 1);
        } while (x == y);  // continue if x and y are the same number.
        System.out.println(x + " " + y);
(2)     int x,y;  // Two random integers.
        x = y = 0;  // Give them junk value to "prime" the loop to make sure it runs.
        while (x == y) {
            x = (int)(10*Math.random() + 1);
            y = (int)(10*Math.random() + 1);
        System.out.println(x + " " + y);
(3)     int x,y; // Two random integers.
        x = (int)(10*Math.random() + 1);  // Pick x, and keep its value.
        do { // The loop finds a y with a different value from x.
            y = (int)(10*Math.random() + 1);
        } while (x == y);
        System.out.println(x + " " + y);
Question 10:

Suppose that s1 and s2 are variables of type String, whose values are expected to be string representations of values of type int. Write a code segment that will compute and print the integer sum of those values, or will print an error message if the values cannot successfully be converted into integers. (Use a try..catch statement.)


The function Integer.parseInt can be used to convert the strings into integers. This function will throw an exception of type NumberFormatException if the conversion fails. A try..catch statement can catch this exception and print an error message. So, the code segment can be written:

try {
   int n1, n2;  // The values of s1 and s2 as integers.
   int sum;     // The sum of n1 and n2.
   n1 = Integer.parseInt(s1);
   n2 = Integer.parseInt(s2);
   sum = n1 + n2;   // (If an exception occurs, we don't get to this point.)
   System.out.println("The sum is " + sum);
catch ( NumberFormatException e ) {
    System.out.println("Error!  Unable to convert strings to integers.);  
Question 11:

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

public static void main(String[] args) {
    int N;
    N = 1;
    while (N <= 32) {
       N = 2 * N;

The exact output printed by this program is:


(The hard part to get right is the 64 at the end. 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 12:

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

public static void main(String[] args) {
   int x,y;
   x = 5;
   y = 1;
   while (x > 0) {
      x = x - 1;
      y = y * x;

The way to answer this question is to trace exactly what the program does, step-by-step. 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 13:

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;
int i;
boolean startWord;

name = "Richard M. Nixon";
startWord = true;
for (i = 0; i < name.length(); i++) {
   if (startWord)
   if (name.charAt(i) == ' ')
      startWord = true;
      startWord = false;

This is a tough one! The output from this program consists of the three lines:


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 the first character is a space or when there are two spaces in a row. What happens in these cases?)

Question 14:

Suppose that numbers is an array of type int[]. Write a code segment that will count and output the number of times that the number 42 occurs in the array.


Use a for loop to go through the array and test each array element. If the value is 42, add 1 to a counter:

int count42;  // The number of 42s in the array
count42 = 0;
int i;  // loop control variable
for ( i = 0; i < numbers.length; i++ ) {
    if ( numbers[i] == 42 ) {
System.out.println("There were " + count42 + " 42s in the array.")
Question 15:

Define the range of an array of numbers to be the maximum value in the array minus the minimum value. Suppose that raceTimes is an array of type double[]. Write a code segment that will find and print the range of raceTimes.


We need both the minimum and the maximum value in the array. We can compute both using one for loop.

double max;    // maximum value from the array
double min;    // minimum value from the array
double range;  // the range of the array, max - min
int i;         // for-loop variable
max = min = raceTimes[0];  // start with both equal to the first element
for ( i = 1; i < raceTimes.length; i++ ) {
    if ( raceTimes[i] > max )
        max = raceTimes[i];
    if ( raceTimes[i] < min )
        min = raceTimes[i];
range = max - min;
System.out.println("The range is " + range);

[ Chapter Index | Main Index ]