Interview Questions and Answers - Order By Newest 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 Q12. How can we make sure that a code segment gets executed even in case of uncatched exceptions ?
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 Q13. Is it necessary that each try block to be followed by catch block ? Core Java
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 Asked in 3 Companies basic   frequent Try 1 Question(s) Test Q14. What is exception propogation ? Core Java
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 Q15. Can we have try and catch blocks within finally ? Core Java
Ans. Yes, if we have a cleanup code that might throw an exception in the finally block, then we can have a try-catch block 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 Q16. When does an application throw NullPointerException ? Core Java
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 Asked in 1 Companies basic   frequent Q17. 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
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 Q18. 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
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 exceptionsFrequently asked. Q19. When does the finally block gets executed ? Core Java
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 Asked in 4 Companies basic   frequent Q20. 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 Q21. 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 Q22. How does a try statement determine which catch clause should be used to handle an exception? Core Java
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 Q23. Give an Example of checked and unchecked exception ? Core Java
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 Asked in 2 Companies Q24. Can finally block be used without catch ? Core Java
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 Q25. How would you go about debugging a NullPointerException? Core Java
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 Q26. Difference between throw and throws ? Core Java
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 Q27. What are the sub classes of Exception class? Core Java
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 Q28. How finally used under Exception Handling? Core Java
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 Asked in 1 Companies Try 1 Question(s) TestAns. 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 Q30. Will finally be called always if all code has been kept in try block ? Core Java
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 Q31. 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
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 Q32. Can finally block throw an exception ? Core Java
Ans. Yes. Methods invoked from within a finally block can throw an exception. Failure to catch and handle such exceptions results in the abrupt termination of the entire try block. 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 Q33. Which exception should be handled in the following code ?
File file = new File("../file.txt");
FileWriter fileWriter = new FileWriter(file); Core Java
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 Q34. Which exceptions should be handled with the following code ?
FileOutputStream fileOutputStream = new FileOutputStream(new File("newFile.txt"));
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 Q35. 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
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 Q36. 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
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 Q37. How can we protect an application from throwing a NullPointerException ?
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 Q38. 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);
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 Q39. Tell me something about AssertionError ? Core Java
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 Q40. What is CopyOnWriteArrayList ?
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 Asked in 1 Companies