Interview Questions and Answers for 'Exception' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Exception' - 82 question(s) found - Order By Rating

next 30
 Q1. Can we pass the junit test if the tested method throws the exception ? and Should we do that ?Junit
Ans. Yes, We can pass the test even if the tested method throws an exception.

One way is know as exception testing in which the tested method is expected to throw exception and hence the expectation is set accordingly.

@Test(expected = IndexOutOfBoundsException.class)
public void test(){}

Other way is to just ignore if the tested code throws an exception. In that case , we can enclose the tested method call in the try block and within exception block, we just return gracefully.

Exception testing is perfectly ok to be done as we would like test code in case an exception occurs and if the code is handling the exception properly. Ignoring the exception by putting the tested method call within try block isn't something normal and should be avoided.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Junit exception testing


 Q2. What is Throwable ?Core Java
Ans. Throwable in java is a class that is the superclass of all exceptions and errors which may occurs in java program.It extends obcect class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exception handling   throwable     Asked in 1 Companies      basic


 Q3. If we have a return statement inside the finally block, Then what will happen ?Core Java
Ans. Returning from inside a finally block will cause exceptions to be lost. A return statement inside a finally block will cause any exception that might be thrown in the try block to be discarded.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exception handling  finally block     Asked in 1 Companies


 Q4. Explain CloneNotSupportedException ?Core Java
Ans. If we do not mention, Cloneable Interface to the Class which we want to Clone then we get this exception, only if we try to clone an object

Like:
public class TestClone{
@Override
   protected Object clone() throws CloneNotSupportedException {
      return super.clone();
   }
}

In Main, You try to do:
TestClone clone = new TestClone();
      
      TestClone clone2 = (TestClone) clone.clone();

You will get CloneNotSupportedException.

Just add -> public class TestClone implements Cloneable {

and things are fixed.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exceptions handling  cloning


 Q5. What is the difference between try-catch and @ExceptionHandler in Java/Spring Boot?Spring Boot
Ans. ExceptionHandler is Used to handle at Method level

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     @ExceptionHandler


 Q6. Differences between class not found exception and noclassdef found error ?Core Java
Ans. ClassNotFoundException is an exception that occurs when we try to load a class at run time using Class.forName() or loadClass() methods and mentioned classes are not found in the classpath.

NoClassDefFoundError is an error that occurs when a particular class is present at compile time, but was missing at run time.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exception handling  ClassNotFoundException  NoClassDefFoundError     Asked in 1 Companies


 Q7. Will this give compile time error ?

Object[] strArray = new String[2];
strArray[0] = 5;
Core Java
Ans. No. But It will throw ArrayStoreException at runtime.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     ArrayStoreException  arrays


 Q8. What is ArrayStoreException ?Core Java
Ans. It's an exception that is thrown when we attempt to add value of an incompatible type to an array.

For example -

Object[] strArray = new String[2];
strArray[0] = 5;

In this code, strArray reference of type Object has currently been assigned the String array but at line 2 we are trying to add an integer value.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     arrays   ArrayStoreException


 Q9. What is the difference between ArrayIndexOutOfBoundException and ArrayStoreException?Core Java
 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exception handling


 Q10. What will happen if there is an exception in Java finally block ?Core Java
Ans. The regular behavior of exception handling will occur. It will look for any immediate catch handler and if none is provided, it would be transmitted to the callers until a catch handler is found or it's out of main function.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exceptions  finally


 Q11. Can you explain following exception

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
Core Java
 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     ArithmeticException


 Q12. Is it a bad practice to initialize object reference to Null ?Core Java
Ans. never initialise local to null,yes it is bad pratice

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     null  nullpointerexception


 Q13. What is the purpose of re throwing the exception when we can provide the handling at the first catch itself ?Core Java
Ans. Sometimes the calls move across layers of classes and functions and hence each layer needs to perform some function like cleaning if something goes wrong deep inside.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exceptions  exceptions handling  rethrowing exception


 Q14. Can we catch errors in Java ? Core Java
Ans. Yes we can

try {
// code
} catch (Error ex) {
// handling code
}

but we shouldn't ideally do that as errors are mostly JVM based and not application based and there is rarely we can do something about it. Very likely catching and not re throwing would lead to muting their response or trace.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exception handling  error handling  errors     Asked in 1 Companies      intermediate


 Q15. Is it ok to use optional everywhere just to get over nullpointerexception ?Core Java
Ans. Optional is to be used for arguments / atrributes which are indeed optional i.e the request should continue even if they aren't provided. It should not be used for mandatory attributes or arguments as we would like application to shout out ( with error message / exception trace ) to signify a problem.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     optional  nullpointerexception  java8  java 8     Asked in 1 Companies


 Q16. What will happen if we don't have termination statement in recursion ?Core Java
Ans. It would result in endless function calls and hence eventually would result in stackoverflow exception.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     recursion  stackoverflowexception      Basic        frequent


 Q17. What could be the possible cause for following exception ?

org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):

What could be the way to fix it ?
Hibernate
Ans. This looks like the case for optimistic locking wherein hibernate suspects that the information in table was updated by some other transaction after the entity was loaded by current transaction.

One way is to have synchronized entity state and don't detach the entity. Other could be to merge the entity with the table record rather than just directly persisting the entity.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     StaleObjectStateException  Optimistic locking


 Q18. What is ConstraintViolationException in Hibernate ?Hibernate
Ans. The exception is thrown when a database constraint is violated.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     ConstraintViolationException


 Q19. What do you make out of this error

org.hibernate.exception.ConstraintViolationException: could not insert: [<Entity>]
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (<Constraint Name>) violated

and how would you go about debugging it ?
Database
Ans. Application is unable to insert a record as it violates a unique constraint.

The exception states the constraint and Table can be located by the Entity mapping. So I will go to the DB and will first check to which all columns the unique constraint applies. And then I will go and check the code and logs to see how come the duplicate column values were attempted to be inserted when they were not supposed to be.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     ConstraintViolationException  SQLIntegrityConstraintViolationException


 Q20. Doesn't it make sense to necessarily initialize references to some object to avoid NullPointerExceptions ? Can this be done ?Core Java
Ans. We can declare the reference as final to avoid reassignment but again we can always initialize the final reference to null. Even if there was any such facility available , it would have meant poor use of resources by assigning a new object in memory to each reference that's created. Many a times references are just meant to refer to other objects which already have a reference i.e sharing object by multiple references.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     null  NullPointerException


 Q21. Is it advisable to keep session or transaction open for long time just to avoid LazyInitializationException ?Hibernate
Ans. No. It's a resource and performance overhead.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     LazyInitializationException  Lazy Loading


 Q22. If you are given choice to avoid LazyInitializationException using any of the following measures, which are the ones you will choose and why ?

1. Set lazy=false in the hibernate config file.
2. Set @Basic(fetch=FetchType.EAGER) at the mapping.
3. Make sure that we are accessing the dependent objects before closing the session.
4. Force initialization using Hibernate.initialize
Hibernate
Ans. First resolution is a big No as it conveys no lazy loading in complete app. even second is advocating the same but for a particular mapping.

third one is most appropriate if loading and dependent entity property access is closer to each other in code and can be accomplished.

I don't mind using 4th too.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     lazy loading  lazy initialization  LazyInitializationException


 Q23. What does the following exception means

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
Hibernate
Ans. The error states that Hibernate is not able to initialize proxy / dependent entity objects as there is no session or transaction present. Very likely we are trying to load the dependent entities lazily but the call to dependent object property is not wrapped within the session or transaction.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Lazy Loading  Lazy Initialization  org.hibernate.LazyInitializationException


 Q24. What is Exception Wrapping ?
Ans. Exception wrapping is wrapping an exception object within another exception object and then throwing the outer exception.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exception handling  exception wrapping  exceptions      intermediate        rare


 Q25. Can we have only try block in java ?Core Java
Ans. No, It should be followed by either catch or finally.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     try  exception handling


 Q26. How should we handle errors while writing or accessing Stored Procedures?Database
Ans. Store Procedure returns the error code. Moreover we can put the call withing try block and catch SQL Exception.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     stored procedure  exception handling


 Q27. What are the different ways to handle an exception ?Core Java
Ans. 1. Wrap the desired code in try block followed by a catch / finally to catch the exception

2. Declare the throws clause with exceptions that the code is expected to throw as an indication to the calling method to take care of it.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exception handling     Asked in 4 Companies

Try 1 Question(s) Test


  Q28. What is an exception and exception handling in Java ?Core Java
Ans. An Exception in java is the occurrence during computation that is anomalous and is not expected.

Exception handling is the mechanism which is used to handle such situations.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exception handling     Asked in 18 Companies      basic        frequent


 Q29. When is the situation when finally section won't execute ?Core Java
Ans. If the process / app is abruptly killed or terminated.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exception handling  finally     Asked in 2 Companies      intermediate

Try 1 Question(s) Test


 Q30. What is the difference between following code segments

1.

try {
for(int i=0;i<100;i++){
calculate(i);
}
} catch (Exception ex) {
System.out.println("Error while processing")
}

and

2.

for(int i=0;i<100;i++){
try {
calculate(i);
} catch (Exception ex) {
System.out.println("Error while processing")
}
}
Core Java
Ans. In first case the whole loop will terminate as soon as any exception happens in the method calculate ( assuming calculate method is not handling its exception and those are thrown back at the calling method )

In Second case exception will be caught for individual iteration and hence it wont break the loop and will continue for the next iteration.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exception handling  try catch     Asked in 1 Companies


next 30

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: