(a) When programming with Java, most classes need to be imported before they can be used in an application. Why are applications able to use the class System without first importing it? (2 marks)
(b) What is the importance of exception handling in Java programming? (2 marks)
(c) Explain why we got compilation errors in the following Java code. (3 marks)
public class Test {
public static void main(String[] args) {
try {
int a[] = {1, 2, 3, 4};
for (int i = 0; i <= 3; i++) {
System.out.println("a[" + i + "]=" + a[i] + "\n");
}
} catch (Exception e) {
System.out.println("Error is " + e);
} catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException");
}
}
}
(d) Explain the usage of throw ex in the following code segment. (3 marks)
try {
String s = null;
System.out.println(s.length());
NullPointerException
} catch (NullPointerException ex) {
System.out.println("NullPointerException is caught here");
throw ex;
}