Language Translator

JAVA EXCEPTIONS DESCRIPTION

Q. The two immediate subclasses of Throwable class are: __________ and _________.

Ans. Exception and Error.

Q. What is an exception?

Ans. An exception is basically a runtime error.

Q. How does an exception arise in a Java code? Illustrate with the help of an example.

Ans. Consider the following code snippet:

public class A
{
public static void main(String args[])
{
int k=4/0;
System.out.println(k);
}
}

The statement int k=4/0; generates an object of the class ArithmeticException via the statement;

ArithmeticException e=new ArithmeticException();

This object starts searching from the line where the exception arose, for a piece of code which can accept it and process accordingly. As a result, all subsequent lines of code are skipped from being executed.

Q. What are the cases in which the finally block is executed?

Ans. The finally block is always executed in the following cases:

1. When no exception arises.
2. When an exception arises and it is handled by one of the catch blocks.
3. When an exception arises and it is not handled by any of the catch blocks.

Q. Is it necessary to write a finally with a try-catch block?

Ans. No, it is not at all mandatory to write a finally block with a try-catch block. This is because a finally block contains code which should execute whether or not the exception arises. However this might not always be required.
Usually, a finally block is used to place code that will release resources acquired in a try block. This strategy is an effective way to avoid resource leaks.

Q. Can a try statement be used in isolation, i.e., without a catch or finally block?

Ans. No, a try statement cannot be used in isolation. It should be followed by either the catch block or the finally block.

Q. What is the output of the following code:

public class A
{
public static void main(String args[])
{
try
{
int num=4/0;
}
catch(Exception e)
{
System.out.println(“Inside Exception”);
}
catch(ArithmeticException e)
{
System.out.println(“Inside ArithmeticException”);
}
}
}

Ans. Output:
Error: exception java.lang.ArithmeticException has already been caught catch(ArithmeticException e)

Reason:
When multiple catch statements are used, it is important to remember that exception subclasses must come before any of their superclasses. This is because, a catch statement that uses a superclasses will catch exceptions of that type plus any of its subclasses. Thus, a subclass would never be reached if it comes after its superclass. In the above case, Exception is the superclass of all types of exceptions including ArithmeticException.




Q. What is the output of the following code?

public class A
{
public static void main(String args[])
{
try
{
try
{
int num=4/0;
}
catch(NullPointerException e)
{
System.out.println(“Inside NullPointerException”);
}
}
catch(ArithmeticException e)
{
System.out.println(“Inside ArithmeticException”);
}
}
}

Ans. Ouput: Inside ArithmeticException

Reason: In case of nested try-catch statements, if an inner try statement does not have a catch handler for a particular exception, the next try statement’s catch handlers are examined for a match. This continues until one of the catch statements succeeds or until the entire nested try statements are exhausted.

Q. Is it possible to create exceptions of our own?

Ans. Yes, it is possible to create user-defined exceptions. This can be done by extending the Exception class as:

public MyException extends Exception
{
………
………
}




Q. Is it possible that the finally block is not executed?

Ans. Yes, it is possible that the finally block is not executed. This is possible when there is an exception in the finally block itself.

Q. What is the use of throws keyword?

Ans. If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of that method can guard themselves against that exception. This is done by including a throws clause in the method’s declaration. A throws clause lists the types of exceptions that a method might throw.

Q. What is the error in the following code:

void fun() throws NullPointerException, ArithmeticException

……….

public class A
{
public static void main(String args[])
{
try
{
fun();
}
catch(NullPointerException e)
{
}
}
}

Ans. For all the exceptions listed in the throws list of a method, a catch block should be written; otherwise the compiler would report an error. In the above example, the try block should be succeeded by two catch blocks for handling objects of the type NullPointerException and ArithmeticException.

Reason: The Java compiler forces the user to write catch blocks for each of the exceptions mentioned in the throws clause because it is not known till run-time that which exception out of the pre-specified list can be thrown by the method. Therefore, in order to make provision for every possible contingency, the compiler forces the programmer to write a catch block for all exceptions mentioned in the throws exception list.


Q. What is the error in the following code?

public class A
{
public static void main(String args[])
{
float f=2.5f;

switch(f)
{
case 5: System.out.println(“Hello”);
break;

case 2.5: System.out.println(“Bye”);
break;

default: System.out.println(“Not Found”);
}
}
}

Ans. The switch expression can take only the following types of values:
1. byte
2. short
3. int
4. char
It cannot take a float or any other type.

Q. What is a null statement in Java?

Ans. A statement that consists of only a semicolon is called a null statement.

Q. What is the output of the following code?

………
for(int i=0;i<4;i++)
System.out.println(“Hello”);

System.out.println(i);
……….

Ans. Output: cannot resolve the symbol: variable i
Reason: The scope of a variable inside a for-loop ends when the for block ends. Outside the for-block, the variable ceases to exist.



Q. What is the error in the following code?

public class A
{
public static void main(String args[])
{
int t=5, x=10, y=2;

switch(t)
{
case(10/2):
System.out.println(“Hello”);
break;

case(x/y): System.out.println(“Bye”);
break;

default: System.out.println(“Not Found”);
}
}
}

Ans. A case value must be a constant and not a variable. It can be an expression involving only constants which evaluates to a constant. An expression involving variables is not allowed.

Q. What is the error in the following code?

public class A
{
public static void main(String args[])
{
int t=9;

switch(t)
{
case(10-1):
System.out.println(“Hello”);
break;

case(3*3):
System.out.println(“Bye”);
break;

default: System.out.println(“Not Found”);
}
}
}

Ans. Duplicate case values are not allowed.

Q. What is the output of the following code?

