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

Search Java Interview Questions


 2137 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Exception' - 76 question(s) found - Order By Newest

 Q1. What are the ways to avoid LazyInitializationException ?Hibernate
Admin
info@buggybread.com
Ans. 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

5. Using Fetch Join in HQL.

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

   Like      Discuss      Correct / Improve     hibernate   lazy loading hibernate   lazy initialization hibernate   lazyinitializationexception   architecture     Squelch  Overstock.com

Try 2 Question(s) Test


 Q2. How compiler handles the exceptions in overriding ?Core Java
Admin
info@buggybread.com
Ans. 1)The overriding methods can throw any runtime Exception , here in the case of runtime exception overriding method (subclass method) should not worry about exception being thrown by superclass method.

2)If superclass method does not throw any exception then while overriding, the subclass method can not throw any new checked exception but it can throw any runtime exception

3) Different exceptions in java follow some hierarchy tree(inheritance). In this case , if superclass method throws any checked exception , then while overriding the method in subclass we can not throw any new checked exception or any checked exception which are higher in hierarchy than the exception thrown in superclass method

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

   Like      Discuss      Correct / Improve     java   overriding   exceptions   inheritence   inheritance   oops


 Q3. What things should be kept in mind while creating your own exceptions in Java?Core Java
Admin
info@buggybread.com
Ans. All exceptions must be a child of Throwable.

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.

You want to write a runtime exception, you need to extend the RuntimeException class.

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

   Like      Discuss      Correct / Improve     java   exceptions   exception handling   user defined exceptions   throwable   architecture   library development   technical architect   technical lead


Very Frequently asked.
 Q4. Explain throw, throws , try and catch in Java ?Core Java
Admin
info@buggybread.com
Ans. throw is used to re throw an exception.

throws is used to declare that the method throws the respective exceptions.

try block is used to identify if the respective block has thrown any exception.

catch is used to catch the exception that has been thrown by the respective try block.

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

   Like      Discuss      Correct / Improve     java   exception handling   throw   throws   try   catch     Vidram Solutions  Cognizant (CTS)      basic        frequent


 Q5. Name few Java Exceptions ?Core Java
Anonymous
Ans. IndexOutofBoundException ,
NoClassDefFoundException ,
OutOfMemoryException ,
IllegalArgumentException,
NullPointerException,
IllegalStateException

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

   Like      Discuss      Correct / Improve     java   oops   exceptions   basic interview question      basic        frequent


 Q6. How can we make sure that a code segment gets executed even in case of uncatched exceptions ?
Anonymous
Ans. By putting it within finally.

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

   Like      Discuss      Correct / Improve     java   oops   exceptions   finally   uncatched exceptions      basic        frequent


 Q7. Explain java.lang.OutOfMemoryError ?
Core Java
Anonymous
Ans. This Error is thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.

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

   Like      Discuss      Correct / Improve     java   exceptions   jvm   outofmemoryerror


 Q8. Is it necessary that each try block to be followed by catch block ? Core Java
Anonymous
Ans. It should be followed by either catch or finally block.

  Sample Code for Retry in case of exception

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

   Like      Discuss      Correct / Improve     java   exceptions   exception handling   try   catch   finally  Oracle OCA Test     Accenture  Cognizant (CTS)      basic        frequent

Try 1 Question(s) Test


 Q9. What is exception propogation ?Core Java
Anonymous
Ans. Passing the exception object to the calling method.

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

   Like      Discuss      Correct / Improve     java   exceptions   exception handling   throw



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. When do you get ClassCastException?
Anonymous
Ans. As we only downcast class in the hierarchy, The ClassCastException is thrown to indicate that code has attempted to cast an object to a subclass of which it is not an instance.

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

   Like      Discuss      Correct / Improve     java   oops   class casting   classcastexception   exception   error      intermediate        frequent

Try 2 Question(s) Test


 Q11. Can we have try and catch blocks within finally ?Core Java
Admin
info@buggybread.com
Ans. Yes

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

   Like      Discuss      Correct / Improve     java   exceptions   exception handling   finally   try   catch   yesno


 Q12. Which of the following exception is thrown when we try to access element which is beyond the size ?

a. NullPointerException
b. ArrayIndexOutOfBoundException
c. ArithmeticException
d. ParseException
Anonymous
Ans. ArrayIndexOutOfBoundException

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

   Like      Discuss      Correct / Improve     java   exceptions   error handling


 Q13. Checked exception needs to be ...

