Your SlideShare is downloading. ×
Exception handling
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Exception handling

947
views

Published on

Java Exception Handling, Technical interview questions, SCJP Dumbs.

Java Exception Handling, Technical interview questions, SCJP Dumbs.

Published in: Education, Technology, Business

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
947
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
71
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Exception Handling Duration 30 min.
  • 2. Q1What will the following code print when run?public class Test{ static String j = ""; public static void method( int i) { try { if(i == 2) { throw new Exception(); } j += "1"; } catch (Exception e) { j += "2"; return; } finally { j += "3"; } j += "4"; } public static void main(String args[]) { method(1); method(2); System.out.println(j); }}Select 1 correct option.a 13432b 13423c 14324d 12434e 12342
  • 3. Q2Which digits, and in which order, will be printed when the following program is run?public class TestClass{ public static void main(String args[]) { int k = 0; try{ int i = 5/k; } catch (ArithmeticException e){ System.out.println("1"); } catch (RuntimeException e){ System.out.println("2"); return ; } catch (Exception e){ System.out.println("3"); } finally{ System.out.println("4"); } System.out.println("5"); }}Select 1 correct option.a The program will print 5.b The program will print 1 and 4, in that order.c The program will print 1, 2 and 4, in that order.d The program will print 1, 4 and 5, in that order.e The program will print 1,2, 4 and 5, in that order.
  • 4. Q3What class of objects can be declared by the throws clause?Select 3 correct optionsa Exceptionb Errorc Eventd Objecte RuntimeException
  • 5. Q4public class TestClass{ class MyException extends Exception {} public void myMethod() throws XXXX { throw new MyException(); }}What should replace XXXX?Select 3 correct optionsa MyExceptionb Exceptionc No throws clause is necessaryd Throwablee RuntimeException
  • 6. Q5What will the following code print when run?public class Test{ static String s = ""; public static void m0(int a, int b) { s +=a; m2(); m1(b); } public static void m1(int i) { s += i; } public static void m2() { throw new NullPointerException("aa"); } public static void m() { m0(1, 2); m1(3); } public static void main(String args[]) { try { m(); } catch(Exception e){ } System.out.println(s); }}a 1b 12c 123d 2e It will throw exception at runtime.
  • 7. Q6What will be the output of the following class...class Test{ public static void main(String[] args) { int j = 1; try { int i = doIt() / (j = 2); } catch (Exception e) { System.out.println(" j = " + j); } } public static int doIt() throws Exception { throw newException("FORGET IT"); }}Select 1 correct option.a It will print j = 1;b It will print j = 2;c The value of j cannot be determined.d It will not compile.e None of the above.
  • 8. Q7What will be the result of compiling and running the following program ?class NewException extends Exception {}class AnotherException extends Exception {}public class ExceptionTest{ public static void main(String[] args) throws Exception { try { m2(); } finally { m3(); } catch (NewException e){} } public static void m2() throws NewException { throw new NewException(); } public static void m3() throws AnotherException{ throw new AnotherException(); }}Select 1 correct option.a It will compile but will throw AnotherException when run.b It will compile but will throw NewException when run.c It will compile and run without throwing any exceptions.d It will not compile.e None of the above.
  • 9. Q8class NewException extends Exception {}class AnotherException extends Exception {}public class ExcceptionTest{ public static void main(String [] args) throws Exception { try { m2(); } finally{ m3(); } } public static void m2() throws NewException{ throw new NewException(); } public static void m3() throws AnotherException{ throw newAnotherException(); }}Select 1 correct option.a It will compile but will throw AnotherException when run.b It will compile but will throw NewException when run.c It will compile and run without throwing any exceptions.d It will not compile.e None of the above.
  • 10. Q9What will be the result of attempting to compile and run the following program?public class TestClass{ public static void main(String args[]) { try { ArithmeticException re=null; throw re; } catch(Exception e) { System.out.println(e); } }}Select 1 Correct optiona. The code will fail to compile, since RuntimeException cannot be caught by catching anException.b. The program will fail to compile, since re is null.c. The program will compile without error and will print java.lang.RuntimeException when run.d. The program will compile without error and will print java.lang.NullPointerException whenrun.e. The program will compile without error and will run and print null.
  • 11. Q10Given the following program, which of these statements are true?public class FinallyTest{ public static void main(String args[]) { try { if (args.length == 0) return; else throw new Exception("Some Exception"); } catch(Exception e) { System.out.println("Exception in Main"); } finally { System.out.println("The end"); } }}Select 2 correct optionsa If run with no arguments, the program will only print The end.b If run with one argument, the program will only print The end.c If run with one argument, the program will print Exception in Main and The end.d If run with one argument, the program will only print Exception in Main.e Only one of the above is correct.
  • 12. Q11class E1 extends Exception{ }class E2 extends E1 { }class Test{ public static void main(String[] args) { try{ throw new E2(); } catch(E1 e){ System.out.println("E1"); } catch(Exception e){ System.out.println("E"); } finally{ System.out.println("Finally"); } }}Select 1 correct option.a It will not compile.b It will print E1 and Finally.c It will print E1, E and Finally.d It will print E and Finally.e It will print Finally.
  • 13. Q12What will be the output of the following program?class TestClass{ public static void main(String[] args) throws Exception { try{ amethod(); System.out.println("try"); } catch(Exception e){ System.out.println("catch"); } finally { System.out.println("finally"); } System.out.println("out"); } public static void amethod(){ }}Select 1 correct option.a try finallyb try finally outc try outd catch finally oute It will not compile because amethod() does not throw any exception.
  • 14. Q13class MyException extends Throwable{}class MyException1 extends MyException{}class MyException2 extends MyException{}class MyException3 extends MyException2{}public class ExceptionTest{ void myMethod() throws MyException { throw new MyException3(); } public static void main(String[] args) { ExceptionTest et = new ExceptionTest(); try { et.myMethod(); } catch(MyException me) { System.out.println("MyException thrown"); } catch(MyException3 me3) { System.out.println("MyException3 thrown"); } finally { System.out.println(" Done"); } }}Select 1 correct option.a MyException thrownb MyException3 thrownc MyException thrown Doned MyException3 thrown Donee It fails to compile
  • 15. Q14What letters, and in what order, will be printed when the following programis compiled and run?public class FinallyTest{ public static void main(String args[]) throws Exception { try { m1(); System.out.println("A"); } finally { System.out.println("B"); } System.out.println("C"); } public static void m1() throws Exception { throw new Exception(); }}Select 1 correct option.a It will print C and B, in that order.b It will print A and B, in that order.c It will print B and throw Exception.d It will print A, B and C in that order.e Compile time error.
  • 16. Q15What is wrong with the following code written in a single file named TestClass.java?class SomeThrowable extends Throwable { }class MyThrowable extends SomeThrowable { }public class TestClass{ public static void main(String args[]) throws SomeThrowable { try{ m1(); }catch(SomeThrowable e){ throw e; }finally{ System.out.println("Done"); } } public static void m1() throws MyThrowable { throw new MyThrowable(); }}Select 2 correct optionsa The main declares that it throws SomeThrowable but throws MyThrowable.b You cannot have more than 2 classes in one file.c The catch block in the main method must declare that it catches MyThrowable rather thanSomeThrowabled There is nothing wrong with the code.e Done will be printed.