final review flashcards.txt

Card Set Information

final review flashcards.txt
2015-08-06 23:36:48
java cs150
final review cs150
Show Answers:

  1. Describe the differences between instance variables and class variables.
    • Class (static) variables are shared by all members of the class. They cannot access instance members of the class.
    • Instance variables are variables dependent on a specific instance of the class.
  2. How do you declare an instance variable?
    • variableType variableName;
    • i.e., String firstName;
  3. How do you declare a static variable?
    • static variableType VARIABLE_NAME;
    • i.e., static double PI_TO_TWO = 3.14;
  4. What do the public and private modifiers do?
    • The public visibility modifier (for classes, methods and data fields) denotes that they can be accessed from any other classes.
    • The private visibility modifier makes methods and data fields accessible *only* from within its own class.
  5. What is the purpose of constructor overloading?
    To give a client more initializing options.
  6. What is the difference between a convenience constructor and a designated constructor?
    The designated constructor initializes the object most completely. The convenience constructors should call the designated constructor. A no-arg constructor is an example of a convenience constructor.
  7. What are accessor methods?
    Getters (to read state) and setters (to update state) are used to access instance variables (ivars) and class variables (cvars). You should use them whenever you need access to an ivar/cvar. Doing so reduces maintenance, avoids redundancy and offers flexibility.
  8. What does UML stand for?
    Unified Modeling Language
  9. UML: -
  10. UML: +
  11. UML: underline
    static (class)
  12. UML: italic
  13. UML: <>
  14. UML: filled diamond
    composition (object is fully owned)
  15. UML: open diamond
    aggregation (object is shared)
  16. UML: open triangle
    generalization (inheritance)
  17. Know how to draw a UML class diagram.
    Practice this!!!
  18. What are two benefits of inheritance?
    • Reusability - reuse code from parent class
    • Extensibility - Use subclasses to extend the functionality superclasses (i.e., student -> graduate or undergraduate. Both share common traits, but have unique traits that student doesn't have and other subclasses don't require).
  19. How do you declare a subclass?
    • public class superClass {}
    • public class subClass extends superClass{}
  20. Know how to draw a UML class diagram showing class relationships (super, sub)
    Practice this!!!
  21. "Is a" applies to what type of class relationship?
  22. "Has a" applies to what type of class relationship?
  23. What is the benefit of polymorphism?
    Polymorphism allows for the proper method (super- or sub-class) to be called at runtime (because of dynamic -runtime- binding).
  24. How can you write a method so that it can be overridden?
    • The method must exist in both the subclass and the superclass with the same name and the same argument lists (number and type).
    • The return type of both methods must match.
    • Don't declare the superclass private or final (or static if the classes are in the same file).
  25. Discuss declared type and actual type and how they relate to polymorphism.
    An object can be declared as the type of the superclass, but be of a subclass type. For example, if you create an array of Students, they can be Graduate or Undergraduate type objects. You can return the actual type of an object at runtime using instanceof. (if ( person1 instanceof undergraduate ))
  26. What are declared type and actual type (with regard to polymorphism)?
    • Declared type - determines if a method can be called
    • Actual type - Provides actual implementation
  27. What does polymorphism offer?
    Polymorphism offers the desired flexibility that is needed in OO environments. It also allows code to be easily modified to allow for future needs.
  28. What is the syntax for a subclass method to call a superclass method?
    • Constructor: super() - for no-arg constructor
    • super(args)
  29. Method: super.method(args if needed)
  30. Describe the try-catch method of exception handling.
    When you are handling a checked exception, you must declare a checked exception if the method might throw it (e.g., throws FileNotFoundException). Then, you enclose the code that could throw the exception in a try block and handle the exception in the catch block.
  31. Errors should be caught _____________.
  32. Errors should be handled in the ________________.
  33. Why benefit comes from detecting errors locally but handling them in the caller?
    The error is caught to prevent errors in execution, but the exception is re-thrown to the caller to allow any clean-up that needs to take place as a result of the error. Otherwise the called method would terminate the program.
  34. What happens when an exception is caught?
    If one of the statements inside a try statement is caught, Java skips the remaining statements in the try block and trys to handle the exception. Each catch statement (on the stack of calling methods) is examined in turn to see if the caught exception is handled in that catch block. If so, it is executed. If nhot, it proceeds up the stack.
  35. Name one advantage that storing data in binary form has over storing the same data in ASCII form.
    ASCII requires 1 byte for each character, while binary can store data using fewer bits.
  36. Binary is/is not readable.
    is not
  37. Text is/is not readable.
  38. Storing data in binary form is more ________________ because it does not require encoding/decoding.
  39. Name the 4 stream classes and what they are used for.
    • FileXXXStream - bytes
    • DataXXXStream - primitive/String
    • BufferedXXXStream - buffered bytes
    • ObjectXXXStream - objects, primitives
  40. Declaring a stream for specified IO will likely require _______________.
    nesting of streams
  41. Why is the Serializable interface used?
    It allows class hierarchies to be read to/written from disk.
  42. What is the syntax for declaring a generic class?
    public class Stack {}
  43. What is the syntax for declaring a generic method?
    public void display(T[] list)
  44. What are the advantages of generics?
    • *compile time error checking
    • *flexibility
  45. Are generics available at runtime?
    No. They are replaced by Java at runtime with concrete classes.
  46. When you use generics, can you use any data type at runtime?
    No. Primitive types must be wrapped (or boxed). For example, int becomes Integer.
  47. What does boxing do?
    Converts a primitive type to an object (e.g., int to Integer).
  48. What does unboxing do?
    Converts an object to a primitive type (e.g., Integer to int).
  49. What does erasure do?
    • *provides backward compatibility
    • *Raw syntax is rewritten to use generics
    • *generic bounding wildcards (?, ? extends)
  50. Name the two types of containers.
    Collections and maps
  51. Name and describe the three types of collections.
    • Set: unordered no duplicates
    • List ordered dups allowed
    • Queue ordered (FIFO) dups allowed
  52. Describe the characteristics of a map.
    A map is composed of key/value pairs. There can be no duplicates (due to the hashing algorithm) and search is very fast.
  53. ~~~~~
  54. What are some important characteristics of the ArrayList data construct?
    • *array implementation
    • *dynamically increases
    • *maintains order of insertion
    • *requires shifting during insertions/deletions
  55. What are some important characteristics of the LinkedList data construct?
    • *linked implementation
    • *dynamically increases/decreases
    • *maintains order of insertion
    • *does not require shifting durint insertions/deletions
  56. When should you use the Comparator interface?
    When classes don't impelement the Comparable interface, and when the classes are not the same but share common characteristics.
  57. What does the Comparator interface offer?
    • *can compare in a non-natural order
    • *can compare dissimilar objects
    • *offers compare method
  58. What are some important characteristics of the Stack data construct?
    • *LIFO
    • *reverses order of insertion (when popped)
    • *push and pop ONLY at the TOP of the stack
  59. What are some important characteristics of the Queue data construct?
    • *FIFO
    • *maintains order of insertion
    • *insert at the end of the queue (push)
    • *remove at the front of the queue (pop)
  60. What are some important characteristics of the Priority Queue data construct?
    • *insert at the end of the queue
    • *remove at the front of the queue
    • *order of insertion not maintained (due to prioritization)
    • *value with highest priority moves to the front of the queue
  61. What are some important characteristics of the Linked List data construct?
    • *each node is linked to the next one
    • *order of insertion is maintained
    • *uni-directional traversal (head to tail)
  62. What are some important characteristics of the Deque data construct?
    • *queue, but with
    • *bi-directional traversal (head to tail and tail to head)
  63. Name 3 characteristics of Sets:
    • *no duplicates
    • *insertion order not maintaines
    • *can be searched fast
  64. Give a distinguishing characteristic of a HashSet.
    It uses a hashcode for fast searching.
  65. Give a distinguishing characteristic of a LinkedSet.
    It maintains order of insertion.
  66. Give a distinguishing characteristic of a TreeSet.
    It provides sorting order (increasing or decreasing)
  67. Name 5 characteristics of Maps:
    • *key-value pairs
    • *key is usually a string
    • *value can be any object
    • *no duplicates
    • *fast searches
  68. Give a distinguishing characteristic of a HashMap.
    It uses a hash code for fast searching
  69. Give a distinguishing characteristic of a LinkedMap.
    It maintains insertion order.
  70. Give a distinguishing characteristic of a TreeMap.
    It provides sorting order (increasing or decreasing).
  71. Bubble Sort, Insertion Sort and Selection Sort are good only for ______________ data sets.
  72. Bubble Sort, Insertion Sort and Selection Sort require ______________ passes.
    n - 1, where n is the number of items in the set to be sorted
  73. Bubble Sort is different from Insertion Sort and Selection Sort in that it offers _________________.
    quick exit. It knows when it is finished; the others have to sort to the end of the set to ensure completion.
  74. Mergesort and Quicksort use ____________________ implementations.
  75. Mergesort and Quicksort can be used for _______________ data sets.
  76. Mergesort can be used with ___________________ as well.
    external data
  77. The Heapsort method is based on a _______________________ (also known as a heap).
    binary tree
  78. Describe the Heapsort algorithm.
    • *Build heap, remove root successively
    • *add to the bottom, rebuild heap
    • *remove root, rebuild heap
  79. Describe the Radix Sort.
    • *sorts n values, each m digits in length
    • *starts with least significant digit (lsd) and progresses to most significant digit (msd)
    • *uses value-based bins to distribute digits
  80. Example: subclass constructor calls super constructor
    super(args for super constructor);
  81. Example: switch
    • switch(criterium) {
    • case ?:
    • statement;
    • break;
    • }
  82. The term override implies _____________.
  83. Example: lambda expression
    • setOnSomeEvent(e -> {
    • if ( condition is true) {statement;}
    • else {statement;}
    • });
  84. Example: declare an array list
    ArrayList listName = new ArrayList<>();
  85. Example: Use ObjectOutputStream to output to a file
    • ObjectOutputStream oout = new ObjectOutputStream(
    • new BufferedOutputStream(
    • new FileOutputStream("")));
  86. When you want a class to be output to a file (using a stream), you should implement the _______________ interface in which class? (the class that outputs the data to the file or the class that the output is composed of)?
    Serializable, the class the output is composed of
  87. Example: if statement
    • if(condition) {do this;}
    • else {do this instead;}
  88. Example: create a generic array to hold objects
    E[] arrayName = new Object[arraySize];