a. Caught
b. Method needs to declare that it throws these exception
c. Either A or B
d. Both A and B
Anonymous
Ans. Either A or B

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

   Like      Discuss      Correct / Improve     java   exceptions   checked exceptions


 Q14. What will happen if we don't have termination statement in recursion ?
Manoj
Ans. Function call allocates a stackframe in stack. Every stackframe will use some memory to store local variables, parameters and to remember return address. Without terminating condition stackframes will keep consuming memory from stack and eventually program will result in stackoverflow error.

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

   Like      Discuss      Correct / Improve     recursion   break statement  break  stackoverflow exception  stackoverflow  memory management  memory  exceptions


Frequently asked.
 Q15. When does the finally block gets executed ?Core Java2016-10-31 09:29:10

Ans. A finally block of code always executes, whether or not an exception has occurred.The only time finally won't be called is if you call System.exit() or if the JVM crashes first.

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

   Like      Discuss      Correct / Improve     finally  exception handling  exceptions     Verilogue  Model N  CA Technologies  Mindteck      basic        frequent


Frequently asked at HCL Technologies ( Based on 3 feedback )
  Q16. Difference between Checked and Unchecked exceptions ?Core Java
Anonymous
Ans. Checked exceptions are the exceptions for which compiler throws an errors if they are not checked whereas unchecked exceptions are caught during run time only and hence can't be checked.

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

   Like      Discuss      Correct / Improve     java   exceptions   checked exceptions   unchecked exceptions   exception handling   basic interview question     Verilogue  F5 Network  Pegasystems  PegaSystems  SAP  Ciber  Conde Nast  Pramati Technologies  IBM  JP Morgan  HCL Technologies  Tata Consultancy (TCS)  Cognizant (CTS)  Cogent Infotech  MarketAxess  defense group  NETS INTERNATIONAL  Accenture  Integra Micro  ColdLight Solutions  Endeavor  Deloitte  Infosys      basic        frequent

Try 1 Question(s) Test


 Q17. How does a try statement determine which catch clause should be used to handle an exception?Core Java
Anonymous
Ans. When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception is executed. The remaining catch clauses are ignored.

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

   Like      Discuss      Correct / Improve     java   exceptions   exception handling   try   catch      basic        frequent


 Q18. Give an Example of checked and unchecked exception ?Core Java
Anonymous
Ans. ClassNotFoundException is checked exception whereas NoClassDefFoundError is a unchecked exception.

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

   Like      Discuss      Correct / Improve     java   exceptions   checked exceptions   unchecked exceptions   exception handling     SAP  JP Morgan


 Q19. Can finally block be used without catch ?Core Java
Anonymous
Ans. Yes but should follow "try" block then.

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

   Like      Discuss      Correct / Improve     java   exceptions   exception handling   try   catch   finally   yes-no

Try 1 Question(s) Test



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q20. How would you go about debugging a NullPointerException?Core Java
Admin
info@buggybread.com
Ans. Open ended Questions.

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

   Like      Discuss      Correct / Improve     java   open questions   exceptions


 Q21. Difference between throw and throws ?Core Java
Admin
info@buggybread.com
Ans. throw is used to explicitly throw an exception especially custom exceptions, whereas throws is used to declare that the method can throw an exception.

We cannot throw multiple exceptions using throw statement but we can declare that a method can throw multiple exceptions using throws and comma separator.

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

   Like      Discuss      Correct / Improve     java   exceptions   exception handling   throw   throws   difference between   basic interview question      basic        frequent

Try 1 Question(s) Test


 Q22. What are the sub classes of Exception class?Core Java
Admin
info@buggybread.com
Ans. The Exception class has two main subclasses : IOException class and RuntimeException Class.

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

   Like      Discuss      Correct / Improve     java   exceptions   exception class   ioexception   runtimeexception


 Q23. How finally used under Exception Handling?Core Java
Admin
info@buggybread.com
Ans. The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

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

   Like      Discuss      Correct / Improve     java   exceptions   exception handling   try   finally   basic interview question     Oracle Engineering Group

Try 1 Question(s) Test


 Q24. Can try statements be nested?Core Java
Admin
info@buggybread.com
Ans. Yes

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

   Like      Discuss      Correct / Improve     java   exceptions   try   yes no      basic


 Q25. Will finally be called always if all code has been kept in try block ?Core Java