public class A
{
public static void main(String args[])
{
int t=9;

switch(f)
{
case(10-1):
System.out.println(“Hello”);

case 3:
System.out.println(“Bye”);

case 4:
System.out.println(“Good Day”);
break;

default:
System.out.println(“Not Found”);
}
}
}

Ans. Output: Hello
Bye
Good Day

Reason: If there are no break statements after any case, it would result in the execution of all the case blocks starting from the first case value which matches with the switch expression.












Q. What is the use of the break statement?

Ans. The break statement causes the termination of a loop and the program control resumes at the next statement following the loop. For example,

for(……..)
{
for(……..)
{
for(……..)
{
if(……..)
break;
}
//Control resumes from here.
}
}

Q. What is the result when you compile and run the following code?

int i = 100;
switch (i)
{
case 100:
System.out.println(i);
case 200:
System.out.println(i);
case 300:
System.out.println(i);
}

[a] Nothing is printed
[b] Compile time error
[c] The values 100,100,100 printed
[d] Only 100 is printed

Ans. The values 100, 100, 100 are printed.








Q. How can you change the break statement below so that it breaks out of the inner and middle loops and continues with the next iteration of the outer loop?

outer:
for ( int x =0; x < 3; x++ ) {
middle: for ( int y=0; y < 3; y++ ) {
if ( y == 1)
break;
}
}
}

[a] break inner:
[b] break middle:
[c] break outer:
[d] continue
[e] continue middle

Ans. break middle;
Reason: When this form of break executes, the control is transferred out of the named block of code. The labeled block of code must enclose the break statement, but it does not need to be the immediately enclosing block. This means you can use a labeled break statement to exit from a set of nested blocks. But you cannot use break to transfer control to a block of code that does not enclose the break statement.

Q. Select all correct answers:
public class ExpTest {
public static void main ( String[] args ) {
try {
MyMethod();
} catch ( Exception e) {
}
}
static void MyMethod() {
try {
System.out.println("a");
} catch ( ArithmeticException ae) {
System.out.println("b");
} finally {
System.out.println("c");
}
System.out.println("d");
}
}

[a] a
[b] b
[c] c
[d] d

Ans. a
c
d

Q. What is the result of the following code when you compile and run?

public class ThrowDemo
{
static void demoMethod()
{
try
{
throw new NullPointerException("demo");
}
catch(NullPointerException e)
{
System.out.println("Caught inside demoMethod.");
throw e; // re-throw the exception
}
}
public static void main(String args[])
{
try
{
demoMethod();
}
catch(NullPointerException e)
{
System.out.println("Recaught: " + e);
}
}
}

[a] Compilation error
[b] Runtime error
[c] Compile successfully, nothing is printed.
[d] Caught inside demoMethod. followed by Recaught: java.lang.NullPointerException: demo



Ans. Caught inside demoMethod. followed by Recaught: java.lang.NullPointerException: demo

Reason: NullPointerException is one of Java’s unchecked exceptions. Therefore, there is not need to mention it in the method’s throws list.

Q. Select the correct answer:

public class ThrowsDemo
{
static void throwMethod()
{
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}

public static void main(String args[])
{
try
{
throwMethod();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
}
}
}

[a] Compilation error
[b] Runtime error
[c] Compile successfully, nothing is printed.
[d] inside demoMethod. followed by caught: java.lang.IllegalAccessException: demo

Ans. Compilation error.
Reason: IllegalAccessException is one of Java’s checked exceptions. Therefore, it has to be mentioned in the throws list of throwMethod().









Q. What is the output of the following program if you compile and run?
outer: for (int i = 0; i < 2; i++ ) {
for ( int j = 0; j < 3; j++ ) {
if ( i == j ) {
continue outer;
}
System.out.println("The value of i is: " + i );
System.out.println("The value of i is: " + j);
}
}
[a] The value of i is: 0
The value of j is: 0
[b] The value of i is: 0
The value of j is: 1
[c] The value of i is: 0
The value of j is: 2
[d] The value of i is: 1
The value of j is: 0
[e] The value of i is: 1
The value of j is: 1
[f] The value of i is: 1
The value of j is: 2
Ans. The value of i is: 1
The value of j is: 0
Reason: If you are using the continue statement in labeled form then the statement continues at the next iteration of the labeled loop.

Q. What is the result when you compile and run the following code?

int i = 100;
switch (i)
{
case 100:
System.out.println(i);
case 200:
System.out.println(i);
case 300:
System.out.println(i);
}

[a] Nothing is printed
[b] Compile time error
[c] The values 100,100,100 printed
[d] Only 100 is printed

Ans. The values 100, 100, 100 are printed.

Q. What is the base class for Error and Exception?

Ans. Throwable.

Q. Throwable class resides in the package _______ ?

Ans. java.lang

Q. What class of exceptions are generated by the Java run-time system?

Ans. The Java run-time system generates RuntimeException and Error exceptions.

Q. Can a no argument constructor throw exception?

Ans. Yes, see the following code:
public class A
{
public A() throws Exception
{
throw new Exception("No argu constructor can throw
exception too");
}

public static void main(String args[])
{
try
{
new A();
}
catch (Exception e)
{
System.out.println(e);
}
}
}






Q. I have two exceptions thrown in the same try block,
when the first exeption is thrown, the second exception is
ignored. Why?

Ans. When an exception is thrown, the code cannot continue
its normal path. Therefore, he code following the exception line will not
be executed no matter what.

Q. Checked exception must be caught or declared, how about
unchecked ones?

Ans. Checked exceptions must be caught or declared in the method throws
statement. Unchecked Exceptions do not require the same.
However, if you do, it does not cause an error either. Something must be specified does not imply Something else must not be specified.

No comments:

Post a Comment