Wednesday, September 3, 2014

Java Interview Questions for Experience and Freshers

                                                         General Interview Questions


Que 1 - Difference between == and equals method ?

Ans : The == operator compares two objects to determine if they are the same object in memory i.e. present in the same memory location. It is possible for two String objects to have the same value, but located in different areas of memory.

== compares references while .equals compares contents.

 public class EqualsTest {

                    public static void main(String[] args) {

                                         String s1 = ?abc?;
                                         String s2 = s1;
                                         String s5 = ?abc?;
                                         String s3 = new String(?abc?);
                                         String s4 = new String(?abc?);
                                         System.out.println(?== comparison : ? + (s1 == s5));
                                         System.out.println(?== comparison : ? + (s1 == s2));
                                         System.out.println(?Using equals method : ? + s1.equals(s2));
                                         System.out.println(?== comparison : ? + s3 == s4);
                                         System.out.println(?Using equals method : ? + s3.equals(s4));
                    }
}
Output
== comparison : true
== comparison : true
Using equals method : true
false
Using equals method : true
Que 2 - What is the difference between an Abstract class and Interface ?
Ans :
1.       Abstract classes may have some executable methods and methods left unimplemented. Interfaces contain no implementation code.
2.       An class can implement any number of interfaces, but subclass at most one abstract class.
3.       An abstract class can have non abstract methods. All methods of an interface are abstract.
4.       An abstract class can have instance variables. An interface cannot.
5.       An abstract class can define constructor. An interface cannot.
6.       An abstract class can have any visibility: public, protected, private or none (package). An interface's visibility must be public or none (package).
7.       An abstract class inherits from Object and includes methods such as clone() and equals().


14.Can overloaded methods be override too?
Yes, derived classes still can override the overloaded methods. Polymorphism can still happen. Compiler will not binding the method calls since it is overloaded, because it might be overridden now or in the future.

15.Is it possible to override the main method?
NO, because main is a static method. A static method can't be overridden in Java.
27.What are the differences between Interface and Abstract class?
Abstract Class
Interfaces
An abstract class can provide complete, default code and/or just the details that have to be overridden.
An interface cannot provide any code at all,just the signature.
In case of abstract class, a class may extend only one abstract class.
A Class may implement several interfaces.
An abstract class can have non-abstract methods.
All methods of an Interface are abstract.
An abstract class can have instance variables.
An Interface cannot have instance variables.
An abstract class can have any visibility: public, private, protected.
An Interface visibility must be public (or) none.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
An abstract class can contain constructors .
An Interface cannot contain constructors .
Abstract classes are fast.
Interfaces are slow as it requires extra indirection to find corresponding method in the actual class.

28.When should I use abstract classes and when should I use interfaces?
Use Interfaces when?
·         You see that something in your design will change frequently.
·         If various implementations only share method signatures then it is better to use Interfaces.
·         you need some classes to use some methods which you don't want to be included in the class, then you go for the interface, which makes it easy to just implement and make use of the methods defined in the interface.
Use Abstract Class when?
·         If various implementations are of the same kind and use common behavior or status then abstract class is better to use.
·         When you want to provide a generalized form of abstraction and leave the implementation task with the inheriting subclass.
·         Abstract classes are an excellent way to create planned inheritance hierarchies. They're also a good choice for nonleaf classes in class hierarchies.

29.When you declare a method as abstract, can other nonabstract methods access it?
Yes, other nonabstract methods can access a method that you declare as abstract.

30.Can there be an abstract class with no abstract methods in it?
Yes, there can be an abstract class without abstract methods.
36.What are the differences between Class Methods and Instance Methods?

