1) When do you encounter NullPointerException?(Choose
as many as apply from the options below)
a) When you access item n in an
array of size n (ie a[10] where a.length == 10).
b) When you try to access an object
which has been declared but has not been initialised.
c) When you try to access an object
which has not been declared.
d) When you try to access an object
outside the scope of its declaration.
Solution: b
2-9) Explain the differences between the following
pairs of terms:
a) Throwing an exception and
catching
an exception.
You 'throw' an exception when an error
is encountered and you 'catch' an exception when you want to
handle the error.
b) Try block and catch block.
The try block is where you 'try' to execute the code
that might throw an exception and the catch block is where you put the
code that is to be executed in case the exception occurs.
c) Catch block and finally block.
The catch block is where you put the code that is to
handle the exception and the finally block is where you put the code that
you want executed whether or not an exception is thrown.
d) Try block and finally block.
The try block is where you try the code that might throw
an exception and the finally block is where you put the code that you
want executed whether or not an exception is thrown.
e) Dynamic scope and static scope.
Dynamic scope refers to the way a program is executed(runtime).
Static scope refers to the way the program is written(compiletime).
You can only determine dynamic scope by executing the program but you can
determine the static scope by reading the program.
f) Dialog box and top-level window.
A dialog box is a window that can be opened to communicate
with the user and a top-level window is the base window that is created
when your application is started.
g) Checked and unchecked expression.
A checked exception is an exception that must be accounted
for by handling it or throwing it, otherwise the compilier will complain.
An unchecked exception is an exception that you do not have to explicitly
account for as Java will handle the exception automaticlly for you if you
do not handle it.
h) Method stack and method call.
The method stack is the list of methods that have been
called and a method call is when you actually call the method in your program.