Questions On Exception Handling
Questions On Exception Handling
LARA TECHNOLOGIES
Questions on Exception Handling
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)
18)
WWW.LARATECHNOLOGY.COM
Question Paper
{
System.out.println(3);
System.out.println(ex.getMessage());
System.out.println(4);
}
System.out.println(5);
}
}
29) class D {
public static void main(String[] args) {
try
{
int i;
return ;
}
catch (Exception e)
{
System.out.println("inCatchBlock");
}
finally
{
System.out.println("inFinallyBlock");
}
}
}
30) class E {
public static void main(String[] args) throws
ClassNotFoundException
{
System.out.println(1);
if (true)
{
throw new ClassNotFoundException();
}
System.out.println(2);
}
}
31) public class F {
public static void main(String[] args) {
try
{
System.out.println(1);
int i=10/0;
System.out.println(2);
}
catch(NumberFormatException ex)
{
System.out.println(4);
System.out.println(ex.getMessage());
System.out.println(5);
}
finally
{
System.out.println(6);
}
System.out.println(5);
}
}
32) public class G {
public static void main(String[] args) {
try
{
System.out.println(1);
String s=null;
System.out.println(s);
System.out.println(s.length());
System.out.println(2);
}
080-41310124
Exception Handling(10-02-2015)
LARA TECHNOLOGIES
catch(NullPointerException ex)
{
System.out.println(4);
System.out.println(5);
}
System.out.println(6);
}
}
33) public class H {
public static void main(String[] args) {
try
{
System.out.println(1);
String s=null;
System.out.println(s);
System.out.println(s.length());
System.out.println(2);
}
catch(NullPointerException ex)
{
System.out.println(4);
System.out.println(s);
System.out.println(5);
}
System.out.println(6);
}
}
34) class I {
int test1() {
try
{
//some stmts
}
catch (ArithmeticException ex)
{
}
finally
{
}
return 10;
}
int test2() {
try
{
//some stmts
}
catch (ArithmeticException ex)
{
}
finally
{
return 30;
}
}
}
35) public class J {
public static void main(String a[]) {
try
{
for(int i=5;i>=0;i--){
System.out.println(10/i);
}
}
catch(Exception ex)
{
System.out.println("Exception Message:+ex.getMessage()");
ex.printStackTrace();
WWW.LARATECHNOLOGY.COM
Question Paper
}
System.out.println("After for loop...");
}
}
36) public class K {
public static void main(String[] a) {
try
{
int i = 10/0;
}
catch(Exception ex)
{
System.out.println("Inside 1st catch Block");
}
finally
{
System.out.println("Inside 1st finally block");
}
try
{
int i = 10/10;
}
catch(Exception ex)
{
System.out.println("Inside 2nd catch Block");
}
finally
{
System.out.println("Inside 2nd finally block");
}
}
}
37) class N {
public static void main(String[] args) {
String languages[] = { "C", "C++", "Java", ".Net", "C#" };
try
{
for (int i = 1; i <= 5; i++) {
System.out.println(languages[i]);
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
38) public class P {
static int test() {
try
{
return 10;
}
catch(NumberFormatException ex)
{
return 20;
}
finally
{
return 30;
}
return 40;
}
public static void main(String[] args) {
System.out.println(1);
System.out.println(test());
System.out.println(2);
080-41310124
Exception Handling(10-02-2015)
LARA TECHNOLOGIES
}
}
39) public class Q {
static int test() {
try
{
}
catch(NumberFormatException ex)
{
return 20;
}
finally
{
}
return 40;
}
public static void main(String[] args) {
System.out.println(1);
System.out.println(test());
System.out.println(2);
}
}
40) class R {
public static int test() {
try
{
return 0;
}
finally
{
System.out.println("Inside Finally block");
}
}
public static void main(String args[]) {
System.out.println(R.test());
}
}
41) class S {
public static void main(String[] args) {
try
{
long data[] = new long[1000000000];
}
catch (Exception e)
{
System.out.println(e);
}
finally
{
10.
System.out.println("finally block will execute always."); 11.
}
12.
}
13.
}
14.
42) public class X {
15.
public static void main(String [] args) {
16.
try
17.
{
18.
badMethod();
19.
System.out.print("A");
20.
}
catch (RuntimeException ex)
21.
{
22.
System.out.print("B");
23.
}
24.
catch (Exception ex1)
25.
{
26.
System.out.print("C");
27.
WWW.LARATECHNOLOGY.COM
Question Paper
}
finally
{
System.out.print("D");
}
System.out.print("E");
}
public static void badMethod()
{
throw new RuntimeException();
}
}
43) class Fex {
public static void main(String[] args) throws
ClassNotFoundException {
System.out.println(1);
if (true)
{
throw new ClassNotFoundException();
}
System.out.println(2);
}
}
44) public class T {
public static void main(String[] args) {
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
45) public class Z {
public static void main(String[] args) {
try
{
System.out.println(1);
int i=10/0;
}
catch(NullPointerException ex)
{
System.out.println(4);
try
{
int i=23/0;
}
catch(ArithmeticException ex)
{
System.out.println(5);
}
finally
{
System.out.println(6);
}
System.out.println(7);
}
}
}
46) public class K {
static int test()
{
try
{
}
catch(NumberFormatException ex)
080-41310124
Exception Handling(10-02-2015)
LARA TECHNOLOGIES
28.
29.
Question Paper
{
return 20;
}
finally
{
return 30;
}
return 40;
}
public static void main(String[] args) {
System.out.println(1);
System.out.println(test());
System.out.println(2);
}
}
47) public class F {
public static void main(String[] args) {
try
{
System.out.println(1);
String s=null;
System.out.println(s.length());
}
catch(NullPointerException ex)
{
System.out.println(4);
try
{
int i=23/0;
}
System.out.println(5);
}
finally
{
System.out.println(6);
}
System.out.println(7);
}
}
48) public class Test {
public static void main(String args[]) {
try
{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("task1 is completed");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("task 2 completed");
}
catch(Exception e)
WWW.LARATECHNOLOGY.COM
080-41310124