267 lines
6.1 KiB
Plaintext
267 lines
6.1 KiB
Plaintext
Test 4 Review Questions
|
|
-------------------------------------
|
|
References, Parameters, and the like
|
|
-------------------------------------
|
|
|
|
1) Given the following classes:
|
|
public class IntHolder {
|
|
|
|
private int intHeld;
|
|
|
|
public int getIntHeld() {
|
|
return intHeld;
|
|
}
|
|
|
|
public void setIntHeld(int intHeld) {
|
|
this.intHeld = intHeld;
|
|
}
|
|
|
|
public IntHolder(int intHeld) {
|
|
setIntHeld(intHeld);
|
|
}
|
|
|
|
public String toString() {
|
|
Integer temp = new Integer(getIntHeld());
|
|
return temp.toString();
|
|
}
|
|
}
|
|
|
|
public class Test {
|
|
|
|
public static void squareInt (int in) {
|
|
in = in * in;
|
|
}
|
|
|
|
public static void squareIntHolder (IntHolder in) {
|
|
in.setIntHeld(in.getIntHeld() * in.getIntHeld());
|
|
}
|
|
|
|
public static void main(String args[]) {
|
|
int x = 5;
|
|
IntHolder y = new IntHolder(5);
|
|
|
|
System.out.println("x is holding: " + x);
|
|
System.out.println("y is holding: " + y);
|
|
|
|
squareInt(x);
|
|
squareIntHolder(y);
|
|
|
|
System.out.println("x is now holding: " + x);
|
|
System.out.println("y is now holding: " + y);
|
|
}
|
|
}
|
|
|
|
What output is produced when the class Test is run using 'java Test'? Explain?
|
|
|
|
|
|
|
|
|
|
----------------------
|
|
Exceptions
|
|
----------------------
|
|
|
|
2) Compare and contrast checked and unchecked exceptions:
|
|
|
|
|
|
|
|
3) Given the following code:
|
|
|
|
public class ExeptionExample {
|
|
public static void main(String args[]) {
|
|
BufferedReader br;
|
|
FileReader fr;
|
|
PrintWriter pr;
|
|
FileWriter fw;
|
|
|
|
try {
|
|
fr = new FileReader("input2.txt");
|
|
br = new BufferedReader(fr);
|
|
}
|
|
catch (FileNotFoundException e) {
|
|
System.out.println("Caught one!");
|
|
}
|
|
catch (IOException e) {
|
|
/* do nothing */
|
|
}
|
|
|
|
try {
|
|
fw = new FileWriter("output2.txt");
|
|
pr = new PrintWriter(fw);
|
|
}
|
|
catch (FileNotFoundException fnfe) {
|
|
System.out.println("Caught two!");
|
|
}
|
|
catch (IOException e) {
|
|
/* do nothing */
|
|
}
|
|
finally {
|
|
System.out.println("I'm late!");
|
|
}
|
|
}
|
|
}
|
|
|
|
and the following directory listing:
|
|
|
|
sanjay@griff:/home/sanjay/test$ ls
|
|
input.txt output.txt ExceptionExample.class
|
|
|
|
What is the output when ExceptionExample is run?
|
|
|
|
|
|
|
|
------------------
|
|
Useful Tools
|
|
------------------
|
|
|
|
4) What methods are defined by the Enumeration interface?
|
|
Describe what each of these methods does.
|
|
|
|
5) Implement the method tokenize which will take in a string of input and a
|
|
string of delimiters and output an Enumeration of tokens contained in the
|
|
input. You may find such classes as StringTokenizer, Enumeration, and Vector
|
|
helpful.
|
|
|
|
public Enumeration tokenize(String input, String delim) {
|
|
StringTokenizer st = new StringTokenizer(input);
|
|
Vector v = new Vector();
|
|
v.add(st.nextToken(delim));
|
|
|
|
return v.elements();
|
|
}
|
|
|
|
|
|
--------------------
|
|
Binary Search Trees
|
|
--------------------
|
|
|
|
Given the following BST:
|
|
|
|
|
|
17
|
|
/ \
|
|
/ \
|
|
/ \
|
|
9 21
|
|
/ \ / \
|
|
7 12 18 30
|
|
/ \
|
|
3 15
|
|
/
|
|
13
|
|
|
|
6) What would be the output of a post-order traversal?
|
|
|
|
|
|
7) An in-order traversal?
|
|
|
|
|
|
8) A pre-order traversal?
|
|
|
|
|
|
9) a. Draw the resulting tree if the number 10 were added
|
|
b. Draw the resulting tree if the number 22 were added to the original tree
|
|
c. Draw the resulting tree if the number 32 were added to the original tree
|
|
d. Describe the algorithm you used to arrive at your conclusion.
|
|
|
|
|
|
---------------------------
|
|
Hashtables
|
|
---------------------------
|
|
|
|
10) What is a hashtable?
|
|
|
|
11) What are the advantages of a hashtable over other data structures?
|
|
(i.e.Why would you ever use this?)
|
|
|
|
12) A hashtable resembles a combination of two data structures. What are
|
|
those data structures?
|
|
|
|
For the following questions, assume you have all the methods from p3:
|
|
|
|
|
|
Write the following methods ...
|
|
|
|
13) public boolean contains(Object key)
|
|
/* Returns true if the data with the given key is found in the hashtable */
|
|
/* returns false otherwise. */
|
|
|
|
14) public int numberOfElements()
|
|
/* Returns the a count of the number of nodes in the hashtable */
|
|
|
|
15) public boolean equals(Object o)
|
|
/* Returns true if the Object passed is identical to current table */
|
|
/* Notice how for any class, all the equals method has the same */
|
|
/* signature ... */
|
|
|
|
|
|
---------------------------
|
|
File I/O
|
|
---------------------------
|
|
|
|
16) What is file I/O?
|
|
|
|
17) Why would you use file I/O?
|
|
|
|
18) What's the difference between a buffer and a stream?
|
|
Which one helps "handle" the other?
|
|
|
|
Given a class Parser:
|
|
|
|
public class Parser{
|
|
private BufferedReader br, inputFile;
|
|
private PrintWriter outputFile;
|
|
|
|
public Parser()
|
|
{
|
|
br=new BufferedReader(new InputStreamReader(System.in));
|
|
}
|
|
/* YOUR METHODS WILL GO HERE */
|
|
}
|
|
|
|
19) public String readLine()
|
|
/* A method that will read and return in a line from System.in. */
|
|
|
|
20) public int parseFile()
|
|
// In this method you will prompt the user for three values:
|
|
//
|
|
// 1) The file to read from
|
|
// 2) The keywords to search for
|
|
// 3) The file to write to.
|
|
|
|
//Each of the three values will be read as a String. You must then open the
|
|
//file that you were asked to open and read from it. The file will consist
|
|
//of multiple lines, each containing a single word. You must count the number
|
|
//of times that the user specified keyword appears in the file. Then you must
|
|
//write out all of the words except for the keywords into the specified output
|
|
//file. The method should return the number of occurences of the keyword that
|
|
//were read read from the file.
|
|
|
|
21) Define the following terms:
|
|
|
|
o Exception
|
|
o Try-catch
|
|
o Finally
|
|
o Throw
|
|
o File IO
|
|
o Decorator class
|
|
o Stream
|
|
o Standard Output
|
|
o Standard Input
|
|
o Standard Error
|
|
o Hashtable
|
|
o External Chaining
|
|
o Quadratic Probing
|
|
o Linear Probing
|
|
o Coalesced Chaining
|
|
o Load Factor
|
|
o Rehash
|
|
o Collision
|
|
o Enumeration
|
|
o StringTokenizer
|
|
o Binary Tree
|
|
o Binary Search Tree
|
|
o Preorder Traversal
|
|
o Postorder Traversal
|
|
o Inorder Traversal
|
|
|