Please remember to read the exam directions carefully. There may be a guessing penalty on some questions.
If you earn a higher percentage on the final than you did on the midterm, then your midterm will not be counted and your final will be 45% of your course grade. The following summary of the exam coverage may be helpful as you prepare. If you have any questions, or would like to discuss solutions to the practice problems, do not hesitate to stop by during my office hours. During the week of the final, I will have extended office hours as follows:
import java.io.*;
public class ParseFarce {
public static void main(String args[]) {
try {
double result1 = 0;
double result2 = 0;
StreamTokenizer st = new StreamTokenizer(new FileInputStream("input.txt");
while (st.nextToken != st.TT_EOF) {
if ((st.ttype = st.TT_WORD) && (st.sval.compareTo("foo") == 0)) {
System.out.println(""+result1);
result1 = 0;
} else if ((st.ttype = st.TT_NUMBER) {
result1 = result1 + st.nval;
result2 = result2 + st.nval;
}
}
System.out.println("finished: " + result2);
} catch (IOException ioe) {
System.out.println("Error parsing file input.txt" + e);
}
}
}
Question:
What woult be the output from executing the program ParceFarce if the
file "input.txt" contained the following:
CS 101 and CS 102 are courses in which the word foo appears at least 10 times each semester. Every year, at least 6 students ask the origin of foo, but the answer is widely debated. There are only 1 or 2 people in the world who know true origin of foo, and I am fortunately not 1 of them. Of course, the preceding is utter nonsence and should not be taken seriously by any self-respecting person.Solution: The output would be
203 16 3 finished: 223
Question:The following server is supposed to handle multiple simultaneously. Each client connects to the server, provides his/her name, and then provides all the data objects, which are processed by the server using a separate thread for each client, so multiple client's objects can be processed concurrently However, once deployed, it is noticed that sometimes clients must wait long periods of time in order to be served. (Those processing code is not provided here, since it is irrelevant to the problem.) What is the wrong with the server?
import java.io.*;
import java.net.*;
public class ObjectRepository {
public static void main(String args[]) {
try {
ServerSocket ss = new ServerSocket("localhost",11111);
} catch (IOException ioe) {
System.out.println("Failed to create ServerSocket. " + ioe);
}
if (ss != null) {
while (true) {
try {
Socket client = ss.accept();
ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream);
ObjectInputStream ois = new ObjectInputStream(client.getInputStream);
String cleintName = (String) ois.readObject();
(new Thread(new ClientObjectProcessor(clientName, ois, oos))).start();
} catch (IOException ioe) {
System.out.println("A client connection failed. + ioe);
}
}
}
}
}
Answer: Although a separate thread is started for each client,
that thread is started only after the client's name has been received by
the server. In other words, at each iteration of the loop, the server blocks
at the line that calls the readObject method until an object comes in.
Therefore, if a socket is accepted from a client who waits a long time
before providing his/her name, then no new client sockets can be
accepted during that time, because no more iterations of the loop can take place
until the object comes in. To fix the problem, let the ClientObjectProcessor
read the name from the input stream instead of passing it to the ClientObjectProcessor
in the constructor.
The remainder of this section will be a short-answer portion in which you will be asked to either discuss or explain the implications of some important difference between the two languages. For example, if you were asked to discuss the implications of choosing whether or not to provide garbage collection, you might say: "Garbage collection makes programs easier to write because the implementer does not need to be concerned with keeping track of whether or not an object is still referenced by any data structure so that the program can explicitly release the memory used by the object. However, garbage collectors can slow down a program's execution because it takes time for the garbage collection algorithm to find all the objects on the heap that are no longer reachable." (The actual exam question may involve reading a small amount of code.)