Exceptions
- What is the output of this program?
public class Array_Exception
{
public static void main(String args[])
{
try
{
int p = args.length;
int q = 12 / p;
System.out.print(p);
try
{
if (p == 1)
p = p / p - p;
if (p == 2)
{
int []s = {1};
s[10] = 8;
}
}
catch (ArrayIndexOutOfBoundException e)
{
System.out.println("First");
}
}
catch (ArithmeticException e)
{
System.out.println("Second");
}
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
Because we can’t go beyond array limit
- What is the output of this program?
public class Null_Pointer_Exception
{
public static void main(String args[])
{
try
{
System.out.print("First");
throw new NullPointerException ("Interview Mania");
}
catch(ArithmeticException e)
{
System.out.print("Second");
}
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
FirstException in thread "main" java.lang.NullPointerException: Interview Mania
at Null_Pointer_Exception.main(Null_Pointer_Exception.java:8)
- What is the output of this program?
public class Finally_Example
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally block executed..." );
}
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Output: Finally block executed...
Because finally will execute always.
- What is the output of this program?
public class Without_Catch
{
public static void main(String args[])
{
try
{
System.out.print("Interview Mania ");
}
finally
{
System.out.println("Finally executed.. ");
}
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
Interview Mania Finally executed..
- Which of these clause will be executed even if no exceptions are found?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
finally keyword is used to define a set of instructions that will be executed irrespective of the exception found or not.