Java Programming Multiple Choice Questions - Exception Handling

This section focuses on the "Exception Handling" In Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.

1. Which statement is true?

A. catch(X x) can catch subclasses of X where X is a subclass of Exception.
B. Any statement that can throw an Exception must be enclosed in a try block.
C. The Error class is a RuntimeException.
D. Any statement that can throw an Error must be enclosed in a try block.

View Answer


2. Which statement is true?

A. An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method.
B. Multiple catch statements can catch the same class of exception more than once.
C. A try statement must have at least one corresponding catch block.
D. Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.

View Answer


3. When does Exceptions in Java arises in code sequence?

A. Run Time
B. Can Occur Any Time
C. Compilation Time
D. None of the mentioned

View Answer


4. Which of these keywords is not a part of exception handling?

A. finally
B. thrown
C. catch
D. try

View Answer


5. Which of these keywords must be used to monitor for exceptions?

A. finally
B. throw
C. catch
D. try

View Answer


6. Which of these keywords must be used to handle the exception thrown by try block in some rational manner?

A. finally
B. throw
C. catch
D. try

View Answer


7. Which of these keywords is used to manually throw an exception?

A. finally
B. throw
C. catch
D. try

View Answer


8. Which of these is a super class of all errors and exceptions in the Java language?

A. Catchable
B. Throwable
C. RunTimeExceptions
D. None of the above

View Answer


9. In which of the following package Exception class exist?

A. java.file
B. java.lang
C. java.io
D. java.util

View Answer


10. Which exception is thrown when divide by zero statement executes?

A. NumberFormatException
B. NullPointerException
C. ArithmeticException
D. None of these

View Answer


11. What is the output of this program?

class Main
{
        public static void main(String args[])
	{
		try
		{
			System.out.print("Hello" + " " + 1 / 0);
		}
		catch(ArithmeticException e)
		{
			System.out.print("World");
		}
	}
}

A. Hello
B. World
C. HelloWorld
D. Hello World

View Answer


12. What is the output of this program?

class Main
{
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

View Answer


13. What is the output of this program?

class Main
{
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

View Answer


14. What is the output of this program?

class Main
{
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. 5
C. Compilation Error
D. Runtime Error

View Answer


15. Predict the output of following Java program?

class Main
{
public static void main(String args[]) {
		try {
			throw 10;
		}
		catch(int e) {
			System.out.println("Got the Exception " + e);
		}
	}
}

A. Got the Exception 10
B. Got the Exception 0
C. Compiler Error
D. None of the above

View Answer


16. What will be output for the following code?

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 ");
      }
  }
}

A. Got the Test Exception
Inside finally block
B. Got the Test Exception
C. Compiler Error
D. Inside finally block

View Answer


17. Output of following Java program?

  
class Main {
	public static void main(String args[]) 
	{
		int x = 0;
		int y = 10;
		int z = y/x;
	}
}

A. Compiler Error
B. Compiles and runs fine
C. Compiles fine but throws ArithmeticException exception
D. None of the above

View Answer


18. What will be output for the following code?

class Base extends Exception {}
class Derived extends Base {}
public class Main {
	public static void main(String args[]) 
	{
		try 
		{
			throw new Derived();
		}
		catch(Base b) 
		{
			System.out.println("Caught base class exception");
		}
		catch(Derived d) 
		{
			System.out.println("Caught derived class exception");
		}
	}
}

A. Caught base class exception
B. Caught derived class exception
C. Compiler Error because derived is not throwable
D. Compiler Error because base class exception is caught before derived class

View Answer


19. What will be output for the following code?

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");
		}
	}
}

A. Compile error
B. Divide by zero error
C. Divide by zero error
inside the finally block
D. inside the finally block

View Answer


20. What is the output of this program?

class Main
{
	public static void main(String[] args)
	{
		try
		{
			return;
		}
		finally
		{
			System.out.println( "Finally" );
		}
	}
}

A. Finally
B. Compilation fails.
C. The code runs with no output.
D. An exception is thrown at runtime.

View Answer


21. Which of these is a super class of all exceptional type classes?

A. String
B. RuntimeExceptions
C. Throwable
D. Cacheable

View Answer


22. Which of these class is related to all the exceptions that can be caught by using catch?

A. Error
B. Exception
C. RuntimeExecption
D. All of the mentioned

View Answer


23. Which of these class is related to all the exceptions that cannot be caught?

A. Error
B. Exception
C. RuntimeExecption
D. All of the mentioned

View Answer


24. Which of these handles the exception when no catch is used?

A. Default handler
B. finally
C. throw handler
D. Java run time system

View Answer


25. What exception thrown by parseInt() method?

A. ArithmeticException
B. ClassNotFoundException
C. NullPointerException
D. NumberFormatException

View Answer


26. What is the output of this program?

class Main
{
public static void main(String args[])
  {
   try
   {
    System.out.print("Hello" + " " + 1 / 0);
   }
   finally
   {
    System.out.print("World");
   }
  }
}

A. Hello
B. World
C. Compilation Error
D. First Exception then World

View Answer


27. What is the output of this program?

class Main
{
public static void main(String args[])
  {
   try
   {
    int i, sum;
    sum = 10;
    for (i = -1; i < 3 ;++i)
    {
     sum = (sum / i);
     System.out.print(i);
    }
   }
   catch(ArithmeticException e)
   {
    System.out.print("0");
   }
  }
}

A. -1
B. 0
C. -10
D. -101

View Answer


28. Which of these operator is used to generate an instance of an exception than can be thrown by using throw?

A. new
B. malloc
C. alloc
D. thrown

View Answer


29. Which of these keywords is used to by the calling function to guard against the exception that is thrown by called function?

A. try
B. throw
C. throws
D. catch

View Answer


30. What is the output of this program?

class Main
{
public static void main(String args[])
    {
     try
     {       System.out.print("A");
      throw new NullPointerException ("Hello");
     }
     catch(ArithmeticException e)
     {
      System.out.print("B");
     }
    }
}

A. A
B. B
C. Hello
D. NullPointerException

View Answer


31. What is the output of this program?

class Main
{
public static void main(String[] args)
  {
   try
   {
    return;
   }
   finally
   {
    System.out.println( "Finally" );
   }
  }
}

A. Finally
B. Compilation fails
C. The code runs with no output
D. An exception is thrown at runtime

View Answer


32. A single try block must be followed by which of these?

A. finally
B. catch
C. finally & catch
D. none of the mentioned

View Answer


33. Which of these exceptions handles the divide by zero error?

A. ArithmeticException
B. MathException
C. IllegalAccessException
D. IllegarException

View Answer


34. Which of these exceptions will occur if we try to access the index of an array beyond its length?

A. ArithmeticException
B. ArrayException
C. ArrayIndexException
D. ArrayIndexOutOfBoundsException

View Answer


35. What is the output of this program?

class Main
{
public static void main(String args[])
  {
   try
   {
    int a = args.length;
    int b = 10 / a;
    System.out.print(a);
   }
   catch (ArithmeticException e)
   {
    System.out.println("1");
   }
  }
}

A. 0
B. 1
C. Compilation Error
D. Runtime Error

View Answer





Also check :

Discussion



* You must be logged in to add comment.