Class Methods
Instance Methods
Class methods are methods which are declared as static. The method can be called without creating an instance of the class
Instance methods on the other hand require an instance of the class to exist before they can be called, so an instance of a class needs to be created by using the new keyword.
Instance methods operate on specific instances of classes.
Class methods can only operate on class members and not on instance members as class methods are unaware of instance members.
Instance methods of the class can also not be called from within a class method unless they are being called on an instance of that class.
Class methods are methods which are declared as static. The method can be called without creating an  instance of the class.
Instance methods are not declared as static.
38.What are Access Specifiers?
One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of data in a class and making this class available only through methods. Java allows you to control access to classes, methods, and fields via so-called access specifiers..

39.What are Access Specifiers available in Java?
Java offers four access specifiers, listed below in decreasing accessibility:
·         Public- public classes, methods, and fields can be accessed from everywhere.
·         Protected- protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package.
·         Default(no specifier)- If you do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package.
·         Private- private methods and fields can only be accessed within the same class to which the methods and fields belong. privatemethods and fields are not visible within subclasses and are not inherited by subclasses.
 Situation 
 public 
 protected 
 default 
 private 
 Accessible to class
 from same package? 
yes
yes
yes
no
 Accessible to class
 from different package? 
yes
 no, unless it is a subclass 
no
no

40.What is final modifier?
The final modifier keyword makes that the programmer cannot change the value anymore. The actual meaning depends on whether it is applied to a class, a variable, or a method.
·         final Classes- A final class cannot have subclasses.
·         final Variables- A final variable cannot be changed once it is initialized.
·         final Methods- A final method cannot be overridden by subclasses.
54.Difference between ArrayList and Vector ?
ArrayList
Vector
ArrayList is NOT synchronized by default.
Vector List is synchronized by default.
ArrayList can use only Iterator to access the elements.
Vector list can use Iterator and Enumeration Interface to access the elements.
The ArrayList increases its array size by 50 percent if it runs out of room.
A Vector defaults to doubling the size of its array if it runs out of room
ArrayList has no default size.
While vector has a default size of 10.
69.Difference between HashMap and Hashtable ?
HashMap
Hashtable
HashMap lets you have null values as well as one null key.
HashTable  does not allows null values as key and value.
The iterator in the HashMap is fail-safe (If you change the map while iterating, you?ll know).
The enumerator for the Hashtable is not fail-safe.
HashMap is unsynchronized.
Hashtable is synchronized.
Note: Only one NULL is allowed as a key in HashMap. HashMap does not allow multiple keys to be NULL. Nevertheless, it can have multiple NULL values.
Que - Difference between Class.forName() and ClassLoader.loadClass() ?
Both methods try to dynamically locate and load a java.lang.Class object corresponding to a given class name. However, their behavior differs regarding which java.lang.ClassLoader they use for class loading and whether or not the resulting Class object is initialized.
The most common form of Class.forName(), the one that takes a single String parameter, always uses the caller's classloader. This is the classloader that loads the code executing the forName() method. By comparison, ClassLoader.loadClass() is an instance method and requires you to select a particular classloader, which may or may not be the loader that loads that calling code. If picking a specific loader to load the class is important to your design, you should use ClassLoader.loadClass() or the three-parameter version of forName() added in Java 2 Platform, Standard Edition (J2SE): Class.forName(String, boolean, ClassLoader).
Additionally, Class.forName()'s common form initializes the loaded class. The visible effect of this is the execution of the class's static initializers as well as byte code corresponding to initialization expressions of all static fields (this process occurs recursively for all the class's superclasses). This differs from ClassLoader.loadClass() behavior, which delays initialization until the class is used for the first time.
You can take advantage of the above behavioral differences. For example, if you are about to load a class you know has a very costly static initializer, you may choose to go ahead and load it to ensure it is found in the classpath but delay its initialization until the first time you need to make use of a field or method from this particular class.
The three-parameter method Class.forName(String, boolean, ClassLoader) is the most general of them all. You can delay initialization by setting the second parameter to false and pick a given classloader using the third parameter. I recommend always using this method for maximum flexibility.

No comments:

Post a Comment