Saturday, 28 April 2012

JAVA interview questions 25

Q241. What is the purpose of throw? 
Ans. Sometimes we can create Exception object explicitly and we can handover that exception object to the JVM explicitly by throw keyword. 
The purpose of throw keyword is to handover our created exception object explicitly to the
JVM.
Example1: 
class Test{ 
public static void main(String[] args){ 
System.out.println(10/0); 


In this case ArithmeticException object created implicitly and handover to the JVM automatically by the main method. 
Example2: 
class Test{ 
Public static void main(String[] args){ 
Throw new ArithmeticException(“/by Zero”); } 

In this case creation of an exception object and handover to the JVM explicitly by the programmer. 

Q242. Is it possible to throw an Error? 
Ans. Yes, It is possible to throw any Throwable type including Error. 

Q243. Is it possible to throw any java object? 
Ans. No, we can use throw keyword only for throwable objects otherwise we will get compile time error saying incompatible type. 
* Core Java 
* Servlets & JSP 
* Struts 
* EJB 
* J2ME 

Q244. After throw is it allow to take any statement directly? 
Ans. After throw statement we are not allow to place any statement directly violation leads to compile time error saying Unreachable Statement. 

Q245. What is the purpose of throws? 
Ans. The main purpose of throws keyword is to delegate the responsibilities of exception handling to the caller. It requires in the case of checked exception. 

Q246. What is the difference between throw and throws? 
Ans. Sometimes we can create Exception object explicitly and we can handover that exception object to the JVM explicitly by throw keyword.The main purpose of throw keyword is to 
handover our created exception object explicitly to the JVM. The main purpose of throws 
keyword is to delegate the responsibilities of exception handling to the caller. It requires in the case of checked exception. 

Q47. What is the difference between throw and thrown? 
Ans. There is no terminology of thrown in java. 

Q248. Is it possible to use throws keyword for any java class? 
Ans. No, we can use throws keyword only for Throwable classes. Otherwise we will get compile time error saying Incompatible types. 

Q249. If we are taking catch block for an exception but there is no chance of rising that exception in try then what will happen? 
Ans. If there is no chance of raising an exception in try then we are not allow to write catch 
block for that exception violation leads to compile time error. But this rule is applicable only for fully checked exception. 

Q250. Explain Exception Handling keyword? 
Ans. Exception Handling keyword: Try :- To maintain Risky code. 
Catch:- To maintain Exception Handling code. Finally:- To maintain the clean up code. 
Throw:- To handover our created exception object to the JVM explicitly. 
Throws:- To delegate the responsibilities of Exception Handling to the caller. 

JAVA interview questions 24

Q231.Is it possible to take try, catch inside catch block? 
Ans. Yes, It is possible to take try, catch inside catch block. 

Q232. Is it possible to take try without catch? 
Ans. Yes, it is possible to take try without catch but compulsory finally block should be available. 

Q233. What is the purpose of finally block? 
Ans. The main purpose of finally block is, to maintain the cleanup code. This block will execute always. 

Q234. Is finally block will be execute always? 
Ans. Yes finally block will be executed always irrespective of whether exception raised or not raised whether exceptions are handled or not handle. There is one situation where the finally block won’t be executed if the JVM is going to be shutdown. 

Q235. In which situation finally block will not executed? 
Ans. There is one situation where the finally block won’t be executed if we are using 
system.exit(0) explicitly then JVM itself will be shutdown and there is no chance of executing finally block. 

Q236. If return statement present inside try is finally block will be executed? 
Ans. Yes, if return statement present inside try, then also finally block will be executed. finally block will dominate return statement also. 

Q237. What is the difference between final, finally and finalize()? 
Ans. final:- final is a modifier applicable for variables, methods and classes. final variable 
means constant and reassignment is not possible. final method means implementation is final in the child classes we can’t override. final class means it won’t participate in inheritance and child class creation is not possible. 
finally:- It is a block associated with try catch to maintain cleanup code. Finally block will be executed always irrespective of whether exception is raised or not raised or whether the exception is handle or not handle. 
finalize():- It is a method, Garbage collector always calls this method just before destroying any object to perform cleanup activities. 

Q238. Is it possible to write any statement between try-catch and finally? 
Ans. No, it is not possible to write any statement between try catch and finally. If we will try to write any statement between them then we will get compile time error. 

Q239. Is it possible to take two finally blocks for the same try? 
Ans. No, it is not possible to take two finally blocks for the same try. If we try to take then we will get compile time error. 

Q240. Is syntax try-finally-catch is valid ? 
Ans. No, this syntax is not valid. It should be like try-catch-finally then only code will compile.

JAVA interview questions 23

Q221.What is the purpose of Exception Handling? 
Ans.The main purpose of Exception Handling is for graceful termination of the program. 

Q222.What is the meaning of Exception Handling? 
Ans. Exception Handling doesn’t mean repairing an Exception, we have to define alternative way to continue rest of the code normally. 
Example: If our programming requirement is to read the data from the file locating at London but at Runtime if London file is not available then we have to use local file alternatively to continue rest of program normally. This is nothing but Exception Handling. 

Q223.Explain Default Exception Handling Mechanism in java? 
Ans.If an exception raised, the method in which it’s raised is responsible for the creation of Exceptions object by including the following information: 
Name of the Exception 
Description of the Exception 
Stack Trace 
After creating Exception object the method handover it to the JVM. JVM checks for Exception Handling code in that method. 
If the method doesn’t contain any Exception handling code then JVM terminates the 
method abnormally and removes the corresponding entry from the stack. 
JVM identify the caller method and checks for Exception Handling code in that method. If the caller doesn’t contain any exception handling code then JVM terminates that method abnormally and removes the corresponding entry from the stack. 
This process will be continue until main() method. 
If the main() method also doesn’t contain exception handling code the JVM terminates that main() method and removes the corresponding entry from the stack. Just before terminating the program abnormally JVM handovers the responsibility of 
exception handling to the Default Exception Handler which is the component of JVM. 
Default Exception Handler just print exception information to the consol in the following format 

Name of Exception: Description 
Stack Trace (Location of the Exception) 

Q224.What is the purpose of try? 
Ans We should maintain all risky code inside the try block. 


Q225. What is the purpose of catch block? 
Ans.We have to maintain all Exception Handling code inside the catch block. 

Q226. Is try with multiple catch block is possible? 
Ans. The way of handling an exception is varied from exception to exception compulsory we have to write a separate catch block for every exception. Hence try will multiple catch block is possible and it is recommended to use. 
Example: 
try{ 
//Risky code
}
catch(IOException e)
{
//Hndling code for IOException
}
catch(ArithmeticException e) { 
//handling code for AE
}
catch(NullPointerExcetpion e)
{
// handling code for NPE
}
catch(Exception e)
{
//default exception handling code
}


Q227. If try with multiple catch block present is order of catch blocks important in which order we have to take? 
Ans. If try with multiple catch block present then the order of catch block is very important it should be from child to parent but not from parent to child. 


Q228. What are various methods to print Exception information? and differentiate them. 
Ans. 
Throwable class defines the following method to print exception or error information . 
1. printStackTrace() :- This method print exception information in the following format. 
Name of the Exception: Description 
StackTrace 
2.toString():- This method print exception information in the following format. 
Name of the Exception: Description 
3.getMessage():- This method prints only description of the exception. 
Description 


Q229.If an exception rised inside catch block then what will happen? 
Ans. If an exception raised inside catch block and it is not part of any try block then it is always abnormal termination. 

Q230. Is it possible to take try, catch inside try block? 
Ans. Yes, It is possible to take try, catch inside try block. That is nesting of try catch is possible.

JAVA interview questions 22

Q211. What method must be implemented by all threads? 
All tasks must implement the run() method, whether they are a subclass of Thread or implement the Runnable interface. 

Q212. What methods are used to get and set the text label displayed by a Button object? 
getLabel() and setLabel() 

Q213. Which Component subclass is used for drawing andpainting? 
Canvas 

Q214. What are synchronized methods and synchronized statements? 
Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement. 

Q215. What are the two basic ways in which classes thatcan be run as threads may be defined? 
A thread class may be declared as a subclass of Thread, or it may implement the Runnable interface. 

Q216. What are the problems faced by Java programmers who don't use layout managers? 
Without layout managers, Java programmers are faced with determining how their GUI will be displayed across multiple windowing systems and finding a common sizing and positioning that will work within the constraints imposed by each windowing system. 

Q217. What is the difference between an if statement and aswitch statement? 
The if statement is used to select among two alternatives. It uses a boolean expression to decide 
which alternative should be executed. The switch statement is used to select among multiple 
alternatives. It uses an int expression to determine which alternative should be executed. 

Q218. What happens when you add a double value to a String? 
The result is a String object. 

Q219. What is the List interface? 
The List interface provides support for ordered collections of objects 

Q220.What is an Exception? 
Ans.An unwanted, unexpected event that disturbs normal flow of the program is called Exception.Example: FileNotFondException.