91 - 120 Flashcards

1
Q

what happen when do not catch an exception?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

StringBuilder:

  • substring() –> modify value?
  • append()
  • insert()
  • delete() & deleteCharAt()
  • reverse() - reverse substring?
  • replace() –> end position?
  • subSequence()
A
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()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

StringBuilder:

setLength()

newLength >= current length?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Primitive wrappers + java.lang.System

  • final?
  • meaning?
A

yes. Final –> cannot be extended

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how do compound assignment operators cast?

A
all compound assignment operators internally do an explicit cast with the type of the variable on the left hand side
*=
\+=
-=
/=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

can catch block for exception catch an Error?

A

no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

String, StringBuilder, StringBuffer —> final? What does this mean? (2)

A
  • cannot be extended

- all methods of these 3 classes are also final

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

while-loop —> condition expression blank?

A

no —> cannot leave blank = must have a condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

To construct an instance of a sub class, what needs to be done?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

? constructor –> what should both side of : be?

A

both sides of : should return some value –> break and continue do not work

however, this works:

?true:false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

order of the exceptions caught in the catch blocks

1 . unrelated class
2. IS-A relationship
A
  1. unrelated classes –> order does not matter but each catch must be of a different type
  2. related classes sharing an IS-A relationship: catch an exception of the base class before an exception of the derived class –> Fail to compile
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

try - catch - finally

  • exist independently?
  • 1 try block - how many catch and finally block?
  • what follow try block?
A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

throw exception in Method and Constructor –> difference?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how must checked exception be dealt with?

A

must be handled by either a try catch block or declared in the throws clause of the enclosing method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

overriding method - overridden method:

  • return type
  • access modifier
A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is?

  • null array
  • empty array
A
  • 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];

17
Q

class A calls method B, which throws exception X –> what should A do?

A

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
18
Q

what is instance variable? Its accessibility

A
  • 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
19
Q

are local variables initialized with default value?

A

local variables are not initialized with their default values

–> must be explicitly initialized

20
Q

without argument –> is array in main method assigned to null?

A

no –> the length is 0

21
Q

switch –> case statement: what should value be? (2)

A

compile-time constant value

  • the value should be known at the time of code compilation
  • cannot pass an expression if variable is not final
22
Q

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);
  }
}
A

print A

23
Q

which declaration is correct?

A: double x = 10, double y;
B: double x = 10, y = 5;

A

A —> wrong

B —> correct

24
Q
  • what is method signature?

- more than one method in a class with the same signature?

A
  • 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.
25
Q

operator with least precedence?

A

=

26
Q

exception in overriding method

  • Runtime
  • Checked
A
  • Runtime Exception / Unchecked Exception
    o can only throw
    + exceptions that are declared in the throws clause of the overridden method
    + exceptions that are subclasses of the declared exceptions
    o can choose NOT to throw any exception.
  • Checked Exception: if overridden method does not have any throw clause, the overriding method cannot declare any checked exceptions
27
Q

can abstract method overrides non-abstract method?

A

yes

independent on abstract / non-abstract

28
Q

A:
int a = b = c = 100;

B:
int a, b, c;
a = b = c = 100;

C:
int b = 0, c = 0;
int a = b = c = 100;

which one is correct?

A

A —> wrong

B, C —> correct

29
Q

static variable

  • can static variable be left without being initialized?
  • no instances of the object of the class have been initialized —> can be accesses?
A
  • yes —> default value

- yes. Can be accessed

30
Q

làm tròn số

  1. 5
    - 0.5
A
  1. 5 —> 1
    - 0.5 —> 0

Observe that rounding is a standard mathematical procedure where the number that lies exactly between two numbers always rounds up to the higher one

31
Q

&& - || –> precedence?

A

&& have more precedance than || operator

32
Q

package
import

–> which should come first?

A

package