The answers for questions 7 to 12 are in final-review-question-answers.txt.
- Define the following terms:
wrapper class autoboxing iterator hash code
Ans: Wrapper Class: each of the primitive types has a wrapper class that contains methods that can be executed for the primitive types.
An ArrayList or HashMap object can only contain objects, so this is a way to store primitive datatypes.
Autoboxing: when a primitive type is converted to a wrapper class like this:
Integer n = 5;
instead of the older way like this
Integer n = new Integer(5);
Iterator: an object that returns items sequentially in order. In order for an collection class col to be used in a for statement like this:
for(Object obj : col) {
// process each obj in the collection.
}
col must have an iterator.
Hash code: the hashCode method of an object determines where the object will be inserted into a hash map.
- What are the allowed Java escape characters? Ans:
\n \t \f \r \b \\ \" \'
- What are these Java keywords?
assert break case continue final finally
instanceof package protected static
Ans:
assert: Used for testing. If in "assert expr;", expr is true, then nothing happens or the assert passes.
If, however, the value is false, an exception is raised or the assert fails.
break: used to break out of a loop or a switch statement. break statements are usually required in switch statements.
case: used to designate one of the cases corrsponding to a constant value of a switch statement.
continue: in a loop it means to jump to the bottom of a for loop and then continue with the next iteration of the loop.
final: designates a constant like this public final int maxCapacity = 450;
instanceof: used to determine whether a variable on the left is an instance of the type on the right.
For example p instanceof Person returns true if p is a Person object.
instanceof is an alternative to p.getClass( ).getName( )
package: a collection of related classes.
protected: a protected method can be used in the current class or in any derived class.
static: A static method is a class method. It can be called using the class name and does
not require an object to call it.
- What is the difference between a method argument and a method parameter. Ans:
Ans:
arguments are passed in when the method is invoked. Parameters are
placeholders in the method definition that show the method what to do with the
arguments.
In modern programming languages terminology, an argument is called an
actual parameter; a parameter is called a formal parameter.
- Explain the difference between overloading and overriding.
Ans: overloading means same class, same name, different signatures; overriding means different classes, same name, same signature.
- Why do you need to think like an executive to write recursive programs?
Ans: The executive does the easy part like moving the bottom ring in the Towers of Hanoi problem, or
filling in the start square with 1 in the Flood Fill algorithm. The subordinates (recursive calls)
do the hard work.
- Look up the Character class in the Java Class Library documentation. Find five interesting
methods and test them.
- Write a recursive method named intToWords that converts an integer less than one billion to words.
- Write a main method that reads a list of strings, adding them to an array list. Then print the
words back in reverse order.
- Declare an interface Filter like this:
public interface Filter {
boolean accept(Object x);
}
- Write a method with the header
public static ArrayList<Object> collectAll(
ArrayList<Object> objects, Filter f)
that returns all the objects in the array list that are accepted by the given filter.
Provide a class ShortWordFilter, whose filter method accepts all strings of length
< 5. Then write a main method that reads all words from System.in, puts
them into an array list, calls collectAll, and prints a list of the words in the
returned array list.
- Write classes Point and PointList.
The Point class has the instance variables
int x and int y and method
toString, with getters and setters for x and
y.
The PointList has an array list to hold the points and these methods:
add, display, graph, getCount, toString.
Points are only added to the array list if they are between 0 and 19.