Files
GTSchoolShit/CS1322/t4/t4_answers.txt
2025-06-07 01:59:34 -04:00

350 lines
8.4 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.
e.hasMoreElements(); nextElement();
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();
while(st.hasMoreTokens()){
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?
3,7,13,15,12,9,18,30,21,17
7) An in-order traversal?
3,7,9,12,13,15,17,18,21,30
8) A pre-order traversal?
17,9,7,3,12,15,13,21,18,30
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. */
int iKey = Math.abs(key.hashCode())%hTable.length();
if(hTable[iKey] != null){
return true;
}
else {return false;}
}
14) public int numberOfElements(){
/* Returns the a count of the number of nodes in the hashtable */
int count = 0;
for(int i=0; i<hTable.length; i++){
HashNode temp = hTable[i];
while(temp != null){
count++;
temp = temp.getNext();
}
}
return count;
}//end
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?
Buffers help with streams. Streams are the base Objects used for I/O, buffers
act as wrapper classes that make file I/O more efficient and easier to use.
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. */
String text;
try{
text = br.readLine();
}
catch(IOExcemption ioe){
System.err.println("Error in readLine()");
text = null;
}
finally{
return text;
}
}
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.
{
BufferedReader KB_IN = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the file to read from: ");
String strFileRead,strKeywords,strWriteTo;
try{
strFileRead = KB_IN.readLine();
FileInputStream fis = new FileInputStream(strFileRead);
BufferedReader FILE_IN = new BufferedReader(fis);
}
catch(FileNotFoundException fnfe){
System.err.println("File Not Found! -- parseFile()");
}
catch(IOException ioe){
System.err.println("IO Exception -- parseFile()");
}
System.out.println("Please enter the keywords to search for: ");
try{
strKeywords = KB_IN.readLine();
Vector v = new Vector();
StringTokenizer st = new StringTokenizer(strKeywords);
while(st.hasMoreTokens()){
v.add(st.nextToken());
}
catch(IOException ioe){
System.err.println("IO Exception -- parseFile()-2");
}
System.out.println("Please enter the file to write to: ");
try{
strFileTo = KB_IN.readLine();
PrintWriter pw = new printWriter(new BufferedWriter
(new FileWriter(strFileTo)));
}
catch(IOException ioe){
System.err.println("IO Exception -- parseFile()-3");
}
try{
...
boring enough already
...
}
catch(IOException ioe){
System.err.println("IOException -- parseFile()-4");
}
}
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 - added "celler" to end of hashTable for purposes of collisions
o Load Factor
o Rehash - to resize the hashTable to make it smaller or larger
o Collision - when 2 items have the same key in a hashTable
o Enumeration
o StringTokenizer
o Binary Tree
o Binary Search Tree
o Preorder Traversal
o Postorder Traversal
o Inorder Traversal