1. What is the output of this program?
class MyException extends Exception
{
MyException()
{
super("Exception Occured");
}
public String toString()
{
return "Problem";
}
}
class Output
{
public static void main(String args[])
{
try {
throw new MyException();
}
catch(MyException e)
{
System.out.print(e);
}
}
}
A) Exception Occured
B) MyException: Exception Occured
C) Problem
D) None of These
2. Identify the incorrect statement among the following.
A) throw is used to explicitly throw a user defined exception.
B) throws is used to specify that the method can throw exception
c) Throwable is an abstract class which is parent of Exception.
D) None of the above.
3. What will be the Output of the following Program?
public class Test
{
public static void main(String[] args)
{
int[] array = new int[2];
for (int i=0; i<2; i++)
{
try{
array[i] = i;
System.out.println(i);
}
catch(NullPointerException e)
{
System.out.println("NullPointer Exception");
}
finally
{
System.out.println("Finally Executed");
array = null;
}
}
}
}
A) 0
Finally Executed
NullPointer Exception
Finally Executed
B) 0
Finally Executed
1
Fially Executed
C) 0
Finally Executed
D) None of These
4. What will be the Output of the following Program?
class exception_handling {
public static void main(String args[]) {
try { throw new Error(); }
catch(Throwable e) {
System.out.print("Hello");}
catch(Exception e) {
System.out.print("Hi");}
}
}
A) Hello
B) Hi
C) Compilation Error
D) None of These
5. What will be the Output of the following Program?
class exception_handling
{
public static void main(String args[])
{
try {
throw new Error();
}
catch(Throwable e)
{
System.out.print("Throwable");
}
catch(Exception e)
{
System.out.print("Exception");
}
}
}
A) Throwable
B) Exception
C) Compilation Error
D) None of These
6. What happens in a method if an exception is thrown in a try{} and there is NO MATCHING catch block?
A) Program will not Compile.
B) The program halts immediately.
C) The program ignores that exception.
D) The method passes the exception to its caller.
7. Which statement is FALSE about catch block?
A) There must be only one catch block in a try/catch structure.
B) The catch block for a child exception class must FOLLOW that of a parent exception class.
C) If there is no catch block, there must be a finally{}.
D) Both A & B
8. What will be the Output of following Program?
class Myexception extends Exception
{
int detail;
Myexception(int a) { detail = a; }
public String toString() { return "detail";}
}
class Output {
static void compute (int a) throws Myexception
{ throw new Myexception(a); }
public static void main(String args[]) {
try { compute(3); }
catch(Myexception e) {
System.out.print("Exception " + e);}
}
}
A) Exception detail
B) Exception 3
C) 3
D) Compile time Error
9. When a method can throw an exception then it is specified by _____ keyword.
A) throws
B) throw
C) finally
D) catch
10. What change(s) should be made to the program to get the output like below?
Output: 0
1
class ExceptionHandling{
public static void main(String[] args){
int[] intarray = new int[2];
int i=0;
try{
for ( ; i<2; i++){
intarray[i] = i; //Statement 1
System.out.println(i);
}
}
finally{
intarray = null;
System.out.println(i); //Statement 2
}
}
}
A) Removing just finally block.
B) Removing just Statement 1.
C) Removing just Statement 2.
D) Removing finally block and Statement 1.
11. Which exception is thrown When an array element is accessed beyond the array size?A) ArrayElementsOutOfLimitException
B) ArrayIndexOutOfBoundsException
C) ArrayOutOfBoundsException
D) ArrayElementOutOfBoundsException
12. Which statement is FALSE about try{} block?
A) Some of the statements in a try{} will never throw an exception.
B) The statements in a try{} may throw several types of exception.
C) The statements in a try{} can not include a loop.
D) The try{} can be followed by only a finally block.
13. How many finally{} may be there in a try/catch structure?
A) There must be one, following the last catch block.
B) There can be zero or one, following the last catch block.
C) There can be maximum one, following every catch block.
D) There can be any number of catch blocks after each catch block.
14. Which statement is FALSE about catch block?
A) There must be only one catch block in a try/catch structure.
B) The catch block for a child exception class must FOLLOW that of a parent exception class.
C) The catch block for a child exception class must PRECEED that of a parent exception class.
D) Both A & B
15. RuntimeException is Parent of _______________.
A) InterruptedException
B) NumberFormatException
C) IOException
D) Exception
class MyException extends Exception
{
MyException()
{
super("Exception Occured");
}
public String toString()
{
return "Problem";
}
}
class Output
{
public static void main(String args[])
{
try {
throw new MyException();
}
catch(MyException e)
{
System.out.print(e);
}
}
}
A) Exception Occured
B) MyException: Exception Occured
C) Problem
D) None of These
2. Identify the incorrect statement among the following.
A) throw is used to explicitly throw a user defined exception.
B) throws is used to specify that the method can throw exception
c) Throwable is an abstract class which is parent of Exception.
D) None of the above.
3. What will be the Output of the following Program?
public class Test
{
public static void main(String[] args)
{
int[] array = new int[2];
for (int i=0; i<2; i++)
{
try{
array[i] = i;
System.out.println(i);
}
catch(NullPointerException e)
{
System.out.println("NullPointer Exception");
}
finally
{
System.out.println("Finally Executed");
array = null;
}
}
}
}
A) 0
Finally Executed
NullPointer Exception
Finally Executed
B) 0
Finally Executed
1
Fially Executed
C) 0
Finally Executed
D) None of These
4. What will be the Output of the following Program?
class exception_handling {
public static void main(String args[]) {
try { throw new Error(); }
catch(Throwable e) {
System.out.print("Hello");}
catch(Exception e) {
System.out.print("Hi");}
}
}
A) Hello
B) Hi
C) Compilation Error
D) None of These
5. What will be the Output of the following Program?
class exception_handling
{
public static void main(String args[])
{
try {
throw new Error();
}
catch(Throwable e)
{
System.out.print("Throwable");
}
catch(Exception e)
{
System.out.print("Exception");
}
}
}
A) Throwable
B) Exception
C) Compilation Error
D) None of These
6. What happens in a method if an exception is thrown in a try{} and there is NO MATCHING catch block?
A) Program will not Compile.
B) The program halts immediately.
C) The program ignores that exception.
D) The method passes the exception to its caller.
7. Which statement is FALSE about catch block?
A) There must be only one catch block in a try/catch structure.
B) The catch block for a child exception class must FOLLOW that of a parent exception class.
C) If there is no catch block, there must be a finally{}.
D) Both A & B
8. What will be the Output of following Program?
class Myexception extends Exception
{
int detail;
Myexception(int a) { detail = a; }
public String toString() { return "detail";}
}
class Output {
static void compute (int a) throws Myexception
{ throw new Myexception(a); }
public static void main(String args[]) {
try { compute(3); }
catch(Myexception e) {
System.out.print("Exception " + e);}
}
}
A) Exception detail
B) Exception 3
C) 3
D) Compile time Error
9. When a method can throw an exception then it is specified by _____ keyword.
A) throws
B) throw
C) finally
D) catch
10. What change(s) should be made to the program to get the output like below?
Output: 0
1
class ExceptionHandling{
public static void main(String[] args){
int[] intarray = new int[2];
int i=0;
try{
for ( ; i<2; i++){
intarray[i] = i; //Statement 1
System.out.println(i);
}
}
finally{
intarray = null;
System.out.println(i); //Statement 2
}
}
}
A) Removing just finally block.
B) Removing just Statement 1.
C) Removing just Statement 2.
D) Removing finally block and Statement 1.
11. Which exception is thrown When an array element is accessed beyond the array size?A) ArrayElementsOutOfLimitException
B) ArrayIndexOutOfBoundsException
C) ArrayOutOfBoundsException
D) ArrayElementOutOfBoundsException
12. Which statement is FALSE about try{} block?
A) Some of the statements in a try{} will never throw an exception.
B) The statements in a try{} may throw several types of exception.
C) The statements in a try{} can not include a loop.
D) The try{} can be followed by only a finally block.
13. How many finally{} may be there in a try/catch structure?
A) There must be one, following the last catch block.
B) There can be zero or one, following the last catch block.
C) There can be maximum one, following every catch block.
D) There can be any number of catch blocks after each catch block.
14. Which statement is FALSE about catch block?
A) There must be only one catch block in a try/catch structure.
B) The catch block for a child exception class must FOLLOW that of a parent exception class.
C) The catch block for a child exception class must PRECEED that of a parent exception class.
D) Both A & B
15. RuntimeException is Parent of _______________.
A) InterruptedException
B) NumberFormatException
C) IOException
D) Exception
Good Post! Thank you so much for sharing the post, it was so good to read and useful to improve
ReplyDeleteJava Training in Electronic City