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 - Order By Newest

   
 Q61. 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


 Q62. 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


 Q63. 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


 Q64. 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


 Q65. 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


 Q66. 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


 Q67. 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


 Q68. 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


 Q69. 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


 Q70. 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


 Q71. 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


 Q72. 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


 Q73. 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


 Q74. 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


 Q75. 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


 Q76. 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


 Q77. 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


 Q78. 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


 Q79. 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


 Q80. 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


 Q81. 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


 Q82. 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


 Q83. In Case a method is declared to throw an exception , We can only use a call to that method if ...Core Java
a. We use the call within try block and catch the exception
b. We declare that the method is expected to throw the exception using throws
c. Both 1 and 2
d. Either 1 or 2

Ans.d. Either 1 or 2

 Q84. Which of the following is true ?Core Java
a. throw and throws are used to throw an exception
b. throw is used to throw an exception whereas throws is a declaration that the method can throw the exception
c. throws is used to throw an exception whereas throw is a declaration that the method can throw the exception
d. throw and throws are used to declare that an exception can be thrown by the method

Ans.b. throw is used to throw an exception whereas throws is a declaration that the method can throw the exception

 Q85. throws is a declaration that the ..... is expected to throw an exception ?Core Java
a. Variable
b. Method
c. Class
d. Interface

Ans.b. Method

 Q86. Which of following is not the resolution for preventing LazyInitializationException?Hibernate
a. Set fetch=FetchType.EAGER for Dependent entity mapping
b. Make sure that we are accessing the dependent objects before closing the session
c. Make sure that we are accessing the dependent objects after closing the session
d. Wrap the complete access within Transaction

Ans.c. Make sure that we are accessing the dependent objects after closing the session

 Q87. Which of the following is a valid way to avoid NullPointerException ?Core Java
a. Avoid assigning null to the reference
b. Null Checks before accessing elements of an object
c. Both of the above
d. None of the above

Ans.b. Null Checks before accessing elements of an object

previous 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: