CS102 Final Review

The CS102 final exam will be held on Thursday, April 29 from 10:30 to 12:30 in Brown 100. The exam is covers material through Tuesday, April 20 and counts as 25% of the course grade. The final exam is comprehensive. The first part of the exam will have questions similar to the midterm, so you should redo the problems from the midterm for practice. (If you never picked up your midterm exam, stop by my office hours to get it.) Sample problems are provided here for the remaining sections.

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:

Monday, April 26: 10am-11am, 3:30pm-5pm
Tuesday, April 27: 10am-12pm, 12:30pm-3pm
Wednesday, April 28: 10am-11am, 1:30pm-3pm

Definitions (15 points)
You will given terms and asked to supply definitions, or will be given definitions and asked to supply the terms. Be sure you know the definitions of the following terms. The CS102 Glossary contains definitions for these.

Packages and Protection (15 points)
This section of the exam will cover issues related to protection and application programmer interfaces. You will be given an English description of a package, followed by some code for portions of the package in which the protection modifiers have been omitted. Your job will be to decide the appropriate protection modifiers: public, protected, default, or private for selected variables, classes, and methods. You may also be asked to justify your decisions.

The Java Event Model and Inner Classes (10 points)
You will be given user interface code and asked questions about how events are handled during its execution. The code will include some inner classes. You may be asked to add some code of your own.

Exception Handling (10 points)
You will be given some code that is prepared to handle certain kinds of exceptions, and you will be asked some questions about the code and its execution on various input values. You may be asked to add some code of your own.

Concurrent Threads and Deadlock (15 points)
You will be given some code in which a deadlock situation can occur. Your job is to prove that deadlock can occur by describing a possible interleaving of two threads that results in deadlock, and to provide the corresponding resource allocation graph. You may also be asked to show how to fix the code so deadlock can't occur, and to explain why your solution will prevent deadlock from occurring.

File I/O and Parsing (10 points)
You will be given a program and a sample input file, and you will be asked what output will be produced by running the program on that file. Be sure you are familiar with the concept of an input stream, and be familiar with the general purpose of Java's StreamTokenizer class. Here is an example problem.

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

Interprocess Communication (10 points)
You will be provided with some code for a client and/or server that has some sort of problem. You will be asked to find the problem and suggest a remedy. Here is an example question.

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.

Programming Language Comparison (15 points)
We spent two lectures comparing Java and C++. Although you are not responsible for the syntactic details of C++, you should be familiar with the conceptual differences between the two languages. Part of this section will be multiple choice. You will be given a list of language features. By each, you are to write: For example, if you were given the feature "a garbage-collected heap," you would write Java since Java has a garbage-collected heap but C++ does not.

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.)