[ Ans ]
Name: _______________________________
Student ID: _______________________________

Total points: 100
  1. What do you think of the Java programming language overall? [2]

  2. Describe the role of the Java compiler javac: what does it take as input, and what does it output? [3]
    The compiler translates Java source code (.java files) to machine-independent Java bytecode (.class files). A further runtime translation step by the Java virtual machine is still needed to produce machine code.

  3. What are File objects used for in Java? [3]
    Reference to a file on disk: pathname, whether it exists or not, whether it's readable or not, whether it's a regular file vs. directory vs. named pipe, etc.

  4. Write a complete Java program that outputs the string "Hello, World!" to the command-line console. Include full doc-comments as we did in the labs. [5]
    (See HelloWorld.java)

  5. Describe and contrast the eight Java primitive types. [5]
    boolean type, just true and false
    a Unicode character
    an integer represented with 1 byte
    an integer represented with 2 bytes
    an integer represented with 4 bytes
    an integer represented with 8 bytes
    a real value represented with 4 bytes
    a real value represented with 8 bytes

  6. What is the effect of the static keyword on variables and methods? Why must main() be declared static? [5]
    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.

  7. Describe and contrast the four options we have in Java for access modifiers on attributes/methods. [4]

    only methods in this class can access
    (no modifier)
    methods in this class and this package can access
    also subclasses can access
    anyone can access

  8. Tell me everything you know about polymorphism. [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.

  9. What is downcasting? Write a simple example in Java. [5]
    A name that is declared to be an instance of a superclass, but that actually references an instance of a subclass, can be downcast to that subclass type. An example is when we readObject() from an ObjectInputStream, the object returned only has type Object. If we know the real type of the object we read in, we can downcast it to the appropriate type in order to extract its members.
    class Fruit { }
    class Apple extends Fruit { }
    Fruit myFruit = new Apple();
    Apple myAppleRef = (Apple) myFruit;	// downcasting is okay

  10. What is the effect of the final keyword on
  11. What is an abstract method? Write complete Java code defining an abstract class with an abstract method, as well as a concrete subclass. [5]
    Abstract methods do not have bodies; they can only be declared within abstract classes. Concrete subclasses provide implementations of the abstract methods.
    abstract class Fruit {
    	public abstract boolean isRed();
    public class Apple extends Fruit {
    	public boolean isRed() { return true; }

  12. Compare and contrast the role of a Java Executor with the role of the task scheduler in multithreading. [5]
    The Executor for a pool of threads manages which runnable tasks get queued up for execution. A Java program may have multiple Executors for multiple pools of threads. Executors are software constructs within the Java VM. The task scheduler is part of the operating system (there is only one), and manages which tasks get the actual processors at any point in time -- this may include other tasks on the machine, e.g., reading email, browsing the web, other users on the same computer, etc.

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

  14. What is a lock in multithreading? Why are they important? How are they used? [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.

  15. Define simple Java declarations and sketch a UML class diagram that formalizes the following set of statements. There may be multiple correct interpretations. [8]

    public interface Edible {
    	public boolean isSweet();
    public class Fruit {
    	String colour;
    public class Apple extends Fruit implements Edible {
    	public boolean isSweet() { return false; }
    public class Fuji extends Apple {}
    public class Gala extends Apple {}
    public class Pear extends Fruit implements Edible {
    	public boolean isSweet() { return true; }
    Apple[] myApples = new Apple[5];

    The last two questions are small programming projects: you will need to write complete Java code (one or more classes) to solve the given problem. Include a rudimentary test-bed (a separate class or just a main() method). The API, error-checking, limitations, etc. are all up to you, but if there are limitations or preconditions on the input, you need to state that somewhere in your answer. A full write-up, pseudocode, comments/doc-comments, are not necessary, but may garner points if your code isn't perfect. Please use as much paper as you like; there's more paper at the front of the classroom.

  16. In Java, implement a sorting algorithm of your choice that works on arrays of characters (or Strings, if you like). (No, you may not use one of Java's built-in sorting methods!) [15]

  17. Write a TCP server and client, and make the client say something to the server. You may use any format you wish (strings, binary data, serialized objects, etc.) for the data to be sent. [15]