91 - 120 Flashcards
what happen when do not catch an exception?
if do not catch an exception, it is propagated up the stack of method calls until it is handled
If nobody handles it, the JVM handles that exception and kills the thread. If that thread is the only user thread running, the program ends.
StringBuilder:
- substring() –> modify value?
- append()
- insert()
- delete() & deleteCharAt()
- reverse() - reverse substring?
- replace() –> end position?
- subSequence()
o substring() = not modify o append() --> adds the specified value at the end of the existing value of a StringBuilder object o insert() --> insert the requested data at a particular position removes the characters in a substring of the specified StringBuilder o deleteCharAt() --> removes the char at the specified position o reverse() --> reverse the sequence of characters of a StringBuilder + cannot reverse a substring of StringBuilder o replace() --> replaces a sequence of characters / substring, identified by their positions (end position not included) o subSequence() ~ substring()
StringBuilder:
setLength()
newLength >= current length?
sets the length of the character sequence to a new character sequence whose length is specified by the argument
- newLength argument length is changed to the specified length
- newLength argument >= current length –> sufficient null characters (‘\u0000’) are appended so that length becomes the newLength argument
Primitive wrappers + java.lang.System
- final?
- meaning?
yes. Final –> cannot be extended
how do compound assignment operators cast?
all compound assignment operators internally do an explicit cast with the type of the variable on the left hand side *= \+= -= /=
can catch block for exception catch an Error?
no
String, StringBuilder, StringBuffer —> final? What does this mean? (2)
- cannot be extended
- all methods of these 3 classes are also final
while-loop —> condition expression blank?
no —> cannot leave blank = must have a condition
To construct an instance of a sub class, what needs to be done?
to construct an instance of a sub class, its super class needs to be constructed first.
Since an instance can only be created via a constructor, some constructor of the super class has to be invoked. Either you explicitly call it or the compiler will add super() (i.e. no args constructor) as the first line of the sub class constructor
? constructor –> what should both side of : be?
both sides of : should return some value –> break and continue do not work
however, this works:
?true:false
order of the exceptions caught in the catch blocks
1 . unrelated class 2. IS-A relationship
- unrelated classes –> order does not matter but each catch must be of a different type
- related classes sharing an IS-A relationship: catch an exception of the base class before an exception of the derived class –> Fail to compile
try - catch - finally
- exist independently?
- 1 try block - how many catch and finally block?
- what follow try block?
- try, catch, and finally blocks cannot exist independently
- 1 try block - (can define) multiple catch blocks - only a single finally block
- try block may be followed by either a catch or a finally block or both
throw exception in Method and Constructor –> difference?
Observe that the rule for overriding a method is opposite to the rule for constructors.
- An overriding method cannot throw a superclass exception –> must be same or sub
- A constructor of a subclass cannot throw subclass exception –> must be same or super
how must checked exception be dealt with?
must be handled by either a try catch block or declared in the throws clause of the enclosing method
overriding method - overridden method:
- return type
- access modifier
- an overriding method can change the return type of the overridden method to a subclass of the original return type
–> Covariant returns
- access modifier of the overriding method cannot be more restrictive