Here I have categorized core java interviews questions and answers for fresher and experience developer in Basics of core java interview questions, OOPs interview questions, String Handling interview questions, Multi-threading interview questions, collection interview questions, JDBC interview questions etc.
1- Why main() in java is declared as public static void main?
Why public? In java public is access modifier and it using when we want to method from any where like any native library JNDI, etc. So that is reason main method is public, it can be accessible everywhere and to every object which may desire to use it for launching the application.
Why static? In java static is a keyword and it tells the compiler that particular entity belongs to a class and should be loaded once the JVM starts. So static things we could invoke without creating instance of that class. Lets suppose we do not have main method as static. Now, to call any method you need an instance of it. Now how to create instance of class which have main method and which one should be used and from where the parameters for overloaded constructors will come.
Why void? In void use before the method definition its mean this method is not returning any to the caller of method. But main method is invoked by JVM so there is no use of returning any value to JVM. The only thing application would like to communicate to invoking process normal or abnormal termination. This is already possible using System.exit(int). A non-zero value means abnormal termination otherwise everything was fine.
2- Does Java Pass by Reference or Pass by Value?
Java is pass by value and not pass by reference i.e. Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But if you change the reference inside method, original reference will not get change. If it was pass by reference, then it would have got changed also. Well, primitive types are always pass by value without any confusion. But, the concept should be understood in context of method parameter of custom types.
3- Difference between interfaces and abstract classes?
This is very common question in core java interview for entry level programmer so let's discuss some important point of difference as below:
- Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods.
- Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
- Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
- Abstract class can have static methods, main method and constructor. Interface can't have static methods, main method or constructor.
4- What is Marker interface?
Marker Interface in java is an interface with no fields or methods. It is used to instruct to the JVM for specific task or special behavior for those classes whose implanting marker interface. Marker interface is also called tag interface by some java developers.
Actually marker interface is nothing but it is a design pattern in computer science by using any languages we can implement ad that provide run-time type information about objects for specific behavior. It provides run time metadata to associate class and it actual implementation different from the initial implementation like generating proxies around classes. In java, it is used as interfaces with no method specified.
5- How to create a immutable object in Java?
Immutable class is a class which once created, it’s contents can not be changed. Immutable objects are the objects whose state can not be changed once constructed. e.g. String class. An immutable class is one whose state can not be changed once created. There are certain guidelines to create an class immutable.
6- Difference between wait() and sleep()?
Major difference between yield and sleep in Java is that yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution. The yielded thread when it will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent. Yield method doesn’t guarantee that current thread will pause or stop but it guarantee that CPU will be relinquish by current Thread as a result of call to Thread.yield() method in java.
7- What is the use of the finally block?
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
8- Why there are two Date classes; one in java.util package and another in java.sql?
In java there are two Date classes one in different packages (util & sql), they have different implementation according to different requirements. A java.util.Date represents date and time of day but a java.sql.Date only represents a date. But sql package have one more class Time to calculate the time of day. The java.sql.Date is a subclass of java.util.Date and it is used with JDBC. It was intended to not have a time part, that is, hours, minutes, seconds, and milliseconds should be zero but this is not enforced by the class. So, what changed in java.sql.Date:
- toString() generates a different string representation: yyyy-mm-dd
- a static valueOf(String) methods to create a date from a string with above representation
- the getters and setter for hours, minutes and seconds are deprecated
9- What is the difference between creating String as new() and literal?
When we create String with new() it’s created in heap and also added into string pool, while String created using literal are created in String pool only which exists in Perm area of heap.
10- When do you override hashCode() and equals()?
hashCode() and equals() methods have been defined in Object class which is parent class for java objects. For this reason, all java objects inherit a default implementation of these methods.
hashCode() method is used to get a unique integer for given object. This integer is used for determining the bucket location, when this object needs to be stored in some HashTable like data structure. By default, Object’s hashCode() method returns and integer representation of memory address where object is stored. equals() method, as name suggest, is used to simply verify the equality of two objects. Default implementation simply check the object references of two objects to verify their equality.