CSE143 Summer 2004
Midterm Exam #2
July 30, 2004
Multiple choice questions: 2 pts. each


1. A programmer has written handlers for several possible exceptions.  Which is a correct ordering for the catch blocks?
A.
try { .... }
catch (Exception e) { ...}
catch (IOException e) { ...}
catch (FileNotFoundException e) { ... }
catch (NullPointerException e) { ... }

B.
try { .... }
catch (IOException e) { ...}
catch (FileNotFoundException e) { ... }
catch (Exception e) { ...}
catch (NullPointerException e) { ... }

C.
try { .... }
catch (FileNotFoundException e) { ... }
catch (IOException e) { ...}
catch (NullPointerException e) { ... }
catch (Exception e) { ...}

D.
try { .... }
catch (NullPointerException e) { ... }
catch (Exception e) { ...}
catch (IOException e) { ...}
catch (FileNotFoundException e) { ... }

E.
try { .... }
catch (IOException e) { ...}
catch (NullPointerException e) { ... }
catch (FileNotFoundException e) { ... }
catch (Exception e) { ...}








2. The vocabulary "checked" vs "unchecked" exceptions refers to the fact that
A. At run-time, the system checks only for things that can cause "checked" exceptions and ignores things which can cause "unchecked" exceptions.
B. At compile-time, the compiler checks that "checked" exceptions are handled or declared.
C. At compile-time, the compiler checks that any code which handles errors is correct.
D. In DrJava or other environments or using compiler options, the programmer can "check" or "uncheck" which exceptions are generated.

3. When the throw statement executes in a method...
A. that method automatically returns
B. the application automatically aborts
C. a corresponding catch statement is looked for in that method
D. a corresponding catch statement is looked for in the calling method
E. an error is printed on the console but no other action is taken

4. The general procedure for defining your own type of exception is...
A. to extend a certain class
B. to implement a certain interface
C. to implement a certain event handler
D. to create an object of a certain type
E. to call a certain library method

5. An advantage of following the Sun Java Coding Conventions is that...,
A. most professional Java developers will recognize and respect your style of coding
B. the program is almost guaranteed to run compatibly with any program written by Sun
C. the program is protected from future updates to the Java language
D. the compiler will be able to check for logic errors by interpreting the standard documentation comments

6. Which of the following is not a requirement when writing JUnit tests?
A. extending a certain class in the junit package
B. throwing a certain type of exception when the test detects an error
C. naming the test methods to begin test...
D. not having parameters in the test methods
E. having the test methods be void

7. In the Reader abstract class, the read() method...
A. returns a char from the stream, as an int
B. returns a char from the stream, as a string of length one
C. returns a string containing all characters to the end of the line, including the linefeed.
D. returns a string containing all characters to the end of the line, excluding the linefeed
E. throws an exception at end of file




8. The main difference between an InputStream and a Reader is
A. An InputStream reads bytes and a Reader reads characters
B  An InputStream can only be opened on non-text files, and a Reader can only be only on text files.
C. An InputStream can be opened many types of sources, while a Reader can only be opened on local files.
D. An InputStream can be extended, while a Reader is final.

9. The Java type char is...
A. a reference (object) type, always a 16-bit Unicode character
B. an elementary type, always a 16-bit Unicode character
C. a reference (object) type, always an 8-bit ASCII character
D. an elementary type, always an 8-bit ASCII character
E. a type which depends on the operating system and/or type of computer.

10.  The readLine method of a BufferedReader signals end of file by...
A. throwing an exception
B. returning a -1 value
C. returning a null value
D. none of these -- you have to call another method to test for end of file.

11. What is the return value of the expression "I heart CS".split("\\W") ?
A. A String containing the original value minus the spaces
B. An array of Strings of length 3
C. An array of Strings of length 10
D. A List of Strings
E. An int

12. Which of the following is not an interface of the Java Collections framework?
A. Collection
B. HashMap
C. Iterator
D. List
E. Set

