This section of our 1000+ Java MCQs focuses on try and catch in Java Programming Language.
1. What is the use of try & catch?
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
View Answer
Explanation: None.
2. Which of these keywords are used for the block to be examined for exceptions?
a) try
b) catch
c) throw
d) check
View Answer
Explanation: try is used for the block that needs to checked for exception.
3. Which of these keywords are used for the block to handle the exceptions generated by try block?
a) try
b) catch
c) throw
d) check
View Answer
Explanation: None.
4. Which of these keywords are used for generating an exception manually?
a) try
b) catch
c) throw
d) check
View Answer
Explanation: None.
5. Which of these statements is incorrect?
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
View Answer
Explanation: try must be followed by either catch or finally block.
6. What will be the output of the following Java code?
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");
}
}
}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
View Answer
Explanation: None.
Output:
$ javac Output.javac
java Output
World
7. What will be the output of the following Java code?
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");
}
}
}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
View Answer
Explanation: None.
Output:
$ javac Output.javac
java Output
Hello
8. What will be the output of the following Java code?
class Output
{
public static void main(String args[])
{
try
{
int a = 0;
int b = 5;
int c = b / a;
System.out.print("Hello");
}
}
}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
View Answer
Explanation: 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
9. What will be the output of the following Java code?
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");
}
}
}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error
View Answer
Explanation: finally block is always executed after try block, no matter exception is found or not.
Output:
$ javac Output.javac
java Output
HelloWorld
10. What will be the output of the following Java code?
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");
}
}
}
a) Hello
b) World
c) HelloWOrld
d) WorldWorld
View Answer
Explanation: 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
Sanfoundry Global Education & Learning Series Java Programming Language.
- Check Programming Books
- Apply for Computer Science Internship
- Apply for Java Internship
- Practice Programming MCQs
- Practice BCA MCQs