Typically in all rounds of Java interview in any organization, the first round should be in core Java only. To face the first round interview, you should know the basic core Java interview questions and answers. Here is the list we are providing the frequently asked core Java interview questions and answers.
Core Java Interview Questions :
Irrespective of a fresher or senior professional, we sort out some of the frequently asked core Java interview questions and answers here. To make the answers reliable and more technical, we are using some technical terms, if you feel any difficulty to understand please don’t hesitate to comment. If you need any further answers, we always welcome you to ask a question in the comment section.
1. Difference between JVM, JRE, JDK.
This is one of the basic Java interview questions, you should aware of this question.
The JVM is considered to be a virtual microprocessor on which the Java programs will be interpreted to an assembly language.
The JDK is considered to be a kit, using which a developer can write his own business logic in Java. It includes Java built-in API.
JRE is considered to be the environment, on which the Java applications are running.
2. What is an abstraction in Java?
Abstraction is considered to be providing an indirect way of access to the actual data (properties) with the help of behaviours to prevent the object to move into an invalid state.
3. What is polymorphism?
Polymorphism is the ability of an object to behave differently for different inputs. One acting as multiple is considered to be polymorphism, there exist only thing or one person and taking the work of multiple things or multiple persons is called polymorphism. The most common use of polymorphism in OOPs is when we assign a child class object to parent class reference variable. This is also called as Dynamic Method Dispatch.
4. Compile Time polymorphism
Method overloading is considered to be for compile-time polymorphism i.e. based on the input provided to a method the way it executes and the output it returns will vary. In simple terms, we can say that a class can have multiple methods with the same name and a different number of parameters or different types of parameters is called method overloading.
5. Difference between == and equals method
This is also one of the most important questions in all core Java interview questions. == is considered to be an operator to compare any two primitives or any two references whereas equals are considered to be a method to check the equality between the states (the values associated with the properties) of two different homogeneous objects or objects of the same type.
6. Object Cloning
It is considered to be a technique of preparing a new object as same as that of an existing object i.e. the states of the two objects (Existing and new) will be the same.
7. What is immutability?
Immutability means preventing any changes to the existing one i.e. immune to any modification for an existing one. We can achieve immutability in Java using final keyword.
8. What is Immutable objects in Java?
The string is considered to be an immutable object i.e. it won’t allow changing its state after creation of an instance for it. If we try to change the value of String by executing behaviour on it, a new instance will be created in the memory rather than changing the state.
9. Ways to make a class Runnable.
By implementing the Runnable interface any class can be made as Runnable object.
10. What is serialization?
It is a common interview question out of all core Java interview questions. Saving or storing the state of an object on to a secondary storage device is called serialization. Those objects whichever going to be transferred over a network is supposed to serialized.
11. What is static in Java?
It is one of the most important core Java interview questions for all, but you should be in a position to explain in detail and perfect manner, otherwise it leads to going interview in-depth on Java memory allocations.
If we found any characteristic feature not belonging to any specific object, rather it is belonging to all the objects derived from the class are supposed to be declared as static i.e. that characteristic feature is belonging to the entire class, not to any individual obj.
For More Details..
12. Difference between throw and throws
This is also most frequently asked core Java interview questions forever.
The compiler won’t allow you to compile your code if there is any possibility of raising an exception. You know that your code may raise an exception and but you don’t know how to handle the exception. In that case, you need to intimate the compiler, as you do not handle this exception.
So to intimate the compiler to allow the code to execute and the responsibility of handling the exception handed over to the user of my code by using the throws keyword.
An exception can be raised in your code due to some exception of others, so rather than taking the responsibility of handling the exception into your hands, you just throw the exception to the responsible handler with appropriate info.
13. What is finally?
Finally is a block in exception handling. It executes irrespective of exception raised or not, whereas catch is also a block it executes if and only if the exception is raised. Usually, finally blocks are used to nullify the objects and closing the streams.
14. Can we override the main method?
This is something tricky core Java interview question. The answer is No, we can’t override the main method i.e. public static void main (String [] args) {} as static methods will not be inherited. So there is no concept of overriding, whereas instance methods with the name main can be overridden according to the principles of method overriding as like normal methods.
But another answer is Yes, We can override the main method but it is called a method hiding in Java.
15. Can static methods be overridden?
Static methods are not inherited into the subclass, so there is no such concept of overriding those methods.
16. Difference between an interface and an abstract class
This is also one of the most important questions in all core Java interview questions to test your OOPs knowledge. An interface is considered to be the contract established between any two communicating entities whereas an abstract class is considered to be a virtual or an incomplete entity.
E.g. P1, P2 are assigned with different works and P2 can complete his work only if P1 completed the work. So rather than keeping P2 in waiting till P1 completes his work, P1 and P2 will prepare an agreement or contract that P1 must provide to p2 is considered to be an interface.
E.g. consider the cricket team an example, where each individual is considered to a player. In reality, there exists a batsman, bowler, fielder, Wicket keeper and all the rest will be there and all are considered to be players.
17. Difference between a class and an object
This is a basic core Java interview question for all. If your fresher, you can expect this question.
A class is considered to be the blueprint or a plan from which the objects are deriving.
An object is considered to be a real-world entity with some characteristic features (properties) and offers some services (behaviours) to the world by changing its states on executing the services.
18. What is the need to overriding the “equals()” and “hashCode()” methods in a class or the contract between “equals()” and “hashCode()” methods?
If two objects are supposed to be compared those two need to be of the same type. (We can compare two chairs and we can’t compare a water bottle and a chair). The two objects can be compared by means of some characteristic features of those objects, and the “equals()” method also used to compare the two objects for their equality. The “equals()” method internally uses the hash code for comparison and this is considered to be the contract between the “equals” and “hashCode()” methods.
19. What is an exception?
An exception is considered to be a statement or set of statements which may cause the execution of a program or application to terminate abruptly.
20. Difference between an exception and an error.
An exception is a statement or set of statements can cause the program or application to terminate abruptly and we can recover from an exception by handling it appropriately, whereas we can’t recover from an error i.e. an error can’t be handled programmatically we can just detect the cause of the error.
21. Ways to create threads in Java
There are two ways of creating threads in java. The first way is by extending a Thread class and the second way is by implementing the Runnable interface.
22. What is hashing?
Hashing is considered to be a technique where the required item or the element in the data structure or collection can be retrieved at a first retrieval or search.
Hashing is completely worked on a function called a hash function. The hash function requires a key to search for a value in the collection. If the value can be retrieved at the first trail then it is called as an ideal hash function.
23. What is thread join?
Thread join is considered to be a process of waiting for a thread to wait for another thread to complete its execution. If there is a parent and child thread hierarchy and a child thread holding any resources with it then it’s the responsibility of the child thread to submit those resources to its parent. So the parent needs to wait for the child thread to finish its execution by applying a join.
24. How do we return a value from a thread?
We can return a value from a thread by just implementing a callable interface. The call method in the callable interface has the ability to return an object.
25. What is a daemon thread?
The daemon thread is considered to be a thread i.e. executing infinitely and independently in background.
26. What is considered to be the best way of creating a thread in Java?
Generally, people prefer implementing a runnable or callable interface to extending a thread class as it provides a way to make an existing object can be converted to a thread by extending that object and implementing the runnable or callable interface.
27. How many memory locations are there in Java?
There are five different memory locations in java and are considered to be
-
Stack Memory
-
Heap Memory
-
Static Pool
-
Constant Pool
-
Non-RAM Storage
28. Can we make a private constructor in Java?
Yes, we can define a private constructor in Java, if we are not interested in proving the ability to create an object from outside of the class then we can do it by declaring a constructor as private.
29. Can we define a constructor in abstract class?
Yes, we can define a constructor in an abstract class but we can’t instantiate that abstract class with that constructor. If we want to get access the abstract methods or properties, we are free to extend the abstract class and by creating the object of extended class, we can access the abstract class.
30. How to load a class at runtime?
A class can be loaded at runtime with the help of a “loadClass” method of “ClassLoader” class, the fully qualified class name of the class going to br loaded need to be passed as a parameter.
31. How to define an immutable array list in java?
With the help of a method called “unmodifiableList” method of “Collections” class, we can make an array list as an un-modifiable array list.
32. Difference between an interface and an object.
An object is considered to be a real-world entity whereas the interface is considered to be a contract between any two real-world entities or objects.
33. How to remove duplicates from the list?
This is a repeatedly asked collections interview questions. To test your knowledge on collection framework. The answer is simple. We can remove the duplicate values from List by assigning the List to Set.
[highlight color=”blue”]Example : Set set = new HashSet(list);[/highlight]
34. What is the need to tree set?
The purpose of the tree set is to place the elements into the set based on their nature sorting order without allowing any duplicates and null values.
The insertion and deletion and modification will lead to the recreation of the tree to keep the sorting order.
35. What is the need for a user-defined exception?
Java API can’t handle the exceptions that are going to be raised in the user-defined business logic and they are supposed to be handled by the programmer itself with the help of user-defined exceptions.
36. Explain about deadlock state.
Assume A and B are considered to be two different threads that are executing concurrently. Say “A” needs an object “P” to finish its execution which is locked by “B” and “B” needs an object “Q” to finish its execution which is locked by “A”. So both will wait for an infinite time for their resources is called a deadlock state.
37. What is a thread?
A thread is considered to be a sequential and independent path of execution to perform an independent task.
38. How two threads will communicate with each other?
Two threads can communicate with each other with the help of piped input stream and piped output stream classes.
39. Methods in the object class
The following are considered to be the methods that are in the object class.
equals(), hashcode(), clone(), wait(), notify(), notifyAll(), toString(), and getClass().
40. Can we override the start method in the Thread class?
No, we can’t override the start method of the Thread class. It’ll take the responsibility of communication with the operating system to place the business logic inside the ready queue and is considered to be java API built-in impl.
We will update all core Java interview questions for day by day. You can find all questions here. Please comment if you need any more questions, we are here to answer you.
All the BEST !!