13. Appropriate as the main window of an application: __________.  Appropriate as an area to draw shapes on: ________.   Appropriate as an area to hold other components: __________.
A. JFrame...JFrame ... JFrame only
B. JFrame...JPanel ... both
C. JPanel...JPanel   ... JPanel only
D. JPanel...JFrame ... JFrame only
E. JPanel... JFrame... both

14. To cause a rectangular shape to be colored red, the correct technique is...
A. use the setColor method of that rectangle object
B. use the setColor method of the container (such as the JPanel) in which the rectangle will be drawn
C. use the setBackground method of the container in which the rectangle will be drawn
D. use the setForeground method of the container in which the rectangle will be drawn
E. use the setColor method of the Graphics object used for drawing the rectangle

15. which is one of the Painter's Rules?
A. Always use Swing instead of AWT
B. Draw just the part of the picture which has changed since the last paintComponent
C. Call the superclass paintComponent first
D. Call the superclass repaint first
E. Call the superclass repaint (not necessarily first)

16. The general approach to deriving a complexity formula is to count the number of steps needed to execute an algorithm.  The philosophy behind this is best characterized as
A. taking specific computer hardware as the primary consideration
B. taking a specific operating system or the software platform as the primary consideration
C. basing the formula on concrete timings and measurements
D. avoiding dependence on specific hardware, operating system, or timing measurements






17. (each part worth 1 MC question)  Suppose the string allOfMe contains the entire original text of Shakespeare's Romeo and Juliet, including spaces, punctualtion, line feeds, etc.  Obviously, some words (like "and") occur many times, whereas others may occur only once.  It would be interesting to know how many different words there are (counting words with different cases as different words).  Give brief description or code to show how each step can be done (ideally, done in a simple manner taking advantage of features of the Java Collections framework).

 1. Get all the words, in their original order, into an array, one word per entry, with spaces and punctuation removed.




2. Get all the words into a List (still including duplicates)




3. Get all the words into some kind of collection, with duplicates removed




4. Determine how many (different) words there are in that collection




18
. Segmented lists (worth 10 MC questions)

A segmented list is an implementation of the List interface which uses multiple linked lists to give the illusion of a single list. 
magician with rabbit

When the segmented list is first created, an empty linked list is created (call this sublist LL1), and new elements are added to it, until LL1 reaches some length.  On the next add, a second sublist (say, LL2) is created, and new elements are added to that list.  When LL2 reaches a certain length, LL3 is created on the next add, and so forth.  A master keeps track of the individual sublists.   When an element is deleted, it is removed from the sublist it is in, but no changes are made to the master or other sublists.  Because of deletions, the sublist are not all necessarily of the same length.

To test your understanding, draw an informal picture of what a segmented list would look like if it currently contained 202 objects, and had 3 LLs, containing 100, 99, and 3 objects respectively.  Be sure to show the master list in your picture.  This doesn't have to be a full or detailed picture -- enough to convince us you have the idea.







Compared to a regular linked list, which operation would be most significantly speeded up, in most cases?  (Answer here, and not on the Scantron form):
-- add( )
-- get(position)
-- contains(object)
-- clear( )
-- new
On the next page is the start for a possible implementation.  Given this start, implement the two methods add and get as specified there. Don't assume there are ANY instance variables other than those shown.  Don't assume any other methods of this class have been implemented (but you can call standard Java methods on other objects if appropriate).

Write the methods on that page and/or on this page.








public class SegmentedList implements List {

    /** A list of the sublists, in order of their creation. */
    private List master = new ArrayList();

    /** Number of objects currently stored in the list */
    private int size = 0;

    /** Longest desired length of any sublist. */
    public static int MAX_SUBLENGTH = ...;

     /** Add the object to the end of the list. 
    Return true if the add succeeded, false otherwise. */

    public boolean add(Object newObject) {...   /// IMPLEMENT THIS METHOD

     /** Retrieve the object stored in the given position of the list.
     @throws IndexOutOfBounds if the index is <0 or >= size
     */
     public Object get(int position) {...  /// IMPLEMENT THIS METHOD
...
}