Lecture 8 and 9 - Strings, Arrays, Exceptions Flashcards

1
Q

What is an immutable object ?

A

if its contents or state cannot be changed once it has been created.

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

Array characteristics :

A
size ==> fixed
data type ==> primitives, objects
get size ==> .length
get item ==> [i] 
dimension ==> multi-dimensional
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

ArrayList characteristics :

A
size ==> re-sizable
data type ==> objects
get size ==> .size()
get item ==> .get(i)
dimension ==> one-dimensional
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

String objects are special. Why?

A

Creating a string: String s=”ok”;
String objects are immutable.
Contactinating strings: String s=”Ms “+”Elly”;

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

What is an exception ?

A

An exception is an abnormal event that arises during the execution of the program and disrupts the normal flow of the program.

When an error occurs during the running of a program in Java, an Exception object is automatically generated.

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

What is exception handling ?

A

Exception handling is to handle the runtime errors so that normal flow of the application can be maintained:
try-catch
try-catch-finally

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

Java exception classes are categorised into:

A

unchecked exceptions => is not checked during compilation.

checked exceptions => is checked during compilation.

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

Unchecked exceptions explenation

A

any exception that IS A SUBCLASS of RuntimeException (as well as RuntimeException itself).

are not checked by the compiler.

examples:
ArithmeticException
int a = 2/0;
NullPointerException
Rectangle r;
r.getWidth();
NumberFormatException
int i=Integer.parseInt("book");
ArrayIndexOutOfBoundsException
int[] anArray = new int[3];
System.out.println(anArray[3]);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When an unchecked exception might occur, two options:

A

Don’t handle it - Do nothing.

Handle it with a try-catch or try-catch-finally

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

When a checked exception occurs, two options:

A

Don’t handle it - declare it, i.e. does not handle it inside this method, but propagate it down to the next method in the call stack.

public void methodsX() throws IOException{
____methodY();
}

Note: The keyword for declaring is throws, NOT throw.

catch it, i.e. handle the exception using try-catch or
try-catch-finally.
public void methodX(){
\_\_\_\_try {
\_\_\_\_\_\_\_\_methodY();
\_\_\_\_} catch (IOException ex) { ...}
}

Note: The exception categorization (unchecked or checked) affect compile-time behavior only; they are handled identically at runtime.

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

What is exception propagation?

A

When a method throws an exception, the JVM searches backward through the call stack for a matching exception handler.

If exists an handler, the rest of code is executed.
If no handler, print out the stack trace and exception type,and terminates the program.

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