31 - 60 Flashcards

1
Q

NumberFormatException

o base class
o function?
A

o thrown to indicate that the application has tried to convert a string (with an inappropriate format) to one of the numeric types

o subclass of IllegalArgumentException

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

ClassCastException –> what is?

A

is thrown when an object fails an IS-A test with the class type to which it’s being cast

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

if not overload toString() –> what will be printed?

A

object’s name + hashCode()

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

can use “this” within static Method?

A

no

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

order of execute (2)

A

o direct superclass must be initialized

o SIC way

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

what is for-each loop?

How to read: for(String day : days)

A

enhanced for loop

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

which kind of access modifiers can be set to top class?

A

public, default

no protected & private

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

short b = 3;

b += 4.6;

output? Nguyên tắc cast của operand

A

o implicit cast to the type in left

o result = 7

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

cách viết main method

A

public static void main(String[] args){

public static void main(String args[]){

static public void main(String args[]){

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

how many public class / interface can be in a Java file? Name?

A

only 1 + same name as this file

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

limit of enhanced for loop with: array, collection

A
  • cannot be used to initialize an array + modify its elements
  • cannot be used to delete or remove the elements of a collection
  • can’t iterate over multiple collections or arrays in the same for-each loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

exception handling –> order of exception

A

derived class –> base class

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

where in the constructor can super, this be used?

A

first statement

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

exception

o overriding - overridden method
o constructor - base constructor

A

overriding method / constructor can

  1. only throw
    1. 1. exceptions that are declared in the throws clause of the overridden method
    1. 2.exceptions that are subclasses of the declared exceptions
  2. choose NOT to throw any exception
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

if a method uses another method that may throw a checked exception –> what does this method need? (2)

A
  • the method should be enclosed within a try-catch block

- the method should specify this exception to be thrown in its method signature

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

throw keyword:

  1. base class does not throw exception –> derived class?
  2. which kinds of exception can base class throw freely?
A
  1. cannot throw an exception only applying to checked exception
  2. Errors and Runtime Exception
17
Q

what should a class do when it implements an interface? (2)

A
  • when a class implements an interface, it should implement all of the methods defined by this interface
  • if it doesn’t, it must be defined as an abstract derived class
18
Q

a call needs to be wrapped up in try / catch block or with throws clause –> depend on class of reference or class of actual object?

A

class of reference

19
Q

overridden method has throw –> does overriding have to have?

A

overriding method may choose to have no throws clause even if the overridden method has a throws clause

20
Q

can use “this” within static Method? Use “this” to access static variable?

Can write: this.i = 4?

A

not within static method. Can use to access static variable

this.i = 4 –> wrong –> cannot use like a local variable

21
Q

IllegalStateException & IllegalArgumentException

A
  • IllegalArgumentException: specify that a method has passed illegal or inappropriate arguments
  • IllegalStateException: signals that a method has been invoked at an illegal or inappropriate time
22
Q
class A implements interface I
class B expends class A

–> can class B use interface I?

A

can

when base class implements an interface, derived class can use this interface without implementing it

23
Q

members of an interfaces are implicitly?

A

all members of an interface are implicitly abstract and public, static, final

24
Q

what happens when a constructor in a subclass is invoked?

A
  • when a constructor in a subclass is invoked, some constructors of its superclass have to be invoked first

+ explicitly call it

+ or the compiler will add super() as the first line of the subclass constructor

  • baseclass has to have a no args constructor
25
Q

many overloaded methods with many “appropriate” parameters –> choose

A
  1. compiler always tries to choose the most specific method available with least number of modifications to the arguments
  2. Java designers have decided that old code should work exactly as it used to work before boxing-unboxing functionality became available
  3. Widening is preferred to boxing/unboxing (because of rule 2), which in turn, is preferred over var-args ===> widening > boxing / unboxing > var-args
26
Q
  • trim() is method of?

- trimToSize() is method of?

A
  • trim() is method of String

- trimToSize() is method of ArrayList

27
Q

ExceptionInInitializerError –> when?

A

is typically thrown by the JVM when a static initializer in the code throws any type of RuntimeException

28
Q

how to write octal & binary number?

A

binary number begins with 0b…. + not L, D, F in suffix

Octal –> 0… (không có b hoặc B) + not L, D, F in suffix

29
Q

abstract method –> body? What does a method body look like? What does that mean?

Access modifier

static?

A

no. Body { }
no block. Only semicolon
no private
cannot static

30
Q

precedence: == và =

A

precedence: == > =