[ answers online ]
Name: _______________________________
Student ID: _______________________________

Total points: 110
  1. What do you think of the Java programming language overall? Compare it to another language you know. [4]

  2. What is object-oriented programming? How does it differ from procedural programming? [4]
    Procedural programming is action-oriented: the code is a sequence of procedures, and the data structures are passed as parameters to the procedures. The focus is on the 'verbs'. In contrast, object-oriented programming is data-oriented: code is organized into classes, centred around objects, and the procedures are treated as methods within each class. The focus is on the 'nouns'.

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

  4. What is the effect of the static keyword on attributes and methods? Why must main() be declared static? Describe another example of an appropriate use of 'static' on an attribute or method. [6]
    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. The String library has a number of static utility methods, e.g., String.format(). Another example is the constant Math.PI.

  5. What is the effect of the final keyword on
  6. Tell me everything you know about polymorphism. Why is it cool? [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.

  7. Contrast TCP with UDP. Describe an application in which TCP would be more appropriate, and one in which UDP would be more appropriate. [6]
    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.

  8. What is an interface in Java? What is an abstract class? What's the difference? Describe an example of each (you may come up with your own). [6]
    An abstract class is not meant to be instantiated directly. An abstract superclass can be used as a way of organizing its subclasses under one category. For example, Dog, Cat, and Cow all are within the Mammal category. It makes sense to have specific instances of Dog, Cat, and Cow, but not of Mammal. Mammal is the abstract superclass.
    An interface is a named set of methods, for instance the ActionListener interface specifies the actionPerformed() method and advertises that a class knows how to respond to action events. Interfaces specify functionality, i.e., what a class knows how to do. Abstract superclasses specify identity, i.e., what a class is. Multiple classes that inherit from different superclasses may implement the same interface, but in Java multiple inheritance is not permitted.

  9. What is object serialization in Java? Describe, in as much detail as you can, a situation where serialization might be useful. [4]
    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.

  10. Write a complete Java program that reads in a file "input.txt", removes all digits, and outputs the result in the file "output.txt". Include full doc-comments with pre-/post-conditions. [8]
    (See StripDigits.java.)
  11. A BankTransaction consists of an account number, date, dollar amount, and a flag that indicates whether it was a withdrawal or deposit. Write a complete Java class defining a BankTransaction, including attributes, constructors (include a default constructor with no parameters and a copy constructor), and get/set methods. [10]
    (See BankTransaction.java.)
  12. Design a class hierarchy of your own choosing: include an abstract superclass and at least two concrete subclasses. Design attributes and methods appropriate for each class. Design a method that takes advantage of the polymorphism in this class hierarchy, and explain how it does so. The polymorphic method does not have to live inside any of the classes. [8]
  13. What is Android? Describe how applications are made in Android. [5]
    Android is a Linux-based operating system made by Google and the Open Handset Alliance, designed for mobile phones, tablets, and low-end computers. It is also a component system for building mobile applications. Android applications may be composed of multiple Activities, background Services, BroadcastReceivers, and ContentProvider databases. Each component is implemented by making a subclass of the appropriate type (Activity, Service, etc.) and overriding specific methods (hooks), e.g., onCreate(), onResume(), etc.

  14. Name and describe three Creational design patterns. Give an example or analogy for each. [6]
    factory method, abstract factory, prototype, singleton

  15. Name and describe three Structural design patterns. Give an example or analogy for each. [6]
    facade, adapter, bridge, proxy, decorator, flyweight

  16. Name and describe three Behavioural design patterns. Give example or analogy for each. [6]
    observer, mediator, chain of responsibility, iterator, memento, command

  17. We wish to design software to run a programmable thermostat (controls heating and cooling). Draw a UML use-case diagram for the thermostat. [8]
  18. You want to learn from your friend what he had for breakfast. The problem is that you are standing on opposite cliffs of a large canyon, so you have to yell, and you and your friend might hear incorrectly or not hear what was said. You also don't know how many items he had for breakfast. Draw a UML sequence diagram for a reliable way to learn what your friend had for breakfast via this unreliable communication channel. (This is roughly what yodeling was originally used for.) [8]