50+ Basic Core Java Interview Questions with Hints and Answers

If you are preparing for your next Core Java interview and looking for some common question to practice or check your knowledge, then you have come to the right place. In this article, I'll share 50 Core Java Interview Questions from various companies. I have already discussed the answers to these questions in this blog or Javarevisited, so I have just put the link. First, you should try to answer it yourself and if you cannot then go to the link and find the answer. You can also compare your answer with mine and learn a few things here and there. In most of my interview post, I have also shared some common techniques to drive interview towards your comfort zone and how to leave a long-lasting impression on the interviewer, you can use those techniques to do well on your Java interviews.

These questions are useful for both freshers and experienced Java programmers, mostly 2 to 5 years of experience. It also covers all important topics for core Java interviews, e.g. Java Fundamentals, Java Collections Framework, Java Multithreading and Concurrency, Serialization, Design Patterns, and JVM and Classloading.

Core Java Interview Questions with Answers

Without any further ado, here is my list of 50 Core Java Interview Questions with Answers as a link:

1)  How Java achieves platform independence? (answer)
hint: bytecode and Java Virtual Machine

2)  What is ClassLoader in Java? (answer)
Hint: part of JVM which loads bytecodes for classes. You can write your own.

3)  Write a Java program to check if a number is Even or Odd? (answer)
hint: you can use bitwise operator, e.g. bitwise AND &, remember, even number has zero at the end in binary format, and an odd number has 1 in the end.

4)  Difference between ArrayList and HashSet in Java? (answer)
hint:  all differences between List and Set are applicable here, e.g. ordering, duplicates, random search, etc.

5)  What is double-checked locking in Singleton? (answer)
hint: two-time check whether instances is initialized or not, first without locking and second with locking.

6)  How do you create thread-safe Singleton in Java? (answer)
hint: many ways, e.g. using Enum or by using double-checked locking pattern or using nested static class.

7)  When to use a volatile variable in Java? (answer)
hint: when you need to instruct JVM that a variable can be modified by multiple threads and give a hint to JVM that does not cache its value.

8)  When to use a transient variable in Java? (answer)
Hint:  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.

9)  Difference between the transient and volatile variable in Java? (answer)
hint: totally different, one used in the context of serialization while other is used in concurrency.

10) Difference between Serializable and Externalizable in Java? (answer)
hint: Externalizable gives you more control over the Serialization process.

11) Can we override the private method in Java? (answer)
hint: No, because it's not visible in the subclass, a primary requirement for overriding a method in Java.

Top 50 Core Java Interview Questions with Answers12) Difference between Hashtable and HashMap in Java? (answer)
Hint: several but most important is Hashtable is synchronized while HashMap is not. It's also legacy and slow as compared to HashMap.

13) Difference between List and Set in Java? (answer)
hint: List is ordered and allows duplicate, Set is unordered and doesn't allow duplicate elements.

14) Difference between ArrayList and Vector in Java (answer)
Hint:  Many, but most important is that ArrayList is non-synchronized and fast while Vector is synchronized and slow. It's also legacy class like Hashtable.

15) Difference between Hashtable and ConcurrentHashMap in Java? (answer)
hint: more scalable

16) How does ConcurrentHashMap achieve scalability? (answer)
hint: by dividing the map into segments and only locking during the write operation. You can further see The Complete Java Masterclass to learn more about how ConcurrentHashMap works.

Basic Core Java Interview Questions with Answers

17) Which two methods you will override for an Object to be used as Key in HashMap? (answer)
hint: equals and hashcode

18) Difference between wait and sleep in Java? (answer)
hint: The wait() method releases the lock or monitor, while sleep doesn't.

19) Difference between notify and notifyAll in Java? (answer)
Hint: notify notifies one random thread is waiting for that lock while notifyAll inform to all threads waiting for a monitor. If you are certain that only one thread is waiting, then use notify, or else notifyAll is better.

20) Why you override hashcode, along with equals() in Java? (answer)
hint: to be compliant with equals and hashcode contract which is required if you are planning to store your object into collection classes, e.g. HashMap or ArrayList.

21) What is the load factor of HashMap means? (answer)
hint: The threshold which triggers re-sizing of HashMap, generally 0.75, which means HashMap resize itself if it's 75% full.

22) Difference between ArrayList and LinkedList in Java? (answer)
Hint: same as an array and linked list, one allows random search while other doesn't. Insertion and deletion easy on the linked list, but a search is easy on an array.

23) Difference between CountDownLatch and CyclicBarrier in Java? (answer)
hint: You can reuse CyclicBarrier after the barrier is broken, but you cannot reuse CountDownLatch after the count reaches to zero. You can further see Multithreading and Parallel Computing in Java course on Udemy to learn more about concurrency utility classes like CyclicBarrier, Phaser, and CountDownLatch in Java.

