Question 1 of 25
Hints left: 3
Q1
What is the result of the following code snippet? try { int[] arr = new int[5]; int value = arr[10]; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Exception Caught!"); } finally { System.out.println("Finally Block!"); }
Q2
Which of the following blocks execute compulsorily whether exception is caught or not.
Q3
What will be the result after compiling this code? class SuperClass{ public int doIt(String str, Integer... data)throws Exception{ String signature = "(String, Integer[])"; System.out.println(str + " " + signature); return 1; } } public class Test extends SuperClass{ public int doIt(String str, Integer... data){ String signature = "(String, Integer[])"; System.out.println("Overridden: " + str + " " +signature); return 0; } public static void main(String... args){ SuperClass sb = new Test(); sb.doIt("hello", 3); } }
Q4
Which keyword is used to specify the exception thrown by method?
Q5
In Java, can a "catch" block catch exceptions of a subclass type before catching exceptions of a superclass type?
Q6
Given the following piece of code: class SalaryCalculationException extends Exception{} class Person{ public void calculateSalary() throws SalaryCalculationException{ //... throw new SalaryCalculationException(); //... } } class Company{ public void paySalaries(){ new Person().calculateSalary(); } } Which of the following statements is correct? 1. This code will compile without any problems. 2. This code will compile if in method paySalaries() we return a boolean in stead of void. 3. This code will compile if we add a try-catch block in paySalaries(). 4. This code will compile if we add throws SalaryCalculationException in the signature of method paySalaries().
Q7
Which exception is thrown when divide by zero statement executes?
Q8
What is the output for the below code? import java.io.FileNotFoundException; class A{ public void printName() throws FileNotFoundException{ System.out.println("Value-A"); } } class B extends A{ public void printName() throws NullPointerException{ System.out.println("Name-B"); } } public class Test{ public static void main (String[] args) throws Exception{ A a = new B(); a.printName(); } }
Q9
What will be the result of executing the following code? public class Test{ public void divide(int a, int b){ try{ int c = a / b; }catch(Exception e){ System.out.print("Exception "); }finally{ System.out.println("Finally"); } public static void main(String args[]){ Test t = new Test(); t.divide(0,3); } }
Q10
try{ File f = new File("a.txt"); }catch(Exception e){ }catch(IOException io){ } Is this code create new file name a.txt ?
Q11
In Java, can a "finally" block have a "return" statement?
Q12
What is the result of the following code snippet? try { throw new ArithmeticException(); } catch (Exception e) { System.out.println("Exception!"); } catch (ArithmeticException e) { System.out.println("Arithmetic Exception!"); }
Q13
public class Test{ public static void main(String args[]){ try{ int a = Integer.parseInt("four"); } } } Which exception could be handled by the catch block for above?
Q14
Which exception is thrown when an array element is accessed beyond the array size?
Q15
What will be the output of the following piece of code: class Person{ public void talk() {} } public class Test{ public static void main(String args[]){ Person p = null; try{ p.talk(); } catch(NullPointerException e){ System.out.print("There is a NullPointerException. "); } catch(Exception e){ System.out.print("There is an Exception. "); } System.out.print("Everything went fine. "); } }
Q16
What is the result of the following code snippet? try { throw new RuntimeException(); } catch (RuntimeException e) { System.out.println("Runtime Exception!"); } catch (Exception e) { System.out.println("Exception!"); }
Q17
In Java, can a "try" block have multiple "catch" blocks?
Q18
Determine output of the following program code? public class Test{ public static void main(String args[]){ int i; try{ i = calculate(); System.out.println(i); }catch(Exception e){ System.out.println("Error occured"); } } static int calculate(){ return (7/2); } }
Q19
What happen in case of multiple catch blocks?
Q20
What will be the output? class MyClass{ public String test(){ try{ System.out.print("One"); return ""; } finally{ System.out.print("Two"); } } } public class Test{ public static void main(String args[]){ MyClass m = new MyClass(); m.test(); } }
Q21
The class at the top of exception class hierarchy is .................
Q22
What is the output of the following program code? public class Test{ public static void main(String args[]){ try{ int i; return; } catch(Exception e){ System.out.print("inCatchBlock"); } finally{ System.out.println("inFinallyBlock"); } } }
Q23
In which of the following package Exception class exist?
Q24
Exception generated in try block is caught in ........... block.
Q25
Which keyword is used to explicitly throw an exception?