Java-MCQs on exception Handling

Exception in Java is some abnormal situation raised during run-time of a java code. The program execution got disturbed and got terminated.

we have to follow two rules here; handle or declare.

Either write the code inside try and catch or declare it along with method

eg:

1.public void add(int a,int b)throws Exception

2.public void add(int a,int b){

try{

//code that may cause exception-problem area

}catch(Exception e){

//exception handling code here-solution area

}

}


But compile time checking of that code where chances are there that an exception could occur in java code is an effective feature of Java coding.

When a Java code access some resources outside JVM(Run Time Environment) like a disk file, a database  server ,a computer in  a net work, here more chances are there to face an exception.

The file we try to access for reading or writing may not be there, or permission to do so may not be there.

The database server like Oracle or MySQL might not be there or might not have been started, or authentication may fail.

The computer in network may not be there or might not have switched off or authentication might fail for such access.

 The exception handling mechanism of Java is an important feature that made the technology so popular.

The hierarchy starts with Throwable class on top 

we have Error and Exception as sub classes.


1.The Exception Handling hierarchy in Java starts with

a) Object Class

b)Exception class

c)Throwable class

d)RuntimeException

Answer: c

----------------------------------------------------------------

2.The top two decedents of throwable class are:

a)Exception and RuntimeException

b)Error and RuntimeException

c)Error and Exception

d)None of the above

Answer:c

---------------------------------------------------------------- 

//Determine the output:

class Testex1 {

public static void main(String args[]) {

try {

throw 5;

}

catch(int e) {

System.out.println("Got the Exception " + e);

}

}

}

/*

1.Got the Exception 10

2.Runtime error

3.Compiletime error

4.None of the above

Answer:3 Type mismatch as you cant throw an object other

than class in exception hierarchy ...started with Throwable

*/

  4.What is true about Custom or User Defined Exception

  • a)All exceptions must be a child of Throwable.
    b)If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class.
    c)If you want to write a runtime exception, you need to extend the RuntimeException class.
    d)All of the above

  • Answer: d


No comments:

Post a Comment

python training with real time projects

java script-text to speech-assignment-Fatima Solkar Final Year-BE I.T FAMT-Ratnagiri-Maharastra

Page Title Mandvi Beach Ratnagiri At a distance of 2 km from Ratnagiri Bus Stand, Mandvi Beach i...

Technical Skills for B.Com and B.B.A