504). Which of these exceptions handles the situations when an illegal argument is used to invoke a method?
[A]IllegalException
[B] Argument Exception
[C]IllegalArgumentException
[D]IllegalMethodArgumentExcepetion
Show Answer
Correct Answer: IllegalArgumentException
Notes:
Answer: c
Explanation: None.
505). Which of these exceptions will be thrown if we declare an array with negative size?
[A]IllegalArrayException
[B]IllegalArraySizeExeption
[C]NegativeArrayException
[D]NegativeArraySizeExeption
Show Answer
Correct Answer: NegativeArraySizeExeption
Notes:
Answer: d
Explanation: Array size must always be positive if we declare an array with negative size then built in exception “NegativeArraySizeException” is thrown by the java’s run time system.
506). Which of these packages contain all the Java’s built in exceptions?
[A] java.io
[B]java.util
[C]java.lang
[D]java.net
Show Answer
Correct Answer: java.lang
Notes:
Answer: c
Explanation: None.
507). Which of these exceptions will be thrown if we use null reference for an arithmetic operation?
[A]ArithmeticException
[B]NullPointerException
[C]IllegalAccessException
[D]IllegalOperationException
Show Answer
Correct Answer: NullPointerException
Notes:
Answer: b
Explanation: If we use null reference anywhere in the code where the value stored in that reference is used then NullPointerException occurs.
508). Which of these class is used to create user defined exception?
[A]java.lang
[B]Exception
[C]RunTime
[D]System
Show Answer
Correct Answer: Exception
Notes:
Answer: b
Explanation: Exception class contains all the methods necessary for defining an exception. The class contains the Throwable class.
509). What will be the output of the following Java program?
class exception_handling
{
public static void main(String args[])
{
try
{
int a[] = {1, 2,3 , 4, 5};
for (int i = 0; i < 7; ++i)
System.out.print(a[i]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("0");
}
}
}
[A]12345
[B]123450
[C]1234500
[D]Compilation Error
Show Answer
Correct Answer: 123450
Notes:
Answer: b
Explanation: When array index goes out of bound then ArrayIndexOutOfBoundsException exception is thrown by the system.
Output:$ javac exception_handling.java
$ java exception_handling
123450
510). What will be the output of the following Java program?
class exception_handling
{
public static void main(String args[])
{
try
{
int a[] = {1, 2,3 , 4, 5};
for (int i = 0; i < 5; ++i)
System.out.print(a[i]);
int x = 1/0;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}
[A]12345
[B]12345A
[C]12345B
[D] Compilation Error
Show Answer
Correct Answer: 12345B
Notes:
Answer: c
Explanation: There can be more than one catch of a single try block. Here Arithmetic exception occurs instead of Array index out of bound exception hence B is printed after 12345
Output:
$ javac exception_handling.java
$ java exception_handling
12345B
511). What will be the output of the following Java program?
class exception_handling
{
static void throwexception() throws ArithmeticException
{
System.out.print("0");
throw new ArithmeticException ("Exception");
}
public static void main(String args[])
{
try
{
throwexception();
}
catch (ArithmeticException e)
{
System.out.println("A");
}
}
}
[A]A
[B]0
[C]0A
[D]Exception
Show Answer
Correct Answer: 0A
Notes:
Answer: c
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
0A
512). What will be the output of the following Java program?
class exception_handling
{
public static void main(String args[])
{
try
{
int a = 1;
int b = 10 / a;
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int c[] = {1};
c[8] = 9;
}
finally
{
System.out.print("A");
}
}
catch (NullPointerException e)
{
System.out.println("B");
}
}
}
Show Answer
Correct Answer: A
Notes:
Answer: a
Explanation: The inner try block does not have a catch which can tackle ArrayIndexOutOfBoundException hence finally is executed which prints ‘A’ the outer try block does have catch for NullPointerException exception but no such exception occurs in it hence its catch is never executed and only ‘A’ is printed.
Output:
$ javac exception_handling.java
$ java exception_handling
A
513). What will be the output of the following Java program?Note: Execution command line: $ java exception_handling one two
class exception_handling
{
public static void main(String args[])
{
try
{
int a = args.length;
int b = 10 / a;
System.out.print(a);
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int c = {1};
c[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e)
{
System.out.println("TypeA");
}
catch (ArithmeticException e)
{
System.out.println("TypeB");
}
}
}
[A]TypeA
[B]TypeB
[C]0TypeA
[D]0TypeB
Show Answer
Correct Answer: 0TypeB
Notes:
Answer: d
Explanation: Execution command line is “$ java exception_ handling one two” hence there are two input making args.length = 2, hence “c[8] = 9” in second try block is executing which throws ArrayIndexOutOfBoundException which is caught by catch of nested try block. Hence 0TypeB is printed.
Output:
$ javac exception_handling.java
$ java exception_handling
0TypeB