Corejava Interview,Corejava questions,Corejava Interview Questions,Corejava

This page discusses - Corejava Interview,Corejava questions,Corejava Interview Questions,Corejava

Corejava Interview,Corejava questions,Corejava Interview Questions,Corejava

Core Java Interview Questions Page1

     

Q 1 : How should I create an immutable class ?
Ans:
An immutable class is a class to which values assigned to the variables can not be altered because they are declared as final, all the variables must be assigned in the constructor. As per the specification declared in java document an immutable class should not contain any modifier method. But a developer should be careful while using an immutable class that the constructor and accessor methods should not contain references of mutable fields.
     
Q 2 : Is println overloading or overriding ? 
Ans :
The println() method is the method of the PrintWriter  class which is an example of overloading because PrintWriter class includes several methods having the same name and same return type. E.g. the methods such as println(boolean), println(int) and println(String) have the same basic method name as "println", and also have same return type as void, but only method signature varies according to the type of the arguments passed, that is sufficient for the java interpreter in identifying the appropriate method in order to call at runtime. 
       
Q 3 : What is dynamic method dispatch ? 
Ans :
Dynamic method dispatch is the mechanism that is used by Java runtime system in order to determine which method implementation to call in an inheritance hierarchy. E.g. the method toString() is the method of Object class that is inherited by all the subclasses, while the String class overrides this method in order to return its string content. If an object refers to a String or other type by using application logic then the java compiler does not have knowledge in advance where a class to the toString() method is resolved but it must be determined dynamically at runtime.
  
Q 4 : How can I call a constructor from a constructor ? 

Ans :
When a class have a number of constructors then calling to these constructors by using this() takes place in the same way as super class constructor
super(). Lets take an example, Suppose you have a "good citizen" that takes a String and a boolean and a shorthand version that only receives a String then you may pass a default value to the two argument constructor.
   
Q 5 : What if two interface methods clash in implementation ? 
Ans :
Suppose there are two interfaces having the same method signature regardless of any other intentions. A concrete class implementing both interfaces can only provide one implementation of a given method signature that avoids ambiguity for the java compiler deal with such type of situations. Whenever the condition arises in which two interface methods having same method signatures and intended behavior then it would be preferable to rename one of the interface methods indicating a more distinct purpose.
      
Q 6 : Why use interfaces to develop Java applications ?
Ans :
Interfaces should be used in large applications because it allows the whole system to modify easily and also extending and integrating new features. To start with, a given interface may have only a single implementation, but in special circumstances where slightly different behavior is required, you only require to write a class implementing to one of the existing interfaces that may be dropped in place without requiring major modifications. Interfaces also enables to a developer to use a class from different hierarchy to work in an existing application. The class only requires to specify that it implements the interface that provides necessary methods in such a way as if it were created for the job.
    
Q 7 : Can we create an object for an interface ? 
Ans :
Yes an interface always requires to create an object that implements the given interface. Interfaces are not allowed to instantiate in their own right, therefore it is required to write a class that implements the given interface by implementing all the methods declared in that interface.
  
Q 8 : Can an interface extend an abstract class ? 
Ans:
Java does not allow an interface to extend an abstract class. An interface can only extend a super-interface (i.e. an interface which comes above in the hierarchy). Java programming language allows an abstract class to implement an interface. It separates interfaces and classes regarding inheritance that only come together when a class implements an interface, the reverse in not true.
   
Q 9 : What is a marker interface ? 

Ans :
In java programming language,
Marker interfaces are those interfaces that don't have any method, but signify their compatibility with certain operations. java.io.Serializable, java.lang.Clonable, java.util.EventListener interfaces are some of the examples of typical marker interfaces. E.g. marker interface java.io.Serializable does not contain any method but the class that implement to this interface have the functionality of serializing and de-serializing to their objects.
  
Q 10 : How does Class.forName(dbDriver) work with DriverManager.getConnection ?

Ans :
The method forName() is a static method of the class Class. This method instantiates a class and reduces hard coded dependencies of any java applications. Most of the user are well known about the database driver that is intend to use while writing code first, but reconfiguring for a different database product does not require re-writing the client application if you are using a string variable for your class name.
    
Q 11 :  What happens to singletons when two JVMs are running ?

ANS
:  A singleton class is used only at those places where the user requires only one instance of a class to be operating through out the application. 
*  It is mostly used in the multi-threaded environments.
*  They often work as global variables permitting the allocation and initialization whenever required. 

Benefits of using a singleton class:  

