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


 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


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: