Prerequisite : Exception Handling, Throws
1. What will be the output of the following program?
class Geeks {
public
static void main(String[] args)
{
throw new ArithmeticException();
}
}
|
Options:
1. RuntineException:java.lang.ArithmeticExcetion
2. RuntineException:/ by zero
3. RuntineException:java.lang.ArithmeticExcetion:/ by zero
4. RuntineException:ArithmeticExcetion
The answer is option (1)
Explanation: In the above program, we are throwing an exception object explicitly to the JVM and the default handler print the description of the exception object, without any description of the object because here we are calling default constructor.
2. What will be the output of the following program?
class Geeks {
static ArithmeticException ae = new ArithmeticException();
public
static void main(String[] args)
{
throw ae;
}
}
|
Options:
1. RuntineException:java.lang.ArithmeticExcetion
2. RuntineException:/ by zero
3. RuntineException:java.lang.ArithmeticExcetion:/ by zero
4. RuntineException:ArithmeticExcetion
The answer is option (1)
Explanation: In the above program, we are throwing an exception object explicitly to the JVM and the default handler print the description of the exception object.
3. What will be the output of the following program?
class Geeks {
static ArithmeticException ae;
public
static void main(String[] args)
{
throw ae;
}
}
|
Options:
1. RuntineException:java.lang.ArithmeticExcetion
2. RuntineException:NullPointerException
3. No Output
4. RuntineException:ArithmeticExcetion
The answer is option (2)
Explanation: Here ae refers to null because static variable is initialized by the compiler by giving default value and the value of reference is null. Thats why we will get RuntimeException saying Exception in thread “main” java.lang.NullPointerException
4. What will be the output of the following program?
class Geeks {
public
static void main(String[] args)
{
throw new ArithmeticException("/ by zero");
System.out.println("Hello Geeks");
}
}
|
Options:
1. Run-time Exception
2. Compile-time error
3. No Output
4. Compile-time Exception
The answer is option (2)
Explanation: In the above program, we are throwing an exception object explicitly to the JVM but after throwing an exception object explicitly we cant declare any statement directly because that statement will not get the chance to execute. Thats why we will get compile time error saying error: unreachable statement
5. What will be the output of the following program?
class Geeks {
public
static void main(String[] args)
{
throw new Geeks();
System.out.println("Hello Geeks");
}
}
|
Options:
1. Hello Geeks
2. No Output
3. Run-time Exception
4. Compile-time error
The answer is option (4)
Explanation: we can use throw keyword only for throwable object types. If we are trying to use for normal java objects, we will get compile time error saying incompatible types.
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.