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
what is?
- null array
- empty array
- a null array is a null Array Reference (since arrays are reference types in Java)
int[] notAnArray = null;
- an empty array is an array of length zero; it has no elements
int[] emptyArray = new int[0];
class A calls method B, which throws exception X –> what should A do?
class A should either (or both):
- throws exception Y, which is the same or subclass of X
- wrap up the exception in a try - catch block
what is instance variable? Its accessibility
- non-static variable + declared within a class, outside of all of the methods
- accessible to all the non-static methods defined in a class + only objects of this class will have this field
are local variables initialized with default value?
local variables are not initialized with their default values
–> must be explicitly initialized
without argument –> is array in main method assigned to null?
no –> the length is 0
switch –> case statement: what should value be? (2)
compile-time constant value
- the value should be known at the time of code compilation
- cannot pass an expression if variable is not final
What will be the result of attempting to compile and run the following program?
public class TestClass{ public static void main(String args[ ] ){ Object a, b, c ; a = new String("A"); b = new String("B"); c = a; a = b; System.out.println(" "+c); } }
print A
which declaration is correct?
A: double x = 10, double y;
B: double x = 10, y = 5;
A —> wrong
B —> correct
- what is method signature?
- more than one method in a class with the same signature?
- includes method name and the argument list but does not include return type.
- cannot have more than one method in a class with the same signature.