Output of Java program | Set 12(Exception Handling)
Last Updated :
06 Feb, 2019
Prerequisites : Exception handling ,
control flow in try-catch-finally
1) What is the output of the following program?
Java
public class Test
{
public static void main(String[] args)
{
try
{
System.out.printf("1");
int sum = 9 / 0;
System.out.printf("2");
}
catch(ArithmeticException e)
{
System.out.printf("3");
}
catch(Exception e)
{
System.out.printf("4");
}
finally
{
System.out.printf("5");
}
}
}
a) 1325
b) 1345
c) 1342
d) 135
Ans. (d)
Explanation: Once an exception occurs in try block, the execution passes to
corresponding catch statement and doesn't return back to try block. Only one of the catch blocks are executed at a time. finally block is always executed whether or not the exception occurred.
2) What is the output of the following program?
Java
public class Test
{
private void m1()
{
m2();
System.out.printf("1");
}
private void m2()
{
m3();
System.out.printf("2");
}
private void m3()
{
System.out.printf("3");
try
{
int sum = 4/0;
System.out.printf("4");
}
catch(ArithmeticException e)
{
System.out.printf("5");
}
System.out.printf("7");
}
public static void main(String[] args)
{
Test obj = new Test();
obj.m1();
}
}
a) 35721
b) 354721
c) 3521
d) 35
Ans. (a)
Explanation: If an exception is handled in the catch statement, the program continues with its normal execution, after executing the catch statement corresponding to that exception. Also, when an exception occurs in the try block, the rest of the program in the try block is not executed.
3) What is the output of the following program?
Java
public class Test
{
public static void main(String[] args)
{
try
{
System.out.printf("1");
int data = 5 / 0;
}
catch(ArithmeticException e)
{
System.out.printf("2");
System.exit(0);
}
finally
{
System.out.printf("3");
}
System.out.printf("4");
}
}
a) 12
b) 1234
c) 124
d) 123
Ans. (a)
Explanation: The only case when the code inside finally block is not executed is when System.exit(0) is called explicitly in the program. Then exit statement is called and the program is terminated without executing any further.
4) What is the output of the following program?
Java
public class Test
{
public static void main(String[] args)
{
try
{
System.out.printf("1");
int data = 5 / 0;
}
catch(ArithmeticException e)
{
Throwable obj = new Throwable("Sample");
try
{
throw obj;
}
catch (Throwable e1)
{
System.out.printf("8");
}
}
finally
{
System.out.printf("3");
}
System.out.printf("4");
}
}
a) Compilation error
b) Runtime error
c) 1834
d) 134
Ans. (c)
Explanation: Exceptions can be thrown in catch clause. This is done in order to change the exception type at run time. Exceptions in catch clause are thrown by creating instances of class Throwable as shown in the program.
5) What is the output of the following program?
Java
import java.io.EOFException;
import java.io.IOException;
public class Test
{
public static void main(String[] args)
{
try
{
System.out.printf("1");
int value = 10 / 0;
throw new IOException();
}
catch(EOFException e)
{
System.out.printf("2");
}
catch(ArithmeticException e)
{
System.out.printf("3");
}
catch(NullPointerException e)
{
System.out.printf("4");
}
catch(IOException e)
{
System.out.printf("5");
}
catch(Exception e)
{
System.out.printf("6");
}
}
}
a) 1346
b) 136726
c) 136
d) 13
Ans. (d)
Explanation: In multi-catch statements, the exceptions must be listed from more specific to more general. Only one catch statement which is most specific to the occurred exception is executed.
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read