Java MCQs on exception handling of Java Programming Language.
1. When does Exceptions in Java arises in code sequence? Answer: a 2. Which of these keywords is not a part of exception handling? Answer: c 3. Which of these keywords must be used to monitor for exceptions? Answer: a 4. Which of these keywords must be used to handle the exception thrown by try block in some rational manner? Answer: d 5. Which of these keywords is used to manually throw an exception? Answer: c 6. What will be the output of the following Java program? a) Hello 7. What will be the output of the following Java program? a) A 8. What will be the output of the following Java program? a) A 9. What will be the output of the following Java program? a) 0
a) Run Time
b) Compilation Time
c) Can Occur Any Time
d) None of the mentioned
Clarification: Exceptions in Java are run-time errors.
a) try
b) finally
c) thrown
d) catch
Clarification: Exceptional handling is managed via 5 keywords – try, catch, throws, throw and finally.
a) try
b) finally
c) throw
d) catch
Clarification: None.
a) try
b) finally
c) throw
d) catch
Clarification: If an exception occurs within the try block, it is thrown and cached by catch block for processing.
a) try
b) finally
c) throw
d) catch
Clarification: None.
class exception_handling
{
public static void main(String args[])
{
try
{
System.out.print("Hello" + " " + 1 / 0);
}
catch(ArithmeticException e)
{
System.out.print("World");
}
}
}
b) World
c) HelloWorld
d) Hello World
Clarification: System.ou.print() function first converts the whole parameters into a string and then prints, before “Hello” goes to output stream 1 / 0 error is encountered which is cached by catch block printing just “World”.
Output:
$ javac exception_handling.java
$ java exception_handling
World
class exception_handling
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}
b) B
c) Compilation Error
d) Runtime Error
Clarification: None.
Output:
$ javac exception_handling.java
$ java exception_handling
B
class exception_handling
{
public static void main(String args[])
{
try
{
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
}
}
b) B
c) AC
d) BC
Clarification: finally keyword is used to execute the code before try and catch block end.
Output:
$ javac exception_handling.java
$ java exception_handling
BC
class exception_handling
{
public static void main(String args[])
{
try
{
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
sum = (sum / i);
}
catch(ArithmeticException e)
{
System.out.print("0");
}
System.out.print(sum);
}
}
b) 05
c) Compilation Error
d) Runtime Error
Clarification: Value of variable sum is printed outside of try block, sum is declared only in try block, outside try block it is undefined.
Output:
$ javac exception_handling.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
sum cannot be resolved to a variable
