Newgen Previous Years Solved Sample Placement Papers
- What will be the output of the following Java program?
class exception_handling { public static void main(String args[]) { try { int a, b; b = 0; a = 5 / b; System.out.print("A"); } catch(ArithmeticException e) { System.out.print("B"); } } }
a) A b) B c) Compilation Error d) Runtime Error Answer: b) B Explanation: A division by zero causes an ArithmeticException, which is caught by the catch block, printing "B". Output:$ javac exception_handling.java $ java exception_handling B
- What will be the output of the following Java program?
class exception_handling { public static void main(String args[]) { try { int a, b; b = 0; a = 5 / b; System.out.print("A"); } catch(ArithmeticException e) { System.out.print("B"); } finally { System.out.print("C"); } } }
a) A b) B c) AC d) BC Answer: d) BC Explanation: TheArithmeticException
is caught by the catch block, printing "B", and the finally block is executed, printing "C". Output:$ javac exception_handling.java $ java exception_handling BC
- What will be the output of the following Java program?
class exception_handling { public static void main(String args[]) { try { int i, sum; sum = 10; for (i = -1; i < 3 ;++i) sum = (sum / i); } catch(ArithmeticException e) { System.out.print("0"); } System.out.print(sum); } }
a) 0 b) 05 c) Compilation Error d) Runtime Error Answer: c) Compilation Error Explanation: The variablesum
is declared inside the try block, making it out of scope when accessed outside the try block, resulting in a compilation error. Output:$ javac exception_handling.java Exception in thread "main" java.lang.Error: Unresolved compilation problem: sum cannot be resolved to a variable