Download Hourly #2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COSC 211
Test #2
3/12/08
Name:
Open book, open notes, open Internet. You may use the computer at your station or your
own computer.
1. Consider this class. When executed, it returns 28. The following questions relate to
executing this.
public class Q1 {
public static void main (String[] args) {
System.out.println(h(5));
}
public static int h(int n) {
System.out.print("#");
if (n <= 1) return 1;
return (2 * h(n/2) + 2 * h((n+1)/2));
}
}
1. A. Give the activation tree, or a linear trace (or any other trace that clearly shows the
stack frames with parameter values and return values.
1. B. How many times is h( ) invoked altogether (the parameter n can have any value)?
1. C. How many times is h(1) invoked?
1. D. How many times is h(4) invoked?
2. Consider this class. What is the output?
public class Q2 {
public static void main(String[] args) {
cheers(6);
}
public static void cheers(int n) {
if (n==1) {
System.out.print("Hurray");
}
else {
System.out.print("Hip ");
cheers(n-1);
}
}
}
3.Using recursion, compute the sum of all values in an array. ‘. . .’ is a placeholder for
parameter lists or one or more statements. Give the program.
public class Q3 {
public static void main(String[] args) {
int[] a = {3, 2, 3};
System.out.println(
"the sum of the elements: " +
getSum( . . .));
}
public static int getSum( . . .) {
. . .
}
}
4. P(n), the n-th Padovan number is defined as follows:
P(0) = P(1) = P(2) = 1
P(n) = P(n-2) + P(n-3)
4. A. Give a recursive function (in Java) that computes the n-th Padovan number.
4. B. What is P(22)?
5. A. Write some code that will create a stream named oStr that is a member of the
class PrintWriter, and that connects this stream to a text file. The name of the text
file is in the String variable fName. Do this in a way such that the file named in fName
always starts empty, so that if there already is a file with that name, the old contents are
lost.
5. B. Write some code that will create a stream named iStr that is a member of the class
Scanner. It should connect the stream to a text file that is named in the String variable
fName so that your program can read input from the text file.
5.C. The nextInt() method of the Scanner class can throw these exceptions:
InputMismatchException - if the next token does not match the
Integer regular
expression, or is out of range
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
5.C.1. Could
int x =
iStr.nextInt(); be a valid statement?
5.C.2. If your answer to 5.C.1. is yes, answer this: can the statement in 5.C.1 throw a
InputMismatchException?
If your answer to 5.C.2 is no, answer this: give a valid statement using iStr .
6. A. How do you make a class implement the Serializable interface?
6. B. Why would you want (need) a class to implement the Serializable interface? Your
answer should have something to do with I/O.
7. Consider this code:
import java.io.*;
public class Q7 {
public static void main(String[] args) {
try {
ObjectInputStream iStr =
new ObjectInputStream (
new FileInputStream ("numbers.dat"));
int x;
try {
while (true) {
x = iStr.readInt();
System.out.println(x);
}
}
catch(EOFException e) {
System.out.println("End of File exception");
}
iStr.close();
}
catch (FileNotFoundException e) {
System.out.println("Cannot find file");
}
catch(IOException e) {
System.out.println("Problem with input");
}
}
}
Suppose the file numbers.dat contains three numbers.
7.A. Why does the while(true) loop not cause infinite looping?
7.B. What happens when the program attempts to read a fourth number?
8. Give code that will open a file that the program will output text to. The text will be
appended to the current contents of the file. You do not have to give a complete program.