Study your flashcards anywhere!

Download the official Cram app for free >

  • Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off

How to study your flashcards.

Right/Left arrow keys: Navigate between flashcards.right arrow keyleft arrow key

Up/Down arrow keys: Flip the card between the front and back.down keyup key

H key: Show hint (3rd side).h key

A key: Read text to speech.a key


Play button


Play button




Click to flip

60 Cards in this Set

  • Front
  • Back
Every exception, every event, every array extends from ____.
When should you override the "toString()" method?
When you want a mere mortal to be able to read something meaningful about the objects of your class.
When you pass an object reference to the "System.out.println()" method, what method is called?
the object's "toString()" method and the return of "toString()" is what you see displayed.
When you don't override the "toString()" method of class Object, what is printed out?
the class name followed by the @ symbol followed by the unsigned hexadecimal representation of the object's hashcode.
Why do people affectionately refer to "toString()" as "the spill-your-guts method"?
Because the most common implementations of "toString()" simply spit out the object's state (in other words, the current values of the important variables).
How does the "equals()" method in Object determine if two objects are equal?
It uses the "==" operation so two objects are considered equal only if the two references refer to the same object.
If you don't override the "equals()" method, what won't you be able to do?
You won't be able to use the object as a key in a hashtable.
What 2 things do we have to do in order to make a valid equality comparison (i.e. override "equals()")?
1. Be sure that the object tested is of the correct type. It comes polymorphically as type Object, so you need to do an "instanceOf" test on it.
2. Compare the attributes we care about. For performance you're going to want to check the fewest number of attributes.
What is the access modifier for the "equals()", "hashCode()", and "toString()" methods?
What is a Java contract?
A set of rules that must be followed if you want to provide a "correct" implementation as others will expect it to be. If you don't follow the contract, you may still compile and run, but your code may break at runtime in some unexpected way.
Give the 5 rules of the "equals()" Java contract.
For any reference values x,y, and z:
1. It is reflexive. x.equals(x) should return true.
2. It is symmetric. x.equals(y) should return true if and only if y.equals(x) returns true.
3. It is transitive. if x.equals(y) and y.equals(z) return true, the x.equals(z) should return true.
4. It is consistent. Multiple invocations of x.equals(y) returns the same value.
5. For any nonnull reference x, x.equals(null) should return false.
What is the joint contract that binds together "equals()" and "hashCode()"?
If two objects are considered equal using the "equals()" method, then they must have identical hashcode values.
If you override "equals()", what other method must you override?
the "hashCode()" method
What is the two-step process in good hashing retrieval?
1. Find the right bucket.
2. Search the bucket for the right element.
What does the hashcode method in the class Object do?
Always comes up with a unique number for each object, even if the equals method is overridden in such a way that two or more objects are considered equal.
Does a "hashCode()" that returns the same value for all instances whether they're equal or not violate the contract?
No - but it would be horribly inefficient because it makes all objects land in the same bucket.
What must happen in order for an object to be located in a collection?
The search object and the object in the collection must have "both" identical hashcode values and return "true" for the "equals()" method.
What are the 3 parts of the "hashCode()" Java contract?
1. Whenever it is invoked on the same object more than once during an execution of a Java application, the "hashCode()" method must consistently return the same integer, provide no information used in "equals()" comparisons on the object is modified. This integer need not remain consistent from one execution to another of the same application.
2. If two objects are equal according to the "equals(Object)" method, then calling the "hashCode()" method on each of the two objects must produce the same integer result.
3. It is "not" required that if two objects are unequal according to the "equals(java.lang.Object)" method , then calling the "hashCode()" method on each of the two objects must produce distinct integer results.
What does serialization do?
Saves an object so that it can be reanimated later by deserializing it back to full objectness.
Note: "transient" objects are not saved when an object is serialized.
Should you use transient variables in determining an object's hashcode or equality?
List 5 basic operations normally used with collections.
1. Add objects to the collection.
2. Remove objects from the collection.
3. Find out if an object (or group of objects) is in the collection.
4. Retrieve an object from the collection (without removing it).
5. Iterate through the collection, looking at each element (object) one after another.
List the 6 core interfaces from the Collections Framework that you need to know for the exam.
Collection, List
Set, Map
Sorted Set, Sorted Map
List 4 concrete classes that implement the Map interface.
HashMap, Hashtable, TreeMap, LinkedHashMap
List 3 concrete classes that implement the Set interface.
HashSet, LinkedHashSet, TreeSet
List 3 concrete classes that implement the List interface.
ArrayList, Vector, LinkedList
Give the 3 overloaded uses of the word "collection".
1. "collection" which represents any of the data structures in which objects are stored and iterated over.
2. "Collection" which is actually the java.util.Collection interface from which Set and List extend.
3. "Collections" which is actually the java.util.Collections class that holds a pile of static utility methods for use with collections.
What are the 3 basic flavors that Collections come in?
1. Lists - List of things (classes that implement List)
2. Sets - Unique things (classes that implement Set)
3. Maps - Things with a unique ID (classes that implement Map)
What are the 4 subtypes of List, Set, and Map?
Sorted, Unsorted, Ordered, Unordered
What does it mean to say that a collection is ordered?
It means you can iterate through the collection in a specific (not-random) order.
Is a Hashtable collection ordered?
How is an ArrayList ordered?
It keeps the order established by the elements' index position.
How is a LinkedHashSet ordered?
It keeps the order established by insertion, so the last element inserted is the last element in the LinkedHashSet.
When a collection keeps an order referred to as the "natural order" of the elements, what do you call it?
What does it mean to say a collection is sorted?
It means the collection is sorted by natural order and natural order is defined by the class of the objects being sorted.
What does the List interface care about?
the index - it has a set of methods related to the index. All three List implementations are ordered by index positions.
Describe key points about the ArrayList class.
This is a growable array. It give you fast iteration and fast random access. In 1.4 it implements the new RandomAccess interface - a marker interface which says it supports fast random access.
Describe the key points about the Vector class.
Is basically the same as the ArrayList class but the Vector() methods are synchronized for thread safety. Synchronized methods add a performance hit. It also implements RandomAccess
What does the Set interface care about?
Uniqueness - it doesn't allow duplicates.
Describe the key points of the HashSet class.
It is an unsorted, unordered Set. It uses the hashcode of the object being inserted. You should use this when you want a collection with no duplicates and you don't care about order when you iterate through it.
Describe the key points of the LinkedHashSet class.
It is an ordered version of HashSet that maintains a doubly-linked List across all elements. Use this when you care about the iteration order. It lets you iterate through the elements in the order in which they were inserted.
Describe the key points of the TreeSet class.
It is a sorted collection and uses Red-Black tree structure and guarantees that the elements will be in ascending order, according to the natural order of the elements.
What does the Map interface care about?
Unique identifers. You map a unique key (the ID) to a specific value, where both the key and the value are of course objects. Where the keys land in the Map is based on the key's hashcode.
Describe the HashMap class.
Gives you an unsorted, unordered Map. It allows one null key in a collection and multiple null values in a collection.
Describe the Hashtable class.
Hashtable is the synchronized counterpart of HashMap which means that the key methods of the class are synchronized. It doesn't let you have anything that's null.
Describe the LinkedHashMap class.
It maintains insertion order. It has faster iteration than HashMap but slower adding and removing elements than HashMap.
Describe the TreeMap class.
It is a sorted Map (i.e. sorted by the natural order of the elements). You can pass your own comparison rules in when you construct a TreeMap.
What is the downside to automatic garbage collection?
You can't completely control when it runs and when it doesn't.
What is the heap?
That part of memory where Java objects live, and it's the one and only part of memory that is in any way involved in the garbage collection process. There is one and only one heap.
When the garbage collector runs, what is its purpose?
To find and delete objects that cannot be reached.
Who decides when the garbage collector runs?
the JVM. From within your Java program you can ask the JVM to run the garbage collector, but there are no guarantees, under any circumstances, that the JVM will comply.
When does the JVM run the garbage collector?
When it senses that memory is running low.
When is an object eligible for garbage collection?
when no live thread can access it
Can a Java application run out of memory?
Yes, if you maintain too many live objects.
List three ways that an object can be made eligible for garbage collection.
1. Set the reference variable that refers to the object to "null".
2. Set its reference variable to refer to another object.
3. "islands of isolation" - a class has an instance variable that is a reference variable to another instance of the same class. Imagine two such instances exist and refer to each other. If all other references to these two objects are removed, then even though each object still has a valid reference, there will be no way for any live thread to access either object.
Note: local variables created in a method that are not returned are eligible for garbage collection.
What class are the garbage collection routines that Java provides members of?
the Runtime class, a special class that has a single object (a Singleton) for each main program.
What method can you use to get the Runtime instance?
the "Runtime.getRuntime()" method
What is the simplest way to request garbage collection?
What is about the only thing you can guarantee about the running of the garbage collector?
If you are running very low on memory, the garbage collector will run before it throws an OutOfMemoryException.
What is the mechanism that Java provides to run some code just before your object is deleted by the garbage collector?
finalize() inherited from class Object. It is recommended that you don't override it because you cannot count on the garbage collector to ever delete an object so any code you put into it is not guaranteed to run.
Give two tricky little "finalize()" gotcha's.
1. For any given object, "finalize()" will be called once by the garbage collector.
2. Calling "finalize()" can actually result in saving a object from deletion.