| Terms |
Definitions |
|
protected equal
|
inheritance
|
|
Which code is important?
|
MapTest.java
|
|
can't do the below combinationsabstract staticabstract finalabstract private
|
|
|
ObjectOutputStream and ObjectInputStream have to be wrapped around which lower-layer classes?
|
FileOutputStream and FileInputStream
|
|
arrays have length or length()
|
length
|
|
Polymorphism is only for instance methods not for instance variables
|
|
|
Which exam watch is important?
|
page 444
|
|
Which table is important?
|
5-4 on 391
|
|
to override, you must keep the argument listyou can use a subtype of the return value
|
|
|
All variables defined in an interface must be public, static, and final
|
|
|
Look for questions with a method declaration that ends with a semicolon, ratherthan curly braces. If the method is in a class—as opposed to an interface—then boththe method and the class must be marked abstract.
|
|
|
The equals() method in class Object uses only the == operator for comparisons,so unless you override equals(), two objects are considered equal only if the tworeferences refer to the same object.
|
|
|
But there is no equivalent exception for generics, because of type erasure. in other words, at runtime JVM knows the type of arrays, but does not know the type of a collection. All the generic type information is removed during compilation
|
NA
|
|
What is the syntax for format strings?
|
%[arg_index$][flags][width][.percesion]character
|
|
Which code is good to look at?
|
NavigableExample.java
|
|
A non-generic collection is happy to hold anything but _________
|
primitives
|
|
So, when you think of default access, think package restriction. Noexceptions. But when you think protected, think package + kids.
|
|
|
To overload a method you cannt change only the return type
|
|
|
What does nextBoolean() return? nextInt()?
|
a boolean. An integer.
|
|
What kind of exception is thrown when there is a mistake in the static initialization block?
|
ExceptionInInitializationError
|
|
Is it appropriate to use assertions to validate command-line arguments?
|
No
|
|
Is ArrayList a growable collection?
|
yes it is growable
|
|
RunTimeExceptions are checked or unchecked? how about non-RunTimeExceptions?
|
RunTimeExceptions are non-checked. That means the compiler won't check to see if you decalred them. Look at the table on page 382
|
|
HashSet is ordered or unordered? sorted or unsorted?
|
unordered and unsorted
|
|
Prior to Java 5, there was no generics
|
NA
|
|
What is the [.percision]
|
it's how many decimal places
|
|
Every time you make a new object, at least oneconstructor is invoked. Every class has a constructor, although if you don't createone explicitly, the compiler will build one for you.
|
|
|
String p = ".";
|
treated as a meta-character when compiled into a pattern
|
|
What if you have an Animal whose not serialized and a Dog who is serialized, will the instance variables of Animal be serialized?
|
No
|
|
Which method is used to see if there is stuff more to tokenize? (in the loop)
|
hasNext()
|
|
What are the different implementations for the Map interface?
|
HashMap, LinkedHashMap, TreeMap, HashTable
|
|
What is the difference between a Vector and ArrayList?
|
Vector is thread-safe
|
|
How do you implement Comparable?
|
You implement the method compareTo()
|
|
For examples on how to use these methods where do i look?
|
page 435
|
|
if you have x=0123456789. What is the output of x.substring(5);? x.substring(5, 8)
|
56789, 567
|
|
The first purpose of having wrapper classes is to create objects out of primitive types. What is the second purpose?
|
To convert stuff
|
|
Let's say you have a Dog class. You want to save objects of the Dog class, then you make it implement Serializable. If the Dog class has a Collar class as instance variable, this will have to implement serializable as well. Otherwise, you'll get a NotSeri
|
NA
|
|
What does reverse do? what arguments does it take?
|
reverses a StringBuffer/StringBuilder object. takes nothing
|
|
How can we use BufferedReader?
|
File f = new File("filename.txt"); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String data = br.readLine();
|
|
How many implementions does List has?
|
3. ArrayList, Vector and LinkedList
|
|
what does trim() do?
|
Removes whitespace from the end and the beginning of a String
|
|
Which method do you use to serialize? deserialize? these are part of which Classes?
|
writeObject(), readObject(), ObjectOutputStream and ObjectInputStream
|
|
How are static variables serialized?
|
Static variables are NEVER serialized, as they are not part of any object
|
|
How do you create a Calendar instance?
|
Calendar.getInstance(). The Calendar class is an abstract class. So you use one of the static getInstance() methods
|
|
How do you create an iterator?
|
Iterator<Dog> i = d.iterator(); //where d is a list instance
|
|
When overloading methods, which is dominatent? widening, boxing, and var-args
|
Widening beants boxing, and wideniing beats var-args.Also, boxing beants var-args. Look at the examples on page 248, 249 and 250
|
|
What should be true about the variables used in equals() and hashCode()?
|
should be the same variables
|
|
If we want to add elements to a Set, what do we use? to a Map?
|
add(), put()
|
|
How do you instantiate a NumberFormat object?
|
using the getInstance or getCurrencyInstance
|
|
If using any of the backed collections (subMap, subSet, headMap, headSet, tailMap, tailSet), and if you're passing a boolean as an argument, then the return reference is of type _________ or _________. If not using a boolean, then the return reference is
|
NavigableSet, NavigableMap, SortedSet, SortedMap
|
|
What's another solution?
|
A solution for this issue is to use the transient modifier. If you mark the Dog's Collar instance variable with transient, then serialization will simply skip the Collar during Serialization. Look at the code at the middle of page 465
|
|
When is a static initialization block run? Instance block?
|
Static Initialization blocks are run when the class is first loaded. Instance initialization blocks are run when an instance is created
|
|
Why does read take char[]?
|
because read reads the entire file one character at a time. We should know what the maximum number of characters before hand. This could be a problem
|
|
what if using the split method, you want to use the dot as a dellimeter. What do you do?
|
s.split("\\."); //because one dot means any character. \. is a compile error
|
|
Which example should we look at?
|
middle of 491 and bottom of 492
|
|
So with the <? extends Animal> syntax, you can pass any subtype of Animal and we can achieve our goal of passing arguments. However, we can Add things to the collection. What if we do want to add, what do we use?
|
public void addAnimal(List<? super Dog> animals){ ...
|
|
Can you create a File instance in a directory that doesn't exist?
|
No. Exception is thrown
|
|
What does setParseIntegerOnly () take as an argument? what is it used for?
|
takes a boolean. Example on page 486
|
|
Two key points to remember aboutconstructors are that
|
they have no return type and their names must exactly matchthe class name.
|
|
<? extends Animal> has two meanings..what are they
|
that any subtype of animal can be taken, or a type that IMPLEMENTS the interface Animal. so the keyword extends can represent extending a class or implementing an interface
|
|
How do you rename a directory/File?
|
//assume f1 already exists. File f2 = new File("newName.txt"); f1.renameTo(f2);
|
|
When do we use a HashSet?
|
When we don't need any duplicates in our collection and we don't care about the order
|
|
How many times does a static initialization block run?
|
One. When the class is first loaded
|
|
How many times does an instance initialization block run?
|
Once everytime an instane variable is created
|
|
How about the code at the bottom of 620?
|
No. Because we're passing List<Integer> and receiving List<Object>
|
|
toArray comes in two different flavours, what are they?
|
one that returns an array. and one that takes an array arguments and work on it. Look at the bottom of 579
|
|
How many times can finalize() run?
|
at most once. It's not even guaranteed to run because objects are not guaranteed to be deleted
|
|
So what is the functionality of parseXxx() and valueOf()? What is common between them? what is the difference between them?
|
They both convert Strings to numbers. The difference bewteen the two is that parseXxx() returns a primitive. valueOf() returns newly created wrapper object of the type that invoked that method. A similarity between the two is that both are static
|
|
When are two wrapper variables "=="?
|
When the boolean value is the same, when the byte value is the same, when the character is from \u0000 to \u007f and the same, when the short value is between -128 to 127 and the same, or when the int value is between -128 and 127 and the same. Look at the points on the middle of page 246
|
|
What is the result of the following tokenization? "ab5 ccc 45 @" "\d"
|
"ab", " ccc ", "", " @"
|
|
What is the result of assert(i = 1);
|
compile error. the stuff in between the parenthesis should evaluate to a boolean
|
|
When is an instance initialization block run?
|
Right after the call to super() in a constructor. Therefore, after all the super constructors have run
|
|
When are two wrapper variables "equal"?
|
When they are the same type and the same value
|
|
Which of logical operators are short-circuit? and why are they called that?
|
&& and ||. Because they don't have to evaluate the second operand if the first is enough. Example look at top of 307
|
|
So the Collections class can sort an ArrayList of String(s), but not an ArrayList of Foo(s). Why?
|
Because Foo doesn't have a natural order. Look at the compile error on top of 571
|
|
Look at the code at the bottom of 295 and determine why we need a downcast?
|
Because A does not have doStuff
|
|
Is it a good idea to use transient variable in calculating the hash code?
|
No. Because if the object is serialized, and then deserialized, it will have different values. Hence not able to retrieve the correct value from the HashMap. For an example look at page 555
|
|
What method od you have to implement in order to use Comparator?
|
compare(). Look at the first code on page 574
|
|
if you have: Animal a = new Dog(). and then you have a instanceof Dog. What is the result?
|
true because a is a dog
|
|
All what we've been looking at is Widening and Boxing seperatly. What happens when you have to box then widen? widen then box?
|
Box then widen is legal. Widen then box is illegal. Look at the examples on page 252
|
|
write a snippet of code of a class to implement comparable
|
class Foo implements Comparable<Foo> { ... public int compareTo(Foo){ ... } }
|
|
If you have a finally block and a catch block, how should they be ordered?
|
finally blocks come right after the catch blocks
|
|
Will the code compile if you thrown an Error and not catch it?
|
yes. because the application won't be able to recover from an error
|
|
But what would happen if we didn't have access to the Collar class source code? In other words, what if making the Collar class serializable was not an option?
|
One solution is to subclass the Collar class, mark the subclass as Serializable, and then use the Collar subclass instead of the Collar class. But that's not always an option for many reasons: The Collar class can be marked as final, or the Collar class might itself refer to other non-Serializable objects, and without knowing the internal structure of Collar, you aren't able to make all these fixes
|
|
What is true about the value of a wrapper variable after they have been declared?
|
Once they are declared, they can't be changed. If you post increment for example, a new wrapper variable is created behind the scenes and assigned a referece
|
|
Can you mix correct generics code with non-generics code?
|
yes. For an example look at page 601. The addAll() method assumes that the list is indeed only contains Integers.
|
|
So what do u do if you want to decalre assert as an identifier?
|
you compile it with -source 1.3
|
|
How do you get the list of files in a directory? what does it return?
|
list(). it returns an array of Strings
|
|
If you add a Dog to a ArrayList of Strings, what is the result?
|
compile error. Look at the last three lines on page 597
|
|
Is the following legal? Float f1 = new Float("3.14")
|
No. It should be Float f1 = new Float("3.14f")
|