Core Java Interview Questions with Answers with hints

24) When do you use Runnable vs. Thread in Java? (answer)
hint: always

25) What is the meaning of Enum type-safe in Java? (answer)
Hint: It means you cannot assign an instance of different Enum type to an Enum variable. e.g., if you have a variable like DayOfWeek day, then you cannot assign it value from DayOfMonth enum.

26) How does Autoboxing of Integer work in Java? (answer)
hint: using valueOf() method

27) Difference between PATH and Classpath in Java? (answer)
hint: PATH is used by the operating system while Classpath is used by JVM to locate Java binary, e.g. JAR files or Class files.

28) Difference between method overloading and overriding in Java? (answer)
Hint: Overriding happens at subclass while Overloading happens in the same class. Also, overriding is a runtime activity while overloading is resolved at compile time.

29) How do you prevent a class from being sub-classed in Java? (answer)
hint: just make its constructor private

30) How do you restrict your class from being used by your client? (answer)
hint:  make the constructor private or throw an exception from the constructor

31) Difference between StringBuilder and StringBuffer in Java? (answer)
hint: StringBuilder is not synchronized while StringBuffer is synchronized.

32) Difference between Polymorphism and Inheritance in Java? (answer)
hint: Inheritance allows code reuse and builds the relationship between class, which is required by Polymorphism, which provides dynamic behavior.

33) Can we override static method in Java? (answer)
hint: No because overriding resolves at runtime while static method call is resolved at compile time.

34) Can we access the private method in Java? (answer)
hint: yes, in the same class but not outside the class

35) Difference between interface and abstract class in Java? (answer)
hint: from Java 8, the difference is blurred, but still a Java class can implement multiple interfaces but can extend just one class.

36) Difference between DOM and SAX parser in Java? (answer)
Hint: DOM loads whole XML File in memory while SAX doesn't. It is an event-based parser and can be used to parse a large file, but DOM is fast and should be preferred for small files.

37) Difference between throw and throws keyword in Java? (answer)
hint: throws declare what exception a method can throw in case of error but throw keyword actually throws an exception.

38) Difference between fail-safe and fail-fast iterators in Java? (answer)
hint: fail-safe doesn't throw ConcurrentModificationException while fail-fast does whenever they detect and outside change on underlying collection while iterating over it.

39) Difference between Iterator and Enumeration in Java? (answer)
hint: Iterator also gives you the ability to remove an element while iterating while Enumeration doesn't allow that.

40) What is IdentityHashMap in Java? (answer)
hint: A Map which uses  == equality operator to check equality instead of equals() method.

41) What is String pool in Java? (answer)
Hint: A pool of String literals. Remember it's moved to heap from perm gen space in JDK 7.

42) Can a Serializable class contain a non-serializable field in Java? (answer)
hint: Yes, but you need to make it either static or transient.

43) Difference between this and super in Java? (answer)
hint: this refers to the current instance while super refers to an instance of the superclass.

44) Difference between Comparator and Comparable in Java? (answer)
hint: Comparator defines custom ordering while Comparable defines the natural order of objects, e.g. the alphabetic order for String.

45) Difference between java.util.Date and java.sql.Date in Java? (answer)
hint: former contains both date and time while later contains only date part.

46) Why wait and notify method are declared in Object class in Java? (answer)
hint: because they require lock which is only available to an object.

47) Why Java doesn't support multiple inheritances? (answer)
Hint: It doesn't support because of a bad experience with C++, but with Java 8, it does in some sense. Only multiple inheritances of Type is not supported in Java now.

48) Difference between Checked and Unchecked Exception in Java? (answer)
hint: In case of checked, you must handle exception using catch block, while in case of unchecked, it's up to you, compile will not bother you.

49) Difference between Error and Exception in Java? (answer)
hint: I am tired of typing, please check the answer

50) Difference between race condition and deadlock in Java? (answer)
hint: both are errors occur in a concurrent application, one occurs because of thread scheduling while others occur because of poor coding.

Java Interview Guide: 200+ Interview Questions and Answers
Spring Framework Interview Guide - 200+ Questions & Answers
Preparing For a Job Interview By John Sonmez
Java Programming Interview Exposed by Markham
Cracking the Coding Interview - 189 Questions and Answers
Data Structure and Algorithms Analysis for Job Interviews

Thanks for reading this article so far. If you like these Core Java Questions, then please share with your friends and colleagues. If you have any questions or feedback, then please drop a note.

1 comment:

  1. I hope that Answer for the question 29 is Wrong , When we declare a class as Final, We cannot inherit the class . This is a Simple solution for the question . There is no need for "Private" Constructors as We cannot inherit Constructors .


Feel free to comment, ask questions if you have any doubt.