• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

Java Exception Handling

Question 1

Java
class Test extends Exception { }
 
class Main {
   public static void main(String args[]) { 
      try {
         throw new Test();
      }
      catch(Test t) {
         System.out.println(\"Got the Test Exception\");
      }
      finally {
         System.out.println(\"Inside finally block \");
      }
  }
}
  • Got the Test Exception
    Inside finally block 
  • Got the Test Exception
  • Inside finally block 
  • Compiler Error

Question 2

Output of following Java program? Java
class Main {
   public static void main(String args[]) {
      int x = 0;
      int y = 10;
      int z = y/x;
  }
}
  • Compiler Error
  • Compiles and runs fine
  • Compiles fine but throws ArithmeticException exception

Question 3

JAVA
class Test
{
    public static void main(String[] args)
    {
        try
        {
            int a[]= {1, 2, 3, 4};
            for (int i = 1; i <= 4; i++)
            {
                System.out.println (\"a[\" + i + \"]=\" + a[i] + \"\\n\");
            }
        }
        
        catch (Exception e)
        {
            System.out.println (\"error = \" + e);
        }
        
        catch (ArrayIndexOutOfBoundsException e)
        {
            System.out.println (\"ArrayIndexOutOfBoundsException\");
        }
    }
}
  • Compiler error
  • Run time error
  • ArrayIndexOutOfBoundsException
  • Error Code is printed
  • Array is printed

Question 4

JAVA
class Test
{
    public static void main (String[] args)
    {
        try
        {
            int a = 0;
            System.out.println (\"a = \" + a);
            int b = 20 / a;
            System.out.println (\"b = \" + b);
        }

        catch(ArithmeticException e)
        {
            System.out.println (\"Divide by zero error\");
        }

        finally
        {
            System.out.println (\"inside the finally block\");
        }
    }
}
  • Compile error
  • Divide by zero error
  • a = 0
    Divide by zero error
    inside the finally block
    
  • a = 0
  • inside the finally block

Question 5

Predict the output of the following program. Java
class Test
{
    String str = \"a\";

    void A()
    {
        try
        {
            str +=\"b\";
            B();
        }
        catch (Exception e)
        {
            str += \"c\";
        }
    }

    void B() throws Exception
    {
        try
        {
            str += \"d\";
            C();
        }
        catch(Exception e)
        {
            throw new Exception();
        }
        finally
        {
            str += \"e\";
        }

        str += \"f\";

    }
    
    void C() throws Exception
    {
        throw new Exception();
    }

    void display()
    {
        System.out.println(str);
    }

    public static void main(String[] args)
    {
        Test object = new Test();
        object.A();
        object.display();
    }

}
  • abdef
  • abdec
  • abdefc

Question 6

The built-in base class in Java, which is used to handle all exceptions is
  • Raise
  • Exception
  • Error
  • Throwable

Question 7

Which of these is a super class of all errors and exceptions in the Java language?
  • RunTimeExceptions
  • Throwable
  • Catchable
  • None of the above

Question 8

Predict the output of the following program. Java
class Test
{   int count = 0;

    void A() throws Exception
    {
        try
        {
            count++;
            
            try
            {
                count++;

                try
                {
                    count++;
                    throw new Exception();

                }
                
                catch(Exception ex)
                {
                    count++;
                    throw new Exception();
                }
            }
            
            catch(Exception ex)
            {
                count++;
            }
        }
        
        catch(Exception ex)
        {
            count++;
        }

    }

    void display()
    {
        System.out.println(count);
    }

    public static void main(String[] args) throws Exception
    {
        Test obj = new Test();
        obj.A();
        obj.display();
    }
}
  • 4
  • 5
  • 6
  • Compilation error

Question 9

Java
class Base extends Exception {}
class Derived extends Base  {}

public class Main {
  public static void main(String args[]) {
   // some other stuff
   try {
       // Some monitored code
       throw new Derived();
    }
    catch(Base b)     { 
       System.out.println(\"Caught base class exception\"); 
    }
    catch(Derived d)  { 
       System.out.println(\"Caught derived class exception\"); 
    }
  }
} 
  • Caught base class exception
  • Caught derived class exception
  • Compiler Error because derived is not throwable
  • Compiler Error because base class exception is caught before derived class

Question 10

Predict the output of following Java program Java
class Main {
   public static void main(String args[]) {
      try {
         throw 10;
      }
      catch(int e) {
         System.out.println(\"Got the  Exception \" + e);
      }
  }
}
  • Got the Exception 10
  • Got the Exception 0
  • Compiler Error

There are 10 questions to complete.

Last Updated :
Take a part in the ongoing discussion