[ Answers ]
Name: _______________________________
Student ID: _______________________________

Total points: 60
  1. Compare and contrast object-oriented programming with 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'.



  2. Explain what the JDK and JRE are and contrast them. [3]

    Java Runtime Environment
    what you need to run (other people's) Java programs: the Java virtual machine (command line: java).
    Java Development Environment
    what you need to compile (make your own) Java programs: javac compiler.


  3. Describe the MVC design pattern [4]

    Model:
    Data structures and methods to interact with the data. e.g., 2D array of letters for word-search
    View:
    Display/output, summary of the data, statistics, etc. e.g., Swing labels, text output
    Controller:
    Input/control from the user or other programs. e.g., Swing buttons, keyboard input


  4. Explain and contrast method overloading with method overriding. [4]

    overloading
    multiple versions of the same method (same name) but with different number/types of parameters.
    overriding
    a method defined in a subclass overrides a method of the same name in the superclass.


  5. What is an abstract class? [3]

    An abstract class is not meant to be instantiated directly, but rather subclassed.

  6. What is a Java class interface? [3]

    An interface is a named set of methods.

  7. Describe and contrast the eight Java primitive types. [8]

    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


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

    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


  9. Tell me everything you know about serialization in Java. [5]

    Serialization is converting an abstract data structure (object) into a series of bytes suitable for writing into a file or sending over the network. (In Python this is "pickling".) In Java an object is serializable if its class implements the (empty) interface Serializable and if every non-transient attribute is serializable. The primitive types (int, char, etc.) and arrays of them are serializable. Non-serializable attributes can be skipped over by declaring them transient. A serializable object can be written out to an ObjectOutputStream by calling its writeObject() method. Similarly, objects (of unknown type) may be read from an ObjectInputStream by calling readObject().



  10. Define simple Java classes and sketch a UML class diagram that formalizes the following set of statements. Declare and diagram every (non-primitive) class used.[8]

    class Appointment {
    	Time startTime;
    	Person[] attendees;
    }
    class Time {
    	byte hour, minute;
    }
    class Person { }
    
    (You may also have made Hour and Minute classes; that's okay, too.)


  11. Write a complete Java class to sort an array of integers. Docstrings and comments are not necessary but may garner partial credit. [10]

    
    public class Sorter {
    
      public static void bubbleSort(int array[]) {
        for (int i=array.length-1; i >= 0; i--) {
          for (int j = 0; j<i; j++) {
    	if (array[j] > array[j+1]) {	// swap!
    	  int temp = array[j];
    	  array[j] = array[j+1];
    	  array[j+1] = temp;
    	}
          }
        }
      }
    
      public static void main( String[] args ) {
        int ARRAY_LEN = 100;			// can change this
        int[] myArray = new int[ARRAY_LEN];
    
        for (int i=0; i<myArray.length; i++) 	// fill with random
          myArray[i] = (int) (100 + 900*Math.random());
    
        System.out.println("Random array: of " + ARRAY_LEN + " 3-digit numbers:");
        for (int i=0; i<myArray.length; i++)
          System.out.print(" " + myArray[i]);
        System.out.println("");
    
        bubbleSort(myArray);		// sorts in-place
    
        System.out.println("Sorted:");
        for (int i=0; i<myArray.length; i++)
          System.out.print(" " + myArray[i]);
        System.out.println("");
    
      }
    }