Ads 468x60px

Pages

Tuesday, 26 June 2012

Java interview questions


Question: What is the difference between interface and abstract class?
Answer:
interface contains methods that must be abstract; abstract class may contain concrete methods.
interface contains variables that must be static and final; abstract class may contain non-final and final variables.
members in an interface are public by default, abstract class may contain non-public members.
interface is used to "implements"; whereas abstract class is used to "extends".
interface can be used to achieve multiple inheritance; abstract class can be used as a single inheritance.
interface can "extends" another interface, abstract class can "extends" another class and "implements" multiple interfaces.
 interface is absolutely abstract; abstract class can be invoked if a main() exists.
interface is more flexible than abstract class because one class can only "extends" one super class, but "implements" multiple interfaces.

















Question: Why do we need public static void main(String args[]) method in Java
Answer: We need
  • public: The method can be accessed outside the class / package
  • static: You need not have an instance of the class to access the method
  • void: Your application need not return a value, as the JVM launcher would return the value when it exits
  • main(): This is the entry point for the application
If the main() was not static, you would require an instance of the class in order to execute the method.
If this is the case, what would create the instance of the class? What if your class did not have a public constructor?

Question: What are the rules of serialization
Answer: Rules:
1. Static fileds are not serialized because they are not part of any one particular object
2. Fileds from the base class are handled only if hose are serializable
3. Transient fileds are not serialized

Question: What is difference between error and exception
Answer: Error occurs at runtime and cannot be recovered, Outofmemory is one such example. Exceptions on the other hand are due conditions which the application encounters such as FileNotFound exception or IO exceptions.

Question: What do you mean by object oreiented programming
Answer: In object oreinted programming the emphasis is more on data than on the procedure and the program is divided into objects.
The data fields are hidden and they cant be accessed by external functions.
The design approach is bottom up.
The functions operate on data that is tied together in data structure 

0 comments:

Post a Comment