Admin
info@buggybread.com
Ans. The only time finally won't be called is if you call System.exit() or if the JVM crashes first.

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

   Like      Discuss      Correct / Improve     java   exceptions   exception handling   try   catch   finally   system   system.exit


 Q26. what will be the output ?

class Animal {
public void eat() throws Exception {
}
}

class Dog2 extends Animal {
public void eat(){}
public static void main(){
Animal an = new Dog2();
an.eat();
}
}
Core Java
Admin
info@buggybread.com
Ans. Compile Time Error: Unhandled exception type Exception

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

   Like      Discuss      Correct / Improve     java   code   coding   overridding   late binding   exception handling   abstract class   abstract methods


 Q27. Can finally block throw an exception ?Core Java
Admin
info@buggybread.com
Ans. Yes.

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

   Like      Discuss      Correct / Improve     java   exceptions   exception handling   finally   yes-no


 Q28. Which exception should be handled in the following code ?

File file = new File("../file.txt");
FileWriter fileWriter = new FileWriter(file);
Core Java
Admin
info@buggybread.com
Ans. IOException

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

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   exception   ioexception  file handling


 Q29. Which exceptions should be handled with the following code ?

FileOutputStream fileOutputStream = new FileOutputStream(new File("newFile.txt"));
Admin
info@buggybread.com
Ans. FileNotFoundException

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

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   scjp   ocjp   filenotfoundexception   fileoutputstream  file handling



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q30. What is the problem with this code ?

class BuggyBread1 {

private BuggyBread2 buggybread2;

public static void main(String[] args){
try {
BuggyBread1 buggybread1 = new BuggyBread1();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
objectOutputStream.writeObject(buggybread1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Core Java
Admin
info@buggybread.com
Ans. Though we are trying to serialize BuggyBread1 object but we haven't declared the class to implement Serializable.

This will throw java.io.NotSerializableException upon execution.

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

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   serialization   notserializableexception   exception   file handling


 Q31. Will this code run fine if BuggyBread2 doesn't implement Serializable interface ?

class BuggyBread1 implements Serializable{
private BuggyBread2 buggybread2 = new BuggyBread2();

public static void main(String[] args){
try {
BuggyBread1 buggybread1 = new BuggyBread1();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
objectOutputStream.writeObject(buggybread1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Core Java
Admin
info@buggybread.com
Ans. No, It will throw java.io.NotSerializableException.

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

   Like      Discuss      Correct / Improve     java   io   file   fileio   coding   code   serialization   notserializableexception   exception   file handling


 Q32. When does an application throw NullPointerException ?Core Java
admin
info@buggybread.com
Ans. When it tries to access an object element or method using reference which is actually null.

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

   Like      Discuss      Correct / Improve     exceptions   npe   nullpointerexception     EverBank Financial      basic        frequent


 Q33. How can we protect an application from throwing a NullPointerException ?
admin
info@buggybread.com
Ans. By having Null Checks.

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

   Like      Discuss      Correct / Improve     exceptions   npe   nullpointerexception   null checks      basic        frequent


 Q34. What will the following code do ?

String dateStr = "2011 11 19";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(dateStr);
System.out.println(date);

admin
info@buggybread.com
Ans. It will throw the ParseException as the date format doesn't abide with the format of the specified date.

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

   Like      Discuss      Correct / Improve     date   utility classes   dateformat   exception   parseexception


 Q35. Tell me something about AssertionError ?Core Java
Admin
info@buggybread.com
Ans. AssertionError is actually a fatal fault or a bug in the program. We may not like to continue program , request or thread execution if this error occurs as this condition is the assumption to continue further execution.

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

   Like      Discuss      Correct / Improve     java   error   exceptions   exception handling   assertion error   assertionerror


 Q36. What is CopyOnWriteArrayList ?
Admin
info@buggybread.com
Ans. Its a type of ArrayList in which all Write operations , i.e add and set are performed by creating a new copy. This array never changes during the lifetime of the iterator, so it never throws ConcurrentModificationException

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

   Like      Discuss      Correct / Improve     java   collections   list   arraylist   copyonwritearraylist   ConcurrentModificationException     Roposo


 Q37. What are the advantages and disadvantages of CopyOnWriteArrayList ?Core Java
Admin
info@buggybread.com
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     Cognizant (CTS)  Engine Singapore  Credit Agricole  Roposo      Expert


 Q38. What is ConcurrentModificationException ?Core Java
Admin
info@buggybread.com
Ans. This is the exception that is thrown when we try to modify the non concurrent collection class while iterating through it.

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

   Like      Discuss      Correct / Improve     java   collections   concurrentmodificationexception   exception  concurrency     Calypso Technology  Deloitte  IRIS Software      intermediate


 Q39. What is ArrayIndexOutOfBoundException ?
Admin
info@buggybread.com
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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q40. Name few Error and Exception classes provided by Java ?
Admin
info@buggybread.com
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


 Q41. In which cases isn't instanceof operator a bad practice?
Admin
info@buggybread.com
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

Try 2 Question(s) Test


 Q42. What is OutOfMemoryError in Java?Core Java
admin
info@buggybread.com
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


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

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

   Like      Discuss      Correct / Improve     java   BatchUpdateException   include


 Q44. 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
Anonymous
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


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

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

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

   Like      Discuss      Correct / Improve     java   exception   threads


 Q46. What are the fail safe systems ?Operating System2016-06-09 09:29:43

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     Verilogue


 Q47. Explain Exception Hierarchy in Java SE ?Core Java2016-10-18 11:02:09

Ans. http://www.programcreek.com/2009/02/diagram-for-hierarchy-of-exception-classes/

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

   Like      Discuss      Correct / Improve     exception hierarchy   exceptions     Cognizant (CTS)


 Q48. 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 Java2016-10-18 11:37:51

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     Cognizant (CTS)      intermediate


 Q49. What happens to the Exception object after the exception handling is done ?Core Java2016-10-05 06:09:59

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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q50. How can we catch an exception thrown by another thread ?Core Java2016-11-17 11:38:29

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


 Q51. What is Java NumberFormatException ?Core Java2016-11-23 10:42:54

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


 Q52. Difference between Error and Exception ?Core Java2016-12-15 14:13:56

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     Accenture      basic        frequent


 Q53. 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 Java2016-12-29 15:42:10

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     Tieto


 Q54. When is the situation when finally section won't execute ?Core Java2017-01-09 12:56:46

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     EPAM  Nix Solutions      intermediate

Try 1 Question(s) Test


  Q55. What is an exception and exception handling in Java ?Core Java2017-01-09 13:01:40

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     EPAM  Deloitte  Choice Hotels  Future Group  Cognizant (CTS)  Wipro  Infosys  HCL Technologies  Accenture  Amdocs  IBM  Ultramain Systems      basic        frequent


 Q56. What are the different ways to handle an exception ?Core Java2017-01-12 11:17:53

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     HCL Technologies  Wipro  Mindtree  HeadStrong

Try 1 Question(s) Test


 Q57. How should we handle errors while writing or accessing Stored Procedures?Database2017-01-19 13:55:55

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


 Q58. Can we have only try block in java ?Core Java2017-01-19 13:57:54

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


 Q59. What is Exception Wrapping ?2017-02-13 15:02:37

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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q60. What does the following exception means

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
Hibernate2017-02-16 11:28:15

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


 Q61. 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
Hibernate2017-02-16 13:32:41

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


 Q62. Is it advisable to keep session or transaction open for long time just to avoid LazyInitializationException ?Hibernate2017-02-16 20:33:52

 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     LazyInitializationException  Lazy Loading


 Q63. Doesn't it make sense to necessarily initialize references to some object to avoid NullPointerExceptions ? Can this be done ?Core Java2017-03-16 09:54:16

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


 Q64. 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 ?
Database2017-03-22 13:12:50

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


 Q65. What is ConstraintViolationException in Hibernate ?Hibernate2017-03-22 13:13:33

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


 Q66. 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 ?
Hibernate2017-03-29 12:44:00

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


 Q67. What will happen if we don't have termination statement in recursion ?Core Java2017-04-18 20:13:15

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


 Q68. Is it ok to use optional everywhere just to get over nullpointerexception ?Core Java2017-05-23 08:16:28

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     Itech


 Q69. Can we catch errors in Java ? Core Java2017-05-23 08:21:10

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     ITech      intermediate



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q70. What is the purpose of re throwing the exception when we can provide the handling at the first catch itself ?Core Java2017-06-20 09:13:41

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


 Q71. Is it a bad practice to initialize object reference to Null ?Core Java2017-07-23 19:56:17

 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     null  nullpointerexception


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

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

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

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

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



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

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:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: