Java MCQs on try and catch in Java Programming Language.
1. What is the use of try & catch? Answer: d 2. Which of these keywords are used for the block to be examined for exceptions? Answer: a 3. Which of these keywords are used for the block to handle the exceptions generated by try block? Answer: b 4. Which of these keywords are used for generating an exception manually? Answer: c 5. Which of these statements is incorrect? Answer: d 6. What will be the output of the following Java code? a) Hello 7. What will be the output of the following Java code? a) Hello 8. What will be the output of the following Java code? a) Hello 9. What will be the output of the following Java code? a) Hello 10. What will be the output of the following Java code? a) Hello
a) It allows us to manually handle the exception
b) It allows to fix errors
c) It prevents automatic terminating of the program in cases when an exception occurs
d) All of the mentioned
Clarification: None.
a) try
b) catch
c) throw
d) check
Clarification: try is used for the block that needs to checked for exception.
a) try
b) catch
c) throw
d) check
Clarification: None.
a) try
b) catch
c) throw
d) check
Clarification: None.
a) try block need not to be followed by catch block
b) try block can be followed by finally block instead of catch block
c) try can be followed by both catch and finally block
d) try need not to be followed by anything
Clarification: try must be followed by either catch or finally block.
class Output
{
public static void main(String args[])
{
try
{
int a = 0;
int b = 5;
int c = b / a;
System.out.print("Hello");
}
catch(Exception e)
{
System.out.print("World");
}
}
}
b) World
c) HelloWOrld
d) Compilation Error
Clarification: None.
Output:
$ javac Output.javac
java Output
World
class Output
{
public static void main(String args[])
{
try
{
int a = 0;
int b = 5;
int c = a / b;
System.out.print("Hello");
}
catch(Exception e)
{
System.out.print("World");
}
}
}
b) World
c) HelloWOrld
d) Compilation Error
Clarification: None.
Output:
$ javac Output.javac
java Output
Hello
class Output
{
public static void main(String args[])
{
try
{
int a = 0;
int b = 5;
int c = b / a;
System.out.print("Hello");
}
}
}
b) World
c) HelloWOrld
d) Compilation Error
Clarification: try must be followed by either catch or finally
Output:
$ javac Output.javac
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error, insert "Finally" to complete BlockStatements
class Output
{
public static void main(String args[])
{
try
{
int a = 0;
int b = 5;
int c = a / b;
System.out.print("Hello");
}
finally
{
System.out.print("World");
}
}
}
b) World
c) HelloWOrld
d) Compilation Error
Clarification: finally block is always executed after try block, no matter exception is found or not.
Output:
$ javac Output.javac
java Output
HelloWorld
class Output
{
public static void main(String args[])
{
try
{
int a = 0;
int b = 5;
int c = b / a;
System.out.print("Hello");
}
catch(Exception e)
{
System.out.print("World");
}
finally
{
System.out.print("World");
}
}
}
b) World
c) HelloWOrld
d) WorldWorld
Clarification: finally block is always executed after tryblock, no matter exception is found or not. catch block is executed only when exception is found. Here divide by zero exception is found hence both catch and finally are executed.
Output:
$ javac Output.javac
java Output
WorldWorld