Use the menu on the left to view sample questions.
To see the answer, click the
icon.
Sometimes there are follow-up questions, also accessible through
the
icon.
Other times, the answer is available through a menu selection.
What mechanism does Java use for passing both primitive and object
reference parameter types?
- Call by value (parameter copying)
Why must arithmetic methods like square root be referred to in Java like
Math.sqrt instead of just
sqrt?
- Because they are static methods and thus
are called without an implicit parameter.
Exceptions
What is the difference between checked and unchecked exceptions in Java?
- Checked exception conditions are detected at
compile time and the programmer
is forced to handle or throw them. Unchecked exception conditions are not
detected.
Give an example of each.
- I/O exceptions like "file not found" are checked. Run-time exceptions like
"null pointer" are not checked.
The class below sums up integers from the command line and prints the total
to standard output:
The program works given valid input:
49% java Adder 1 2 3 4 5
The total is 15
But bad input causes the following behavior:
50% java Adder 1 2 3 foo 4 bar 5
Exception in thread "main" java.lang.NumberFormatException: For input string: "foo"...
Rewrite the class and, using only
try and
catch, handle
the exception so that the following behavior is obtained:
57% java Adder 1 2 3 foo 4 bar 5
Ignoring bad input: foo
Ignoring bad input: bar
The total is 15
Do not use any
if statements.
The class below prompts the user for
a file name and then echoes the file to standard output:
However, as it stands the class will not compile, because a possible
FileNotFoundException is not handled.
Rewrite the
echoFile method so that if the file is not
found the message "
Error -- File fileName not found." is
printed to the screen and the user is prompted again.
The class below is presented without any comments.
Add complete
javadoc documentation for each of its public members.
What is the result of executing the following:
Suppose a new message forwarding feature is added to the voice mail
system, so that the main menu has a new option,
Turn on Forwarding.
This option allows a mailbox owner to select an alternate mailbox to
receive messages.
Write a complete
Turn On Forwarding use case.
-
Turn on Forwarding
1. Mailbox owner carries out Log In
2. Mailbox owner selects "Turn On Forwarding" menu option
3. Voice mail system speaks:
Enter forwarding mailbox number followed by #
4. Mailbox owner types in extension number.
5. Mailbox owner presses "#" key.
6. Voice mail system sets the forwarding mailbox number.
Write a variation on the Turn On Forwarding use case.
-
Variation #1. Invalid forwarding mailbox
1.1 Start at step 4.
1.2 Mailbox owner types an invalid extension number
1.3 Mailbox owner presses "#" key.
1.4 Voice mail system speaks:
Invalid extension number
1.5 Continue with step 3
Another possible variation: hanging up before confirmation.
Class Relationships
Describe the class relationship known as
Dependency.
-
When a class "uses" another class: a
class method has another class type
as a parameter or return value, or it constructs an object of
another class.
Suppose class Foo depends on class Bar. Draw how this
is depicted in UML.
-
Describe the class relationship known as
Aggregation.
-
When a class object "has" an object of
another class type as part of its state: the class has an
instance field that is another class type or a collection of another
class type
Suppose that a variable number of instances of class Bar are
stored in an object of class Foo. Draw how this
is depicted in UML.
-
Describe the class relationship known as
Inheritance.
-
When an object that is an
instance of one class "is" also an instance of another:
the class is a subclass of, or extends, the other
Suppose that Foo is a subclass of Bar. Draw how this
is depicted in UML.
-
Describe the
Implementation relationship that holds between a
class and an interface.
-
When a class provides definitions
(implementations) for the methods whose signatures are specified
in the interface.
Suppose that class Foo implements the interface Bar. Draw how this
is depicted in UML.
-
Consider again the
SimpleMath class below. Suppose that an
object named
simpleMathClient executes the following code.
Draw the UML sequence diagram that models this code.
Name and describe five criteria used to analyze the quality of an
interface to a class.
-
Cohesion: all operations provided by the interface revolve around a common purpose
-
Completeness: all operations that make sense for the class are
supported by the interface
-
Convenience: the interface makes common tasks simple
-
Clarity: the interface does not confuse programmers
-
Consistency: the operations for an interface have matching names,
parameters, return values, and behavior
For each of the following types encountered so far in labs and
assignments, indicate whether it is an interface, abstract class, or
concrete class:
Console:
-
Concrete class
ParenthesisCalculator:
-
Concrete class
AbstractMatrix:
-
Abstract class
ArrayListImplementation:
-
Concrete class
Matrix:
-
Interface
BankAccount:
-
Concrete class
For each of the following types encountered so far in the Java standard
library, indicate whether it is an interface, abstract class, or
concrete class:
ArrayList (in
java.util):
-
Concrete class
List (in java.util):
-
Interface
Runnable (in java.lang):
-
Interface
Thread (in java.lang):
-
Concrete class
String: (in java.lang):
-
Concrete class
Object (in java.lang):
-
Concrete class
Consider the following interface type:
Classes that implement
Filter supply criteria used to accept the object
x. For example, the
StringFilter class accepts strings that
are three characters or less in length. If
sf is
a
StringFilter object, then
sf.accept("abc")
returns
true, while
sf.accept("abcd")
returns
false.
Write the
StringFilter class. Recall that the
String class has
a method of no arguments called
length that returns the length of the
implicit argument.
Describe what we mean by the following object-oriented design concepts.
Encapsulation:
-
Separating how a class's data
is represented from how the class is used
Abstraction:
-
Hiding the details of how a class is
implemented from users of the class
Polymorphism:
-
Using interface-typed variables so that code
can have different behavior depending on the types of objects
they reference at run-time
What is a
thread?
-
A program unit that is executed independly of
other parts of the program
How is a thread executed?
-
An object implementing the Runnable
interface is given to a Thread object, whose start
method is called
To create the illusion of threads running in parallel, what must
every thread do?
-
Occasionally yield control using Thread.sleep
How should a thread be terminated?
-
By either allowing its Runnable's run method to
finish, or interrupting it (don't use the deprecated stop method)
Describe the four possible thread states.
-
- – new: before start is called
- – dead: after run exits
- – blocked: while sleeping or waiting for I/O or
other events
- – runnable: when neither new, dead, nor blocked
How do race conditions occur?
-
When the effect of multiple threads on shared
data depends on the order in which the threads are scheduled
How can race conditions be avoided?
-
A thread can acquire a lock, which
causes any other thread to block until the lock is released
What are the two kinds of locks?
-
- – Objects implementing the java.util.concurrent.Lock
interface, e.g. ReentrantLock
- – Locks built into every object
What is the effect of declaring a method to be synchronized?
-
Upon entry to the method, the object's
built-in lock is acquired. Upon exit, the lock is automatically
released
What is deadlock?
-
When no thread can proceed because each
thread is waiting for another to do some work first, including
releasing a lock
How is deadlock avoided?
-
- – For explicit locks (e.g. using ReentrantLock),
create Condition objects and use await and signalAll
- – For implicit locks (created using synchronized)
use wait and notifyAll
For each of the following subtype claims, indicate whether true or
false, and why:
MatrixException is a subtype of
Matrix
-
False. MatrixException does not
implement Matrix, nor does any of its superclasses
Matrix is a subtype of AbstractMatrix
-
False. The inverse is true,
since AbstractMatrix implements Matrix
ArrayImplementation is a subtype of AbstractMatrix
-
True. ArrayImplementation is a direct
subclass of AbstractMatrix
ArrayListImplementation is a subtype of Matrix
-
True. ArrayListImplementation is a
subclass of AbstractMatrix and AbstractMatrix
implements Matrix
SimpleCalculator is a subtype of PrecedenceCalculator
-
False. The inverse is true,
since PrecedenceCalculator is an indirect subclass
of SimpleCalculator
ParenthesisCalculator is a subtype of SimpleCalculator
-
True. ParenthesisCalculator is an
indirect subclass of SimpleCalculator
ParenthesisCalculator is a subtype of Console
-
True. ParenthesisCalculator is an
indirect subclass of SimpleCalculator,
and SimpleCalculator is a direct subclass
of Console
NoPrecedenceCalculator is a subtype of java.lang.Object
-
True. All non-primitive types are subtypes
of java.lang.Object
java.util.List is a subtype of java.util.Collection
-
True. java.util.Collection is a
superinterface of java.util.List
Suppose
c refers to an object. If "
c instanceOf
PrecedenceCalculator" returns
true, what kind of object
does
c refer to?
-
Either PrecedenceCalculator
or ParenthesisCalculator
How would you test for whether c refers to
a PrecedenceCalculator object only?
-
Use the test "c.getClass() ==
PrecedenceCalculator.class"
In what class is the getClass method found?
-
java.lang.Object
What does the getClass method return?
-
An instance of java.lang.Class
If the variables
a and
b refer to class
objects and the value of
a==b is
true,
what can you say about
a and
b?
-
They are references to the same, identical
object
The equals method is defined in java.lang.Object, so it
is inherited by all classes. Why is it a good idea to override it?
-
The inherited equals only checks for
object identity (this == other),
while object equality usually asks wether two separate objects
have the same state
What should every overridden equals method do before it
compares two objects' state?
-
Check to make sure the class of the other
object is the same as the class of this object,
using getClass, then cast the other object to this
object's class
The
toString method is defined in
java.lang.Object, so it
is inherited by all classes. Why is it a good idea to override it?
-
The inherited toString only returns the
string representation of the object's class and memory
location. Since toString is often used for debugging, this
behavior is of limited value.
The
clone method is defined in
java.lang.Object, so it
is inherited by all classes. When would it be a good idea for a
programmer to override it in his or her own class?
-
When the class has instance fields that are
themselves class objects. The inherited clone does not do an
unlimited deep copy; any instance fields that are class objects
are not cloned. Therefore an object and its clone would share
structure, causing problems if a field is mutable.
Why does the inherited clone not clone instance fields that
are objects?
-
It wouldn't work for cyclic structures.
What three things must programmers do when overriding clone
to prove they know what they are doing?
-
- – Declare the overridden clone to be public
- – Make their class implement the Cloneable interface
(which has no methods)
- – Handle the checked CloneNotSupportedException
What does it mean to
serialize an object?
-
An object, since it can have other objects as
instance fields, can have arbitrary levels of structure. To
serialize an object is to convert it to a "flat" stream of bytes.
When would it be appropriate to serialize an object?
-
When the object needs to be saved in a file
or sent across a communication link.
What must programmers do to make their objects serializable?
-
- – Make their class implement the Serializable interface
(which has no methods)
- – Ensure that the classes of any instance fields also
implement Serializable
What mechanism is used to actually serialize an object at run-time?
-
Use the writeObject method in
the ObjectOutputStream class (from package java.io)
Show the first line of a class header for class
MyClass that
takes a type parameter
E.
-
Suppose MyClass stores a value of type E in
field x obtained through its constructor. Show the
constructor and field declarations.
-
Describe Java's
reflection capability.
-
Reflection is the ability of a running program
to find out about its objects and classes, including details about
constructors, methods, and fields.
When would reflection be useful?
-
Reflection can be used by integrated development environments
(IDEs) like NetBeans to implement structure editors and class
navigators. It is also useful for unit testing tools like JUnit to
isolate and run test methods.