[ answers on web ]
Name: _______________________________
Student ID: _______________________________

Total points: 110
  1. Compare and contrast Java with some other language you know (e.g., C++ or Python). Describe pros/cons of each -- at least two things that you feel Java does better, and at least two things that you feel Java doesn't do as well as the other language.[5]
  2. What is the difference between an instance variable and a class variable? How do you indicate to Java if a variable should be an instance variable or a class variable? [4]
    Each instance of a class has its own separate copy of the instance variables (one per instance), but all instances share the class variables (one per class). Class variables are declared static.
  3. What does static mean for methods? Why must main() be declared static? [4]
    Static members belong to the whole class, rather than to each instance. Static methods can be invoked from the class without needing to instantiate the class. main() has to be static because when it is run no objects have been instantiated yet.
  4. The following block of code is meant to create a list of 20 Student objects and initialize their IDs. What is wrong with this code? How would you fix it? [4]
    	class Student {
    		public int ID;
    	}
    	Student[] classlist = new Student[20];
    	for (int i = 0; i < classlist.length; i++) {
    		classlist[i].ID = i;
    	}
    
    Although classlist is declared as an array of Students, its elements are never instantiated; thus, there will be an error inside the for loop when the program attempts to access the ID attribute of an element. To fix it, create an instance of Student and assign it to the array element before assigning its ID field:
    	for (int i = 0; i < classlist.length; i++) {
    		classlist[i] = new Student();
    		classlist[i].ID = i;
    	}
    
  5. Tell me everything you know about polymorphism. Why is it useful? [5]
    Polymorphism in object-oriented programming is when a single program or method can act upon multiple types of objects using the same code. If we write a program to operate on instances of a superclass, then we can run that program on instances of subclasses of the given superclass without writing a new version of the program. We can even create new subclasses that hadn't been thought of when the program was written, and it will run just the same on instances of the new subclass.
  6. Design and draw a UML class diagram for a class hierarchy at least two deep (i.e., there should be at least a "grand-parent" class) and with at least two sibling classes (i.e., classes that inherit from the same parent). Here's the important part: the class hierarchy ought to be meaningful! Include some methods and inherited attributes. Java code is not required! But: the classes, their relationships, and their methods/attributes ought to make sense and be meaningful! [8]
    Many good examples; e.g., Student inherits from Person; Faculty inherits from Employee, which inherits from Person. Inherited attributes could include name, phone, address (for Person); GPA (for Student). Virtual methods could include call() (for Person), or gradAudit() (for Student).
  7. What is an abstract class? Describe a situation where it might make sense to use one. Also describe at least two concrete subclasses. [5]
    An abstract superclass is a class that cannot be instantiated; it is used to categorize other concrete subclasses and hold common attributes+methods. A simple example might be an abstract superclass 'Person', with two concrete subclasses 'Man' and 'Woman' -- every Person is either a Man or a Woman, so it wouldn't make sense to have an instance of the superclass Person. The Person superclass could contain common attributes like Head, Heart, Arms, etc. that both Men and Women have -- but the "gender-specific" organs would be attributes of the subclasses!
  8. Now, write simple but complete Java definitions for the three classes you described in the previous question (they need not be terribly complex). [6]
  9. In Swing, what is the difference between JFrame and JPanel? [4]
    JFrame represents a window; JPanel is a container for other widgets. Both are JComponents and can contain widgets, but JPanels can be nested (it doesn't make sense to nest windows!). Every Swing application needs a JFrame, and every JPanel must be inside a JFrame, but not every JFrame needs to have a JPanel.
  10. What are layout managers for in Swing? Describe and contrast the layout managers FlowLayout, BorderLayout, and GridLayout. [5]
    Layout managers determine how widgets are positioned within a panel (including handling dynamic resizing). FlowLayout positions the widgets left-to-right in the order they are added to the panel. BorderLayout allows widgets to be positioned on the edges of the panel -- north, south, east, west, and centre. GridLayout places widgets on a grid of equal-size cells.
  11. What does ActionListener mean in Java? How is it used? [4]
    ActionListener is an interface. It has just one method, actionPerformed(). Classes that implement ActionListener must implement the actionPerformed() method, which means they know how to respond to an ActionEvent. This is used by Swing applications to respond to button presses, mouse clicks, and other user events.
  12. Compare and contrast the following java.io classes: FileInputStream, FileReader, and ObjectInputStream. [5]
    FileInputStream represents a general file opened for byte-based reading. FileReader represents a file opened for character-based reading (text). ObjectInputStream represents a file opened for reading of serialized objects; there must first be an open FileInputStream from which the ObjectInputStream reads.
  13. What is object serialization in Java? What kinds of objects can be serialized? Describe, in as much detail as you can, a situation where serialization might be useful. [5]
    Serialization converts Java objects into byte streams that can be sent across any I/O channel. Objects can be serialized if their classes implement the Serializable interface and if all its non-transient instance variables are serializable. Primitive types (int, char, etc.) and arrays of primitive types are serializable. Any kind of inter-process communication may benefit from object serialization: e.g., a client-server restaurant ordering system may wish to send objects that represent customer orders over a network I/O channel.
  14. What is buffered output? Why is it used? When might it be inconvenient? [4]
    Writes on a BufferedOutputStream do not get committed (e.g., to disk) immediately, but put in a buffer/queue for later writing (e.g., when the OS and disk are not busy). Buffering bundles together many small transactions into one large transaction, to optimize for the balance between bandwidth and latency that is inherent in disks, networks, and other media.
  15. What is a component architecture? Why are they cool? Name one component architecture in widespread use (it does not have to be from lecture, or even computer-related, as long as you explain why it's a component architecture). [5]
    A component architecture is a standard defining interoperability for program components. New applications can be assembled out of compatible components; the objective is to maximize code re-use. Examples include JavaBeans, ODBC, Rails, Firefox plugins, etc.
  16. Contrast TCP with UDP. [4]
    TCP provides connection-oriented network communication, with certain guarantees about the circuit. UDP provides connectionless communication: packets may arrive out of order, or duplicated, or may not arrive at all.
  17. How might a TCP server utilize multithreading? What limitations would a single-threaded TCP server have? [4]
    Master thread only listens for connections; when a client connects, the master thread forks off a child thread to communicate with that client, and itself goes back to listening. Without multithreading, only one client could connect at a time.
  18. What is a task scheduler? What does it do? Is it part of the Java VM or part of the host operating system? [4]
    The task scheduler assigns runnable threads to physical processors. It has the authority to preempt running threads, taking them off the processors and assigning other threads to the processors. It may give some threads higher priority to use the processors. It is part of the operating system, not the Java runtime, because there may be other non-Java processes running on the same system.
  19. Describe a situation (need not be computer-related) in which a lock might be required on a shared object. What are the two (or more) entities that access the shared object, and why is a lock needed? [5]
    Locks provide mutual exclusion. If two threads wish to use the same resource (e.g., write to the same file), they must synchronize their accesses or risk incorrect results (e.g., corrupting the file). To use, each thread first requests the lock before accessing the shared resource. If another thread has the lock, the requesting thread is blocked, waiting until the lock is freed. If no other thread has the lock, the lock is given to the requesting thread, whereupon it may freely use the shared resource. After it is done, it yields the lock so that any other waiting threads may try to get the lock.

    An example might be the lock on a washroom door: the entities competing for access might be you and your roommate; a lock is needed because only one person at a time may use the washroom.

  20. Describe the singleton creational design pattern, and how one might implement it in Java. Describe an example application that might need a singleton. [4]
    A singleton class guarantees that there is only ever one instance of that class. In Java, the constructor must be made private (so that nobody can make a new instance). The singleton object can be a class variable, and a static class method can be provided to permit access to the singleton object. An example might be a domain controller; we want to ensure that there is only one domain controller on the network.
  21. The value ex can be approximated using the partial sum 1 + x + x2/2! + x3/3! + ... + xn/n!, where n! is n-factorial, 1*2*3*4*...*(n-1)*n. Write a complete Java program that asks the user for x and n and computes the corresponding partial sum estimate of ex.
    Pseudocode, comments/doc-comments, etc. are not required, but may earn partial marks if the code is not perfect. [8]
  22. Sketch a design for an online classified-ad system, like CraigsList. Sellers can create and see their listings, and buyers can see listings and respond to the sellers. No money is exchanged through the system. There should also be administrators who can edit/delete listings and ban users. Design the components of the system -- not necessarily at the specific level of classes, but general components. Sketch use-cases: who interacts with the system, and how do they interact with it? [8]