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

   next 30
 Q41. What are the advantages and disadvantages of CopyOnWriteArrayList ?Core Java
Ans. This collections class has been implemented in such a manner that it can never throw ConcurrentModificationException. As it performs update and write operations by creating a new copy of ArrayList, It's slower compared to ArrayList.

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

   Like         Discuss         Correct / Improve     java   collections   list   arraylist   copyonwritearraylist   advantages-disadvantages   ConcurrentModificationException     Asked in 4 Companies      Expert


 Q42. What is ArrayIndexOutOfBoundException ?Core Java
Ans. Exception thrown by the application is we try to access an element using an index which is not within the range of array i.e lower than 0 or greater than the size of the array.

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

   Like         Discuss         Correct / Improve     java   exceptions   arrayindexoutofboundexception     Asked in 1 Companies


 Q43. Name few Error and Exception classes provided by Java ?
Ans. http://www.buggybread.com/2015/01/java-list-of-error-exception-classes.html

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

   Like         Discuss         Correct / Improve     java   exceptions   error


 Q44. In which cases isn't instanceof operator a bad practice?Core Java
Ans. To avoid ClassCastException.

Though the following code will compile fine but will result in ClassCastException during runtime.

Fruit fruit = new Apple();
Banana banana = Banana(fruit); // ClassCastException


This code will not give compile time error as Banana and Fruit are related as Banana either extends or implement Fruit, So downcasting is acceptable. With this code we assume that the Fruit handler will have the Apple object at that point, violating which the code will throw the exception.

This exception can be avoided by following code.

Fruit fruit = new Apple();
if(fruit instanceOf Banana){
Banana banana = Banana(fruit); // ClassCastException
}

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

   Like         Discuss         Correct / Improve     java   instanceof   classcastexception   runtime exceptions  instanceOf operator

Try 2 Question(s) Test


 Q45. What is OutOfMemoryError in Java?Core Java
Ans. OutOfMemoryError in Java is a subclass of VirtualMachineError and is thrown by JVM when it runs out of heap memory.

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

   Like         Discuss         Correct / Improve     outofmemoryerror   error   exceptions   jvm   java   heap memory   heap


 Q46. Which interfaces are implemented by BatchUpdateException?
Ans. [Iterable]

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

   Like         Discuss         Correct / Improve     java   BatchUpdateException   include


 Q47. Which of the following is not possible ?

a. try block followed by catch
b. try block followed by finally
c. try block followed by catch block and then finally
d. try block without catch or finally block
Ans. try block without catch or finally block

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

   Like         Discuss         Correct / Improve     exceptions   java   try-catch   finally

Try 1 Question(s) Test


 Q48. Invoking start twice on same thread leads to ..

a. ClassCastException
b. NullPointerException
c. InterruptedException
d. IllegalStateException
Ans. IllegalStateException

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

   Like         Discuss         Correct / Improve     java   exception   threads


 Q49. What are the fail safe systems ?Operating System
Ans. Fail Safe systems are tolerant systems that continue processing even if they sense any problem. the objective here is to continue with the processing even if there are some problems instead of completely shutting it down. Example could be to catch an exception and still letting it complete with partial results.

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

   Like         Discuss         Correct / Improve     fail safe systems   system design   exception handling     Asked in 3 Companies


 Q50. Explain Exception Hierarchy in Java SE ?Core Java
Ans. <a href="http://www.programcreek.com/2009/02/diagram-for-hierarchy-of-exception-classes/" rel="nofollow">http://www.programcreek.com/2009/02/diagram-for-hierarchy-of-exception-classes/</a>

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

   Like         Discuss         Correct / Improve     exception hierarchy   exceptions     Asked in 3 Companies


 Q51. Can we have try statement without catch? If try statement contains return will the finally block be executed? What happens if there is an exception inside finally block?Core Java
Ans. Yes, with finally.

Yes, finally block will be executed even if there is no exception in try block.

If finally throws an exception, the exception gets thrown to the calling module.

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

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


 Q52. What happens to the Exception object after the exception handling is done ?Core Java
Ans. As with all other objects its garbage collected after its usage unless its rethrown.

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

   Like         Discuss         Correct / Improve     exceptions  exception handling        rare


 Q53. How can we catch an exception thrown by another thread ?Core Java
Ans. http://stackoverflow.com/a/6548203/2789764

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

   Like         Discuss         Correct / Improve     exception handling  exception      expert        rare


 Q54. What is Java NumberFormatException ?Core Java
Ans. NumberFormatException is the exception that Java throws when we try to convert one data type to the Number data type but the value is not parsable to be a Number.

For example , the following code will result in the NumberFormatException

String string = "Buggy";
int strtoInt = Integer.parseInt(string);

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

   Like         Discuss         Correct / Improve     exceptions  NumberFormatException


 Q55. Difference between Error and Exception ?Core Java
Ans. An Error indicates serious problems that a reasonable application should not try to catch whereas

An Exception indicates conditions that a reasonable application might want to catch.

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

   Like         Discuss         Correct / Improve     error  exception handling  exceptions     Asked in 2 Companies      basic        frequent


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


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


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


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


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


 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


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