In this post you will know 22 most asked core Java interview Questions and answers for java developers who are having 1 to 4 years of experienced.
When you are attending for Java interview core java interview questions plays important role no matter you are fresher or mid level developer or senior java developer. This technical round decides outcome of your interview.
1. Why you override hashcode along with equls() method?
2. How to prevent a class from being sub classed in java?
When a final modifier is used with a Class then the class cannot be extended further. This is one way to protect your class from being sub classed and often sensitive classes are made final due to security reason. This is also one of the reasons why String and wrapper classes are final in Java.
Want see all answers then watch this video:
3. Can we override static method?
No, you cannot override static method in Java because method overriding is based upon dynamic binding at runtime and static methods are bonded using static binding at compile time. Though you can declare a method with same name and method signature in sub class which does look like you can override static method in Java but in reality that is method hiding.
4. Can we access private method in Java?
Yes, we can access private method in the same class but not outside the class.
5. Can you explain final keyword?
Final is a special keyword it can be used in different context such as:
When the final keyword is used with a variable then its value can’t be changed once assigned.
When a method is declared final then it can’t be overridden by the inheriting class.
When a class is declared as final in Java, it can’t be extended by any subclass class but it can extend other class.
6. Difference between checked Exception and unchecked Exception
Checked Exception: The exceptions which are checked by the compiler for smooth excetuon of the program at runtime is called checked exception:
It means if a method is throwing a checked exception then it should handle the exception using try-catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error.
• Ex: SQLException
Unchecked Exception: The exceptions which are not checked by the compiler are called unchecked Exception.
7. How to fix concurrentModificationException?
8. Difference between abstract class and interface?
9. When to use abstract class and interface in java?
10. Difference Btw method overloading and overriding in java?
11. Difference between encapsulation and abstraction?
12. Have you applied object cloning in your project?
Object cloning in Java is the process of creating an exact copy of an object. It basically means the ability to create an object with a similar state as the original object. To achieve this, Java provides a method clone() to make use of this functionality. This method creates a new instance of the class of the current object and then initializes all its fields with the exact same contents of corresponding fields.
13. Difference between HashTable and HashMap
14. When to use Transient variable?
when you want to make a variable non-serializable in a class, which implements the Serializable interface. In other words, you can use it for a variable whose value you don't want to save.
15. Tell me one example when to use ArrayList and when to use LinkedList?
Arraylist is best choice is if our frequent operation is retrival that means get(int) or when search operation need to be performed.
Whereas Linkedlist is best choice if our frequent operation is insert and delete operations in the middle. Since it does not need several shift operation internally.
16. Diff b/w StringBuffer and StringBuilder
Every method present in StringBuffer is synchronized that means at a time only one thread is allowed to operate on StringBuffer object, hence StringBuffer obj is ThreadSafe whereas no method present in stringbuilder is synchronized that means multiple threads are allowed to operate simulountesly hence stringbuffer is not thread safe.
Stringbuffer increases waiting time of threads and hence performance is low whereas threads are not req to hold and hence performance is high.
17. How to create custom exception? Or user defined exception
To create user defined exception we need to write subclass simply extending the java Exception class and you can override toString()function to display your customize message on catch.
In the Exception class provides methods to show error message to the end users. They are
While you want to develop user defined exception in your programming project no need to override any of the above methods which are available in the Exception class in the sub class. Based on your project requirement you have to customize your message to show end users.
Simply says You can Override toString() method to display custom message.
You must extend the Exception class to create custom exceptions
18. When we should go for join() method?
If a thread wants to wait until completing some other thread then we should go for join() method. Join() method throws interruptedexception which is checked exception, hence we should handle either try-catch or throws keyword.
19. What type of exceptions faced in your project and how you resolve it?
I have faced different types of exceptions but some of the exceptions like nullpointerexceptions, concurrentmodification exception and illegalmonitorstateexception.
Wait(), notify() and notifyall() methods are available in Object class but not in Thread class becaz thread can call these methods on any common object.
To call wait(), notify() and notifyall() methods compulsory the current thread should be owner of that object that is current thread should has lock of that object that is current thread should be synchronized area. Hence we can call wait(), notify() and notifyall() methods only from synchronized are otherwise will get Runtime exception says Illegalstatemonitorexception.
Tell me some java8 features with example
Java 8 has been one of the biggest releases after Java 5 annotations and generics. Some of the important features of Java 8 are:
1. Interface changes with default and static methods
2. Functional interfaces and Lambda Expressions
3. Java Stream API for collection classes
4. Java Date Time API
20. Why we need generics? Have you used generics in Your Project?
21. Difference between Loose coupling and tight coupling?
22. Difference between BufferReader and Scanner in Java? which one prefer?
23. Explain collection conepts with realtime examples?
24. Exalain oops concepts with real time examples
25. What is singleton class in Java?
26. Difference between ClassNotFoundException and NoClassDefFoundError
27. Difference between constructor and method
28. Explain public static void main()
29. Difference between Heap Memory and Stack
30. How to explain Spring MVC Architecture
31. How to explain project architecture?