[ Ans ]
Name: _______________________________
Student ID: _______________________________

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






  2. Discuss the pros and cons of object-oriented programming as compared with procedural programming. [4]










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




  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]
    bool
    boolean type, just true and false
    char
    a Unicode character
    byte
    an integer represented with 1 byte
    short
    an integer represented with 2 bytes
    int
    an integer represented with 4 bytes
    long
    an integer represented with 8 bytes
    float
    a real value represented with 4 bytes
    double
    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]

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





  8. What is the effect of the final keyword on
  9. 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.


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











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







  13. What is object serialization in Java? Describe, in as much detail as you can, a situation where serialization might be useful. [4]









  14. Compare and contrast the role of a Java Executor with the role of the task scheduler in multithreading. [4]
    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.








  15. In programming, what are generics? (C++ calls it "templating".) [3]
    Generic classes can take a type parameter at instantiation; thus for instance we have a generic ArrayList class which can hold objects of any type -- the element type of the ArrayList is specified at instantiation.






  16. Name and describe two design patterns. [6]
    • Creational: factory method, abstract factory, prototype, singleton
    • Structural: facade, adapter, bridge, proxy, decorator, flyweight
    • Behavioural: observer, mediator, chain of responsibility, iterator, memento, command










  17. Name and describe two examples (need not be computer-related) of component architectures. What are the components? What are the interfaces between components? [6]
    JavaBeans, ActiveX/VB, plugins (Firefox, Apache), Zope/ZCA, Rails, hardware









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







  19. The following Python function works on many types: int, float, str, etc. Outline a Java class/method to be just as flexible. [5]
    def double(x):
    	return x + x
    
    Something along the lines of:
    public class Doubler {
      	public Doubler() { }
    	public T double(T x) {
    		return(x + x);
    	}
    }
    





  20. 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];
    









  21. The last question is a small programming project: 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, etc. are not necessary, but may garner points if your code isn't perfect. Please use as much paper as you like; ask me if you need more.

    Choose from one of the following options (circle which option you choose; no extra credit if you work on more than one) [15]