[ Quiz Answers | Chapter Index | Main Index ]

Quiz on Chapter 8

This page contains questions on Chapter 8 of Introduction to Programming Using Java. You should be able to answer these questions about studying that chapter. Sample answers to these questions can be found here.

Question 1:

What does it mean to say that a program is robust?

Question 2:

Why do programming languages require that variables be declared before they are used? What does this have to do with correctness and robustness?

Question 3:

What is a precondition? Give an example.

Question 4:

Explain how preconditions can be used as an aid in writing correct programs.

Question 5:

Java has a predefined class called Throwable. What does this class represent? Why does it exist?

Question 6:

Write a method that prints out a 3N+1 sequence starting from a given integer, N. The starting value should be a parameter to the method. If the parameter is less than or equal to zero, throw an IllegalArgumentException. If the number in the sequence becomes too large to be represented as a value of type int, throw an ArithmeticException.

Question 7:

Rewrite the method from the previous question, using assert statements instead of exceptions to check for errors. What the difference between the two versions of the method when the program is run?

Question 8:

Some classes of exceptions require mandatory exception handling. Explain what this means.

Question 9:

Consider a subroutine processData() that has the header

static void processData() throws IOException

Write a try..catch statement that calls this subroutine and prints an error message if an IOException occurs.

Question 10:

Why should a subroutine throw an exception when it encounters an error? Why not just terminate the program?

Question 11:

Suppose that a program uses a single thread that takes 4 seconds to run. Now suppose that the program creates two threads and divides the same work between the two threads. What can be said about the expected execution time of the program that uses two threads?

Question 12:

Consider the ThreadSafeCounter example from Subsection 8.5.3:

public class ThreadSafeCounter {
   
   private int count = 0;  // The value of the counter.
   
   synchronized public void increment() {
      count = count + 1;
   }
   
   synchronized public int getValue() {
      return count;
   }
   
}

The increment() method is synchronized so that the caller of the method can complete the three steps of the operation "Get value of count," "Add 1 to value," "Store new value in count" without being interrupted by another thread. But getValue() consists of a single, simple step. Why is getValue() synchronized? (This is a deep and tricky question.)


[ Quiz Answers | Chapter Index | Main Index ]