UCLA - PIC 20A
JAVA and Internet Programming, Computation & Visualization
Final Exam Study Guide Solutions, Thu., Aug. 15, 2002, 10:30-11:20AM, MS
6229
I. Theoretical Component
- Abstract classes vs. Interfaces (differences and similarities)
- An interface cannot implement any methods, whereas an abstract class can.
- A class can implement many interfaces but can have only one superclass.
- An interface is not part of the class hierarchy. Unrelated classes can implement the
same interface.
- Interfaces can't grow, since this may break the definition of classes already implementing the old version of the interface.
- How are Java Interface objects different from classes, in general?
- Interfaces are not part ot the Java ancestors/descendents class tree-hierarchy
- An object can iherit from at most one class, but can implement a number of interfaces
- What is a package, why do we need it and how do we use it?
- A collection of related classes and interfaces providing access, protection and namespace management.
- To easily determine that these classes and interfaces are related.
- To know where to find classes and interfaces that provide graphics-related functions.
- The names of your classes won’t conflict with class names in other packages, because
the package creates a new namespace.
- You can allow classes within the package to have unrestricted access to one another
yet still restrict access for classes outside the package.
- package edu.ucla.pic.pic20a.projects;
- What are exceptions and call-stacks?
- An exception is an event that disrupts the normal flow of instructions during the
execution of a program.
- The list of methods called from the start of the program to the point where an exception occurred is known as the call-stack.
- Data structures: Stack (LIFO), queue (FIFO), linked-list.
- What is the catch-or-specify requirement in exception handling?
- A method must either catch or specify all checked (non-runtime) exceptions that can be thrown by that method.
- Runtime exceptions occur within the Java runtime system (e.g., dividing
by zero). A method does not have to catch or specify runtime exceptions,
although it may.
- Nonruntime exceptions (checked exceptions) occur in code outside
of the Java runtime system. For example, exceptions that occur during I/O
are nonruntime exceptions. The compiler ensures that nonruntime exceptions
are caught or specified.
- Exceptions that can be thrown by a method
- Any exception thrown directly by the method with the throw statement
- Any exception thrown indirectly by calling another method that throws an exception
- What is the rationalle for throwing exceptions from one method/class to another in the call-stack framework?
- To make the handling of exceptional events client application-specific!
- What's the proper order in catching exceptions?
- Most specific exceptions objects should be cought first. More general ansestors in the Exception class hierarchy should be handled last
- State the basic advantages of using exceptions. Can exceptions be used for non-exceptional flow of control redirection? Should they be used this way?
- Separate the details of what to do when something out of the ordinary happens
- Propagate error reporting up the call stack of methods
- Grouping and differentiating error types
- Yes, exceptions can be used for general flow-of-controll, however such use is discouraged
- What is a thread?
- A thread is a single sequential flow of control within a program.
- What are the two ways for defining a run method for a thread. How do we decide which approach to take?
- Subclassing Thread and overriding its run method
- Have your class implement the Runnable interface
- If your class must subclass another class (e.g., Applet ), you should use Runnable, otherwise it's your call
- Is the highest priority thread the one always running on the CPU?
- At any given time, the highest-priority thread should be runningn
but this is not guaranteed. The thread scheduler may choose to run a lower-priority
thread to avoid starvation. Thus thread priority only to affect scheduling
policy for efficiency purposes. Do not rely on it for algorithm correctness.
- What are the divisions of the I/O stream classes? Which is the most common type of a stream programs use? Why?
- Two class hierarchies, based on the data type (either character or byte streams)
- Most programs should use readers and writers to read and write textual (character
) information they can handle any character in the Unicode character set,
whereas the byte streams are limited to ISO-Latin-1 8-bit bytes.
- The basic specific streams are: File streams; Pipe streams; Filter streams
- What is object serialization?
- The key to writing objects to a stream is to represent its
state in a serialized form sufficient to reconstruct the object as it is
read. Thus, reading (ObjectInputStream) and writing objects (ObjectOutputStream)
is a process called object serialization. Object serialization is used in
the following ways:
- Remote method invocation (RMI)—communication between objects via sockets.
- Lightweight persistence—the archival of an object for use in a later invocation of the
same program.
- Transient and static variables are not serialized or deserialized.
- Object serialization requires that the ObjIOStreams are constructed on other streams.
- FileInputStream in =new FileInputStream(“theTime ”);
ObjectInputStream s =new ObjectInputStream(in);
- Serialize instances of aclass using defaultWriteObject method of ObjectOutputStream . This method automatically writes out everything required to reconstruct an instance of the class, including the following:
- Class of the object & Class signature
- Values of all non-static members, including members that refer to other objects.
- Deserialize any instance of a class using defaultReadObject method in ObjectInputStream.
- Customize serialization for your classes by providing: writeObject and readObject.
- What is required for a Java event handler to work properly?
- The event handler class definition requires one line of code specifies
that the class either implements a listener interface or extends a class
that implements a listener interface. E.g.,
- public class MyClass implements ActionListener { ...}
- Register an instance of the event handler class as a listener on one or several components. E.g.,
- someComponent.addActionListener ( instanceOfMyClass );
- he event handler class should have a few lines of code implement
the methods required by the the corresponding listener interface. E.g.,
- public void actionPerformed (ActionEvent e) { ... //code that reacts to the action ... }
II. Applications Component
- Define an interface Shape and include a couple of members of types that make sense for this interface (constants, variables?, methods).
- What is the general format for defining and implementing an interface?
Can interfaces be instantiated? Can an interface be used as a reference type
in function calls?
- public interface InterfaceName extends SuperInterfaces { InterfaceBody }
- public class StockApplet extends Applet implements StockWatcher {
....
public void valueChanged (String tickerSymbol,double newValue) { ; }
}
- Can't instantiate interfaces, or any abstract class, for that matter!
- public void watchStock (StockWatcher watcher, String tickerSymbol, double delta) { ... }
- Give a simple example of constructing and importing a package
- package package edu.ucla.pic.pic20a.projects;
public class Proj5_1 extends Graphic implements Draggable { ... }
- import package edu.ucla.pic.pic20a.projects;
- What is the general form for the exception handling mechanism in Java?
- try { ... } catch (ExceptionType1 name ) { ... } catch (ExceptionType2 name ) { ...} ... finally { ... }
- Give some general (heuristic) examples of where using threads may be very beneficial
- Separate mouse, key and drawing functionality in a GUI interface, so that all can run in synchrony
- Web-browser: scroll a page while it’s downloading an applet, play
animation and sound concurrently, print a page in the background, etc.
- Give an example of creating, starting and terminating a thread in an Applet class
- Thread clockThread =null;
public void start() { if (clockThread ==null) { clockThread =new Thread
(this,"Clock");
clockThread.start();
}
}
public void run(){ Thread myThread =Thread.currentThread();
while (clockThread == myThread){ .... }
}
public void stop(){ clockThread =null; }
- Show an example of creating a group of threads. Why is this useful?
- ThreadGroup myThreadGroup =new ThreadGroup("PIC 20A Threads");
Thread myThread1 = new Thread (myThreadGroup, "myThread1 in the group"); Thread myThread2 = new Thread (myThreadGroup, "myThread2 in the group");
- Thread group provides a mechanism for collecting multiple threads
into a single object and manipulating those threads all at once rather than
individually (e.g., run/priority/wait).
- Write a simple file I/O stream that copies the content of one file into another
- import java.io.*;
public class TextFileCopier {
public static void main(String [] args)) throws IOException {
File inputFile =new File("InputFile.txt");
File outputFile =new File("OutputFile.txt");
FileReader in =new FileReader(inputFile);
FileWriter out =new FileWriter(outputFile);
int c;
while ((c = in.read() )!=-1) out.write(c);
in.close(); out.close();
}
}
- Construct a JButton object, instantiated it and provide an action handler that prints button clicked, upon click
- JButton button =new JButton("ClickMe");
button.addActionListener ( new ActionListener( ) {
public void actionPerformed(ActionEvent e){ System.err.println("button clicked"); }
});
- Which of the following is not an event listener; ActionListener,
WindowListener, MouseListener, MouseMotionListener, IOListener, ComponentListener,
FocusListener, ListSelectionListener?
- List 10 Java Swing components frequently used as widgets added in Java GUIs
- JPanel, JFrame, JBotton, JList, JTextField, JCheckButton, JRadioButton,
JMenu, JOptionPane, JDialog, JTextArea, JComboBox, JSlider, JProgressBar.
- List the basic layout managers
- BorderLayout (default), BoxLayout, FlowLayout, GridLayout, GridBagLayout
Last modified on
by
.
\Ivo D. Dinov, Ph.D., Departments of Statistics, Neurology &
Program in Computing, UCLA School of Medicine/