Core Java Interview Question, Interview Question

This page discusses - Core Java Interview Question, Interview Question

Core Java Interview Question, Interview Question

Core Java Interview Question Page 29

     

Flow Control and exception

Question: What is the difference between while and do while loop

Answer: Do while loop walways executes the body of the loop at least once, since the test is performed at the end of the body

Question: When do you use continue and when do you use break statements

Answer: When continue statement is applied it prematurely completes the iteration of a loop.
When break statement is applied it causes the entire loop to be abandoned.

Question: What is the base class from which all exceptions are subclasses

Answer: All exceptions are subclasses of a class called java.lang.Throwable

Question: How do you intercept and thereby control exceptions

Answer: We can do this by using try/catch/finally blocks
You place the normal processing code in try block
You put the code to deal with exceptions that might arise in try block in catch block
Code that must be executed no matter what happens must be place in finally block

Question: When do we say an exception is handled

Answer: When an exception is thrown in a try block and is caught by a matching catch block, the exception is considered to have been handled

Question: When do we say an exception is not handled

Answer: There is no catch block that names either the class of exception that has been thrown or a class of exception that is a parent class of the one that has been thrown, then the exception is considered to be unhandled, in such condition the execution leaves the method directly as if no try has been executed

Question: In what sequence does the finally block gets executed

Answer: If you put finally after a try block without a matching catch block then it will be executed after the try block
If it is placed after the catch block and there is no exception then also it will be executed after the try block
If there is an exception and it is handled by the catch block then it will be executed after the catch block

Question: What can prevent the execution of the code in finally block

Answer: 

  • The death of thread
  • Use of system.exit()
  • Turning off the power to CPU
  • An exception arising in the finally block itself

What are the rules for catching multiple exceptions
A more specific catch block must precede a more general one in the source, else it gives compilation error
Only one catch block, that is first applicable one, will be executed

Question: What does throws statement declaration in a method indicate

Answer: This indicates that the method throws some exception and the caller method should take care of handling it

Question: What are checked exception

Answer: Checked exceptions are exceptions that arise in a correct program, typically due to user mistakes like entering wrong data or I/O problems

Question: What are runtime exceptions

Answer: Runtime exceptions are due to programming bugs like out of bond arrays or null pointer exceptions.

Question: What is difference between Exception and errors

Answer: Errors are usually compile time and exceptions can be runtime or checked

Question: How will you handle the checked exceptions

Answer: You can provide a try/catch block to handle it. OR Make sure method declaration includes a throws clause that informs the calling method an exception might be thrown from this particular method. When you extend a class and override a method, can this new method throw exceptions other than those that were declared by the original method. No it cannot throw, except for the subclasses of those exceptions.

Question: Is it legal for the extending class which overrides a method which throws an exception, not o throw in the overridden class

Answer: Yes it is perfectly legal

Question: Explain the user defined Exceptions?

Answer: User defined Exceptions are the separate Exception classes defined by the user for specific purposed. An user defined can created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions.

Example:
 
class myCustomException extends Exception {
// The class simply has to exist to be an exception
}

Tutorials

  1. Core Java Interview Question Page 1
  2. Core Java Interview Question Page 3
  3. Core Java Interview Question Page 8
  4. Core Java Interview Question, Interview Question
  5. Core Java Interview Question, Interview Question
  6. Core Java Interview Question, Interview Question
  7. Core Java Interview Question, Interview Question
  8. Core Java Interview Question, Interview Question
  9. Core Java Interview Question, Interview Question
  10. Core Java Interview questions and answers
  11. Core Java Interview Question, Interview Question
  12. Core Java Interview Question, Interview Question
  13. Core Java Interview Question, Interview Question
  14. Core Java Interview Question, Interview Question
  15. Core Java Interview Question, Interview Question
  16. Core Java Interview Question, Interview Question
  17. Core Java Interview Question, Interview Question
  18. Core Java Interview Question, Interview Question
  19. Core Java Interview Question, Interview Question
  20. Core Java Interview Question, Interview Question
  21. Core Java Interview Question, Interview Question
  22. Core Java Interview Question, Interview Question
  23. Core Java Interview Question, Interview Question
  24. Core Java Interview Question, Interview Question
  25. Core Java Interview Question, Interview Question
  26. Core Java Interview Question, Interview Question
  27. Core Java Interview Question, Interview Question
  28. Core Java Interview Question, Interview Question
  29. Core Java Interview Question, Interview Question
  30. Core Java Interview Question, Interview Question
  31. Core Java Interview Question, Interview Question
  32. Core Java Interview Question, Interview Question
  33. Core Java Interview Question, Interview Question
  34. Core Java Interview Question, Interview Question
  35. Core java Interview Questions