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 Page2

     

Q 1. 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 2. 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 3. 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 4. 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");

Q 5. How can I implement the Class.forName() scheme ?
Ans :
To work with Class.forName() we need to 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. To use the Class.forName() of class loading, a class only requires a default constructor i.e. a constructor without arguments. As soon as the class is loaded, then the newInstance() method of the Class object is used to get a new object reference.
 
Q 6. Is Class.forName() runtime polymorphism ?
Ans :
The Class.forName() helps to work with the polymorphic nature of a class. This method  is often used while at the time of  class loading to enable the runtime configuration needed to support an application via property files, servlet configurations and xml  files. However, this kind of run time polymorphism  highly relies over the type of the class specified as a string parameter.

 
Q 7. Why is the String class declared final ?

Ans :
Declaring a class as final means that it is not considered suitable for extension, more specifically if it is a highly specialized class. If an API class is more specialized, it means that its internal behavior may be quite complex and pose un-seen problems for anyone who would create a sub-class.

 
Q 8. How many objects are created for identical strings ?

Ans :
Statements declaring two string creates two separate String object references. Similar to the strings they includes the same hash code and their equals(Object) method returns true, but they are separate object references.

 
Q 9. Is the + operator overloaded ?

Ans :
Java does not supports the concept of operator overloading, while string concatenation is a special case. Applying + operator to a String, appends two values into a new String. The Java interpreter converts primitive values, like int and long, into their equivalent string values and then concatenates the strings.

Q 10. What is a StringTokenizer for ?
Ans :
The class java.util.StringTokenizer is a is a special type of class that support Enumeration in order to represent segments of a string, that may be  separated by one or more "delimiters". Constructing a StringTokenizer having comma as a delimiter identifies each word in a comma separated list for instance.

  
Q 11. Why don't two StringBuffers match ?

Ans :
In order to compare the string contents of each object, the String class overrides the default implementation of the equals(Object) method. The StringBuffer class does not override the equals(Object) method of the superclass Object testing the argument refers to the same object reference.

  
Q 12. How many objects are created for identical strings ?

Ans :
Two separate String object references can be created by declaring two string. Strings having the same hash code and their equals(Object) method returns true, while separating object references.

String str1 = "roseindia";

String str2 = "roseindia";

if (str1.hashCode() == str2.hashCode()) {} // true

if (str1.equals(str2)) {} // true  


Q 13. How can I divert console output to a file ?
Ans :
The field System.out of the java.lang.System class is used to hold a static reference to the console output stream, that is an instance of java.io.PrintStream. The method setOut(PrintStream) of java.lang.System  class enables us to assign a different output to the reference.

 
Q 14. How can I generate an array from a list ?

Ans :
The answer of this question depends on the format of the list, which is perhaps stored in a file ? If a consistent character pattern is used to separate the list items then you can use a StringTokenizer to capture them. Use a FileReader class to create a BufferedReader and then use the readLine() method that acquires the data.