Hello guys, If you are preparing for your next Core Java interview and looking for some common questions 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 and given hint or mentioned the key point you need to know to answer these questions. 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 posts, I have also shared some common techniques to drive an 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, like Java Fundamentals, Java Collections Framework, Java Multithreading and Concurrency, Serialization, Design Patterns, and JVM and Classloading.
By the way, if you are new to the Java Programming world, it's better to go through a comprehensive Java course like The Complete Java Masterclass to build the fundamentals and fill gaps in your understanding. This will save you a lot of time and it will make it easier to understand the concepts behind these interview questions.
1. How does Java achieve platform independence? (answer)
hint: bytecode and Java Virtual Machine. Java code is compiled into byte code which is then run by JVM which translate byte code to machine language code. This means, you can run same Java application to any operating system or platform like Windows, Mac, or Linux, provided you have a JVM for that platform. That's why there are different JDK package for Mac, Linux, and Windows.
By the way, if you are new to the Java Programming world, it's better to go through a comprehensive Java course like The Complete Java Masterclass to build the fundamentals and fill gaps in your understanding. This will save you a lot of time and it will make it easier to understand the concepts behind these interview questions.
50+ 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 does Java achieve platform independence? (answer)
hint: bytecode and Java Virtual Machine. Java code is compiled into byte code which is then run by JVM which translate byte code to machine language code. This means, you can run same Java application to any operating system or platform like Windows, Mac, or Linux, provided you have a JVM for that platform. That's why there are different JDK package for Mac, Linux, and Windows.
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 a bitwise operator, e.g. bitwise AND &, remember, even a number has zero at the end in binary format, and an odd number has 1 at the end.
4. 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.
5) What is double-checked locking in Singleton? (answer)
hint: two-time check whether instances are initialized or not, first without locking and second with locking.
6) How do you create thread-safe Singleton in Java? (answer)
hint: many ways, like 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 that 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 the 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.
12. 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.
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 a 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 learn more about how ConcurrentHashMap works.
17) Which two methods you will override for an Object to be used as a 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 do 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 mean? (answer)
hint: The threshold which triggers re-sizing of HashMap, is generally 0.75, which means HashMap resizes 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 the other doesn't. Insertion and deletion are 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 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.
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 a 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 the 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 classes, which is required by Polymorphism, which provides dynamic behavior.
Hint: several but most important is Hashtable is synchronized while HashMap is not. It's also legacy and slow as compared to HashMap.
5) What is double-checked locking in Singleton? (answer)
hint: two-time check whether instances are initialized or not, first without locking and second with locking.
6) How do you create thread-safe Singleton in Java? (answer)
hint: many ways, like 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 that 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 the 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.
12. 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.
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 a 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 learn more about how ConcurrentHashMap works.
17) Which two methods you will override for an Object to be used as a 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 do 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 mean? (answer)
hint: The threshold which triggers re-sizing of HashMap, is generally 0.75, which means HashMap resizes 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 the other doesn't. Insertion and deletion are 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 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.
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 a 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 the 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
hint: StringBuilder is not synchronized while StringBuffer is synchronized.
hint: Inheritance allows code reuse and builds the relationship between classes, which is required by Polymorphism, which provides dynamic behavior.
hint: No because overriding resolves at runtime while static method call is resolved at compile time.
hint: yes, in the same class but not outside the class
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 the 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.
hint: throws declare what exception a method can throw in case of error but the throw keyword actually throws an exception.
hint: fail-safe doesn't throw ConcurrentModificationException while fail-fast does whenever they detect an outside change on underlying collection while iterating over it.
hint: Iterator also gives you the ability to remove an element while iterating while Enumeration doesn't allow that.
hint: A Map that uses == equality operator to check equality instead of equals() method.
Hint: A pool of String literals. Remember it's moved to heap from perm gen space in JDK 7.
hint: Yes, but you need to make it either static or transient.
hint: this refers to the current instance while super refers to an instance of the superclass.
hint: Comparator defines custom ordering while Comparable defines the natural order of objects, e.g. the alphabetic order for String. If you are implementing a value object then you can consider implementing Comparable in Java. Also, you can define multiple Comparator for different kind of comparison and ordering requirements.
hint: former contains both date and time while later contains only date part.
hint: because they require a lock that is only available to an object.
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 are not supported in Java now.
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.
The main difference between Error and Excpetion in Java is that Error is reserved for System related error like NoClassDefFoundError, OutOfMemoryError etc. This means you cannot recover from Errror, hence they are like RuntimeException, Java doesn't check if you handle error or not. On the other hand, Exception is also an indication of unexpected condition but you can recover. There are two types of Exception, checked and unchecked. For checked exception, Java mandates to handle them.
hint: both are errors that occur in a concurrent application, one occurs because of thread scheduling while others occur because of poor coding.
That's all about the basic Core Java interview questions for beginners and junior developers with 1 to 3 years of experience. These are very common questions and it's expected from every Java developer to know them. If you don't know the answer to them then I highly suggest spending some time learning those concepts before going for interviews. Here are some more resources for further learning.
Other Java interview questions posts you may like
- Spring MVC Interview Questions and Answers (list)
- Java Questions for Phone Screen Interviews (list)
- 50+ Java Collection Interview Questions from interviews (questions)
- Thread and Concurrency Questions from Java Interviews (list)
- Top 5 Courses to learn Design Pattern in Java (courses)
- Hibernate Interview Questions with Answers (list)
- Top 5 Courses to learn Microservices in Java with Spring (courses)
- Java Web Service Interview Questions and Answers (list)
- 10 Best Courses to learn Spring Framework (online course)
- 50+ Data Structure and Algorithms problems from interviews (questions)
- RESTful Web Service Interview Questions (list)
- JDBC Interview Questions and Answers (list)
- 21 String problems from interviews (questions)
- Java OOP Interview Questions with Answers (list)
- 25 System design interview questions for beginners (Questions)
- Array Concept Interview Questions in Java (list)
- Servlet and JSP Interview Questions and Answers (list)
- My favorite courses to learn Software Architecture (courses)
Thanks for reading this article so far. If you like these Core Java Interview Questions, then please share them with your friends and colleagues. If you have any questions or feedback, then please drop a note.
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 .
ReplyDelete