Wednesday, September 17, 2008

Interview Q & A: Java Basics

#1 What is the difference between JVM, JRE and JDK?

JVM is required to run bytecode (which is the compiled format of java program). They are different for different OS.

JRE is a java distribution with JVM and java core libraries. This is good enough to run bytecode.

JDK is a java distribution with JRE plus compiler and other tools. This is good enough to write, compile and run code.

#2 What is method overloading and method overriding?

Method overloading: Same method name with different signature. This is also called static polymorphism (response is decided during compile time). It uses early binding.

Method overriding: Same method name with same signature. This is also called dynamic polymorphism (response is decided during run time). It uses late binding.

#3 What is upcast and downcast?

If we convert a base class object to one of its derived class object, it is termed as downcast and vice versa is termed as upcast.
 o  Upcast example: Object obj=new String();
 o  Downcast example: String str=(String) obj;

#4 What is the similarities and difference between abstract class and interface?

Similarities:
 o  Both abstract class and interface cannot be instantiated.

Differences:
 o  Abstract classes have partial implementation whereas interfaces are fully unimplemented classes. That is, abstract classes can have either or both of abstract methods (unimplemented methods/methods without any definition) or concrete methods (methods with definition) whereas interfaces can have only methods with no implementations.
 o  Class can implement multiple interfaces, but can implement only one abstract class.
 o  All methods in an interface are public and all variables are public final static by default which makes them as constants. Variables defined in an interface cannot be overridden by its implementing class.

#5 How to choose between abstract classes and interfaces?

Abstract classes over interfaces:
Abstract classes are faster than interfaces as interface requires an extra indirection to identify the corresponding method in class where implementation resides.
If we have a scenario where we need to provide a default implementation to one of the methods and want to force developers to provide implementation for another method during sub classing, then abstract class can be chosen. This way it allows developers to decide based on needs whether to override the default concrete methods as well apart from providing implementation for abstract methods.

Interfaces over abstract classes:
Provide a form of multiple inheritances. That is class can implement multiple interfaces and also interface can extend multiple interfaces as all interfaces has no implementations.
As all variables inside interfaces are public final static, they can be accessed directly by interfacename.variablename which in turn is very much useful in defining constants that can be used across application.

#6 What are Exceptions in Java?

An event that occurs during the execution of a program that disrupts the normal flow.

#7 What are checked and unchecked exceptions?

Checked exceptions: Exceptions that can be dealt with (continue to run with some alternative code) are called checked exceptions. All compile time exceptions are checked exceptions. They can be handled by using try/catch and finally blocks or declaring “throws” in a method.

Unchecked exceptions: Exceptions that cannot be dealt with are called unchecked exceptions. All runtime exceptions or errors are unchecked exceptions. They are exceptions that should never happen (a bug in the code that needs a permanent fix).

#8 How are exceptions types classified?

System level exceptions – an error that occurs during system level programming. Usually they are not propagated to Client UI. Example, null pointer exceptions or runtime exceptions which your environment should take care and choose to decide on what needs to be done.

Application level exceptions – exceptions that occurs during application level programming, usually propagated to Client UI. Example, withdraw should not be done due to insufficient funds.

#9 How will you create custom exceptions?

By creating a class extending the java.lang.Exception.

#10 What is the use of this and super keyword?

“this” keyword is used to denote current object. If a class has multiple constructors, calling one constructor from another can be achieved through this keyword.
“super” keyword is used to call the base class constructor

#11 What are marker interfaces?

Marker interfaces are empty interfaces which does not have any method declarations just to convey the JVM that they should be treated differently. They are also called null interfaces. For example, java.io.Serializable is a marker interface.

#12 What is the difference between AWT and Swing components?

* AWT components are heavy-weight components and Swing components are light-weight components.
* Heavy-weight means they are platform dependant components that uses native OS peer classes whereas light-weight components does not depend on the OS. For example, look and feel of AWT button component uses native look and feel of OS whereas Swing button components can have thier own look and feel.

#13 What is the difference between static and non-static variables?

Static variables are associated with a class whereas non-static variables are associated with an instance.

#14 How will you do inter-applet communication?

By using AppletContext interface, inter-applet communication can be performed.

#15 Explain automatic garbage collection in Java? Why GC cannot be forced in Java

Garbage collection is automatically taken care by java as a low priority daemon thread that runs in the background. Thats the reason why GC cannot be forced in Java. Whenever objects are unreachable, that objects finalize() method is called before the object is garbage collected thus thier resources are reclaimed or reused. Referencing an object to null will just make the object eligible for GC.

#16 How will you prevent an object from being unreachable?

An unreachable object can be made reachable by overriding its finalize() method to access any other reachable objects

#17 What is the difference between String and StringBuffer?

String objects are immutable whereas StringBuffer are mutable which makes it faster in concatenation.

#18 What is the difference between == and equals() method?

== is used to compare object references whereas equals() method is used to compare the object contents.

No comments: