Quiz on Chapter 8 | Introduction to Programming Using Java
Quiz on Chapter 8
- What does it mean to say that a program is robust?
- Why do programming languages require that variables be declared before they are used? What does this have to do with correctness and robustness?
- What is a precondition? Give an example.
- Explain how preconditions can be used as an aid in writing correct programs.
- Java has a predefined class called Throwable. What does this class represent? Why does it exist?
- 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.
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?
8.Some classes of exceptions require mandatory exception handling. Explain what this means.
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.
- Why should a subroutine throw an exception when it encounters an error? Why not just terminate the program?
- 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?
- Consider the ThreadSafeCounter example from Subsection 8.5.3:
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.)
SEE MORE:
- Chapter 9.1 Recursion | Introduction to Programming Using Java
- Chapter 9.1.2 Towers of Hanoi | Introduc’tion to Programming Using Java
- Chapter 9.3.1 Stacks | Introduction to Program’ming Using Java
- Chapter 9.4.1 Tree Traversal | Introduction to Program,ming Using Java
- Chapter 10.1.1 Generic Programming in Smalltalk | Introduc’tion to Program ming Using Java
- Chapter 10.1.2 Generic Programming in C++ | Introduction to Program’ming Using Java
- Chapter 10.1.3 Generic Programming in Java | Introduction to Program,ming Using Java
- Chapter 10.1.4 The Java Collection Framework | Introduc’tion to Program’ming Using Java
- Chapter 10.1.5 Iterators and for-each Loops | Introduction to Programming Using Java
- Chapter 12 Advanced GUI Programming | Images and Resources | Introduction to Program ming Using Java
7 thoughts on “Quiz on Chapter 8 | Introduction to Programming Using Java”