Singleton class provides 
*  access only to the unique instances,
*  permits a variable number of instances, 
*  provides more flexibility than class operations. 
*  it reduces name space, and 
*  allows refinement of operations and representations, 

A singleton class ensures that only one instance is created for the JVM running currently.
A Singleton class have a private default constructor, this restricts the direct instantiation of that class by any other class.

In a single JVM there must be only one instance of a true singleton class.
If we have 2 JVMs running simultaneously, Then there must exist 2 independent instances of a singleton class.
If these two singletons accesses to the common system resource, then there are chances of occuring the conflicts between the two systems.
  
Q 12 : What is a factory method ?
Ans
:  In java programming language the factory methods are static constructor methods which are required to return the instances of the native class. One can say that a factory is a sort of a constructor that is required to produce a number of multiple kind of objects. This requires the user to implement the factory method as a static method, rather than a simple constructor. The factory methods can?t be directly overriden.

The return type of a this method is an interface or superclass type. This is to give the liberty to govern the actual class that is returned through polymorphism. 

Few of the examples of the factory methods in the JDK are :

  • Pattern.compile
  • LogManager.getLogManager
  • Calendar.getInstance
  • Collections.unmodifiableCollection, Collections.synchronizeCollection
Few of the common factory methods include valueOf() and getInstance() e.g.

public class ComplexNo {  
  public static ComplexNo valueOf(float Real, float Imaginary) {
    return new ComplexNo(Real, Imaginary);
  }  
  private ComplexNo(float Real, float Imaginary) {
    qReal = Real;
    qImaginary = Imaginary;
  }
  private float qReal;
  private float qImaginary; 
} 


Q 13 : When should I use the abstract class rather than an interface ?
ANS
A Java interface is an abstract data type like a class having all its methods abstract i.e. without any implementation. That means we cannot create objects of an interface. Typically, an interface in java programming language consists of one or more abstract methods that must be implemented by a class to confirm to the type. 

public interface A{

abstract_method1();

abstract_method2();

}


Similarly an abstract class is restricted by the compiler to get instantiated as it may contain one or more abstract methods which violates the essential things needed to create an object

abstract class A{
 public abstract abstractmethod();
 
 void show(){
 System.out.println("This is an abstract class");
 }
 }

Interfaces Vs Abstract classes

Few of the differences between Interface and abstract classes are:

  Features    Interface    Abstract Class
  Methods An interface contains all the methods with empty implementation. An abstract class must have at least one method with empty implementation.    
   Variables The variables in the interfaces are final and static by default. An Abstract class may contain final as well as static variables but the user needs to mention explicitly.
  Multiple   Inheritance  In java programming language, multiple inheritance is achieved with the help of the interfaces. This is achieved by implementing more than one interface at a time. Abstract classes does not provide this functionality.
They just allow to introduce the single inheritance in java programming language.
 Additional Functions   If we try to add a method to an interface then we need to implement this method in all the subsequent classes which have implemented this interface. In an abstract class we can add a method with default implementation and then we can use it by extending the abstract class. 


Q 14 : What is hashcode? When is hashCode() used ?

Ans :  It is always faster to sort out the things with the numerically keys rather than going for a long alphabetic key, same thing is followed with the hashcode. A hashCode is a technique or a mechanism of computing a numeric key from a long String. The hash code speeds up the search process among the multiple strings, so there lies a greater chance of being different from other instance. There is a chance that we have two different Strings having the same hashCode in that case we use the equals method to make an exact match.
     
Q 15 : How can I get a class reference without using new operator ?
Ans :
We mostly use the new keyword to construct the objects. However in few conditions we need to get a class reference in absence of the new operator. We do it when we require to load a class in memory at runtime, though we are not aware of the class to be loaded in advance. In such cases, what we do is to just configuring name of the required class in the properties file and then we allow to load it with the help of the the static Class.forName(String) method.
     
Q 16 : What is the return type for the Class.forName(String) method ?

Ans : Always the  method Class.forName(String) returns a Class object reference. Some times we need to get a class reference in absence of the new operator to construct an object.  So we do it when we require to load a class in memory at runtime, though we are not aware of the class to be loaded in advance. In such circumstances, we just configure the name of the required class in the properties file and then we allow to load it with the help of the the static Class.forName(String) method. The user is required to use the newInstance() method on the class reference then only he can obtain an instance of the named class.

Example of using Class.forName(String) in the jdbc code

Class.forName("jdbc.DriverABC");
Connection con = DriverManager.getConnection(url,
  "myLogin", "myPassword");