Sail E0 Webinar

MCQs

Total Questions : 61 | Page 4 of 7 pages
Question 31.
public class MyProgram
{
public static void throwit()
{
throw new RuntimeException();
}
public static void main(String args[])
{
try
{
System.out.println("Hello world ");
throwit();
System.out.println("Done with try block ");
}
finally
{
System.out.println("Finally executing ");
}
}
}


which answer most closely indicates the behavior of the program?


  1.    The program will not compile.
  2.    The program will print Hello world, then will print that a RuntimeException has occurred, then will print Done with try block, and then will print Finally executing.
  3.    The program will print Hello world, then will print that a RuntimeException has occurred, and then will print Finally executing.
  4.    The program will print Hello world, then will print Finally executing, then will print that a RuntimeExceptionhas occurred.
 Discuss Question
Answer: Option D. -> The program will print Hello world, then will print Finally executing, then will print that a RuntimeExceptionhas occurred.

Once the program throws a RuntimeException (in the throwit() method) that is not caught, 

the finally block will be executed and the program will be terminated. If a method does not 

handle an exception, the finally block is executed before the exception is propagated.


Question 32.


What is the output of this program?


class exception_handling {
public static void main(String args[]) {
try {
int a = args.length;
int b = 10 / a;
System.out.print(a);
try {
if (a == 1)
a = a / a - a;
if (a == 2) {
int c = {1};
c[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e) {
System.out.println("TypeA");
}
catch (ArithmeticException e) {
System.out.println("TypeB");
}
}
}


Note: Execution command line: $ java exception_handling one two


  1.    TypeA
  2.    TypeB
  3.    Compilation Error
  4.    Runtime Error
 Discuss Question
Answer: Option C. -> Compilation Error

try without catch or finally
Output:
$ javac exception_handling.java
$ java exception_handling
Main.java:9: error: 'try' without 'catch', 'finally' or resource declarations



Question 33.


What will be the output of the program?


class Exc0 extends Exception { }
class Exc1 extends Exc0 { } /* Line 2 */
public class Test
{
public static void main(String args[])
{
try
{
throw new Exc1(); /* Line 9 */
}
catch (Exc0 e0) /* Line 11 */
{
System.out.println("Ex0 caught");
}
catch (Exception e)
{
System.out.println("exception caught");
}
}
}
  1.    Ex0 caught
  2.    exception caught
  3.    Compilation fails because of an error at line 2.
  4.    Compilation fails because of an error at line 9.
 Discuss Question
Answer: Option A. -> Ex0 caught

An exception Exc1 is thrown and is caught by the catch statement on line 11. The code is 

executed in this block. There is no finally block of code to execute.


Question 34.


What is the output of this program?


class exception_handling {
public static void main(String args[]) {
try {
int a[] = {1, 2,3 , 4, 5};
for (int i = 0; i < 5; ++i)
System.out.print(a[i]);
int x = 1/0;
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.print("A");
}
catch(ArithmeticException e) {
System.out.print("B");
}
}
}
  1.    12345
  2.    12345A
  3.    12345B
  4.    Comiplation Error
 Discuss Question
Answer: Option C. -> 12345B

There can be more than one catch for a single try block. Here Arithmetic exception

(/ by 0) occurs instead of Array index out of bound exception, so 2nd catch block is

executed.
Output:
$ javac exception_handling.java
$ java exception_handling
12345B


Question 35.


What is the output of this 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);
}
}
  1.    0
  2.    05
  3.    Compilation Error
  4.    Runtime Error
 Discuss Question
Answer: Option C. -> Compilation Error

Since sum is declared inside try block and we are trying to access it outside the try 

block it results in error.
Output:
$ javac exception_handling.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
sum cannot be resolved to a variable


Question 36.


What is the output of this program?


class exception_handling {
public static void main(String args[]) {
try {
int a[] = {1, 2,3 , 4, 5};
for (int i = 0; i < 7; ++i)
System.out.print(a[i]);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.print("0");
}
}
}
  1.    12345
  2.    123450
  3.    1234500
  4.    Compilation Error
 Discuss Question
Answer: Option B. -> 123450

When array index goes out of bound then ArrayIndexOutOfBoundsException exceptio 

is thrown by the system.
Output:
$ javac exception_handling.java
$ java exception_handling
123450


Question 37.


What is the output of this program?


class exception_handling
{
public static void main(String args[])
{
try
{
int a = 1;
int b = 10 / a;
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int c[] = {1};
c[8] = 9;
}
}
finally
{
System.out.print("A");
}
}
catch (Exception e)
{
System.out.println("B");
}
}
}
  1.    A
  2.    B
  3.    AB
  4.    BA
 Discuss Question
Answer: Option A. -> A

The inner try block does not have a catch which can tackle ArrayIndexOutOfBoundException 

hence finally is executed which prints 'A' the outer try block does have catch for ArrayIndexOut

OfBoundException exception but no such exception occurs in it 

hence its catch is never executed and only 'A' is printed.
Output:
$ javac exception_handling.java
$ java exception_handling
A



Question 38.


What will be the output of the program?


public class MyProgram
{
public static void main(String args[])
{
try
{
System.out.print("Hello world ");
}
finally
{
System.out.println("Finally executing ");
}
}
}
  1.    Nothing. The program will not compile because no exceptions are specified.
  2.    Nothing. The program will not compile because no catch clauses are specified.
  3.    Hello world.
  4.    Hello world Finally executing
 Discuss Question
Answer: Option D. -> Hello world Finally executing

Finally clauses are always executed. The program will first execute the try block, printing

Hello world, and will then execute the finally block, printing Finally executing.

Option A, B, and C are incorrect based on the program logic described above. Remember 

that either a catch or a finally statement must follow a try. Since the finally is present, the catch 

is not required.


Question 39.


What is the output of this program?


class exception_handling {
static void throwexception() throws ArithmeticException {
System.out.print("0");
throw new ArithmeticException ("Exception");
}
public static void main(String args[]) {
try {
throwexception();
}
catch (ArithmeticException e) {
System.out.println("A");
}
}
}
  1.    A
  2.    0
  3.    0A
  4.    Exception
 Discuss Question
Answer: Option C. -> 0A

None.
Output:
$ javac exception_handling.java
$ java exception_handling
0A

Question 40.


What will be the output of the program?


public class X
{
public static void main(String [] args)
{
try
{
badMethod(); /* Line 7 */
System.out.print("A");
}
catch (Exception ex) /* Line 10 */
{
System.out.print("B"); /* Line 12 */
}
finally /* Line 14 */
{
System.out.print("C"); /* Line 16 */
}
System.out.print("D"); /* Line 18 */
}
public static void badMethod()
{
throw new RuntimeException();
}
}
  1.    AB
  2.    BC
  3.    ABC
  4.    BCD
 Discuss Question
Answer: Option D. -> BCD

(1) A RuntimeException is thrown, this is a subclass of exception.

(2) The exception causes the try to complete abruptly (line 7) therefore line 8 is never executed.

(3) The exception is caught (line 10) and "B" is output (line 12)

(4) The finally block (line 14) is always executed and "C" is output (line 16).

(5) The exception was caught, so the program continues with line 18 and outputs "D".


Latest Videos

Latest Test Papers