121 - 150 Flashcards
what packages are imported automatically?
only java.lang
What letters, and in what order, will be printed when the following program is compiled and run?
public class FinallyTest{ public static void main(String args[]) throws Exception{ try{ m1(); System.out.println("A"); } finally{ System.out.println("B"); } System.out.println("C"); } public static void m1() throws Exception { throw new Exception(); } }
It will print B and throw Exception.
An Exception is thrown in method m1() so println(“A”) will not be executed.
As there is no catch block the exception will not be handled and the main() method will propagate the exception. So println(“C”); will also not be executed.
‘finally’ block is always executed (even if there is a return in try but not if there is System.exit() ) so println(“B”) is executed.
public class TestClass { public static void main(String[] args) { String myStr = "good"; char[] myCharArr = {'g', 'o', 'o', 'd' }; String newStr = null; for(char ch : myCharArr){ newStr = newStr + ch; } System.out.println(newStr); } }
output: nullgood
can constructor be inherited?
Constructors are NEVER inherited
what is AssertationError?
AssertionErrors are thrown when assertions fail. Assertions fail because something the programmer believed to be true turns out not to be true. That means there is a bug in the code
break and continue –> work with label outside loop?
break can work with label (outside loop) but continue cannot
preference: widening, boxing / unboxing, var-args
class TestClass{ void probe(int... x) { System.out.println("In ..."); }
void probe(Integer x) { System.out.println("In Integer"); } void probe(long x) { System.out.println("In long"); } void probe(Long x) { System.out.println("In LONG"); }
public static void main(String[] args){ Integer a = 4; new TestClass().probe(a); int b = 4; new TestClass().probe(b); } }
widening > boxing / unboxing > var-args
output: In Integer and In long
what happens when both catch and finally block define return statements?
If both catch and finally blocks define return statements, the calling method will receive a value from the finally block
char a;
short b;
without casting –> OK?
A: a = b
B: b = a
same to byte and int?
no. Neither A nor B is correct –> need casting because their ranges are different
short: -32,768 to 32,767
char: 0 to 65,535
–> same to byte but not int because range of int is bigger than ranges of short, char and byte
run a constructor in the subclass –> what needs to be done with the superclass?
–
public class SubClass extends SuperClass{ int i, j, k; public SubClass( int m, int n ) { i = m ; j = m ; } //1 public SubClass( int m ) { super(m ); } //2 }
Which of the following constructors MUST exist in SuperClass for SubClass to compile correctly?
- run constructor in the superclass. Constructor in the subclass has no constructor from superclass –> implicit default constructor
–
- public SuperClass() –> the default no args constructor will not be provided because SuperClass has to define one arg constructor
- public SuperClass(int a) –> because it is called in the second constructor of SubClass
- what is instanceof for?
- when can use it?
- null –> instance of?
- operand
- compares an object to a specified type
- use it, when having a reference or parameter to an object that is of a super class or interface type and need to know whether the actual object has some other type
- use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface –> null is not instance of anything
- operand
+ left operand MUST be an object + not a primitive
+ right operand MUST be a class name
narrowing primitive conversion:
- bigger than int
- equals or smaller than int
- bigger than int —> need explicit casting
- equals or smaller than int
o implicit casting
o right hand side must be
+ constant / final
+ or the value must fit the range of the left hand side
int[][] abc = new int[2][];
System.out.println(abc.length);
System.out.println(abc[0].length);
System.out.println(abc.length); –> 2
System.out.println(abc[0].length); –> NullPointerException
StringBuilder –> ensureCapacity?
Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:
- The minimumCapacity argument.
- Twice the old capacity, plus 2.
If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.
can call both super(…) or this(…) from a constructor?
no
can either call super(…) or this(…) but not both from a constructor