Everybody is good! I’ve been sharing a lot of Java interview questions and discussions lately, and many of my readers have asked to put them together so they can put them on the same page and be prepared, and this article is the result.
This article contains over 50 Java interview questions covering all important topics such as core Java basics, Java Collections Framework, Java multithreading and concurrency, Java IO, JDBC, JVM kernel, coding issues, object-oriented programming, and more.
These questions can also be asked in a variety of interviews, but they are by no means too difficult, and you may have already encountered them on the phone or in an in-person interview.
These questions are also great for reviewing important topics like multithreading and collections, as I also share some useful resources for further learning and improvement, such as The Complete Java MasterClass, to fill gaps in Java skills.
So here is a list of some common Java questions for beginners and experienced Java developers of two to five years:
Java interview questions and solutions
1) How does Java achieve platform independence?
Answer: www.java67.com/2012/08/how…
Tip: bytecode and the Java virtual machine
2) What is Java ClassLoader?
Answer: (javarevisited. Blogspot. Sg / 2012/12 / how…).
Tip: part of the JVM that loads the class’s bytecode. You can write it yourself.
3) Write a Java program to determine whether a number is even or odd?
Answer: (javarevisited. Blogspot. Sg / 2013/04 / how…).
Tip: You can use bitwise operators, such as bitwise AND &, AND remember that even numbers end with 0 at the end of the binary format AND odd numbers end with 1.
4)What’s the difference between Java ArrayList and HashSet?
Answer: www.java67.com/2012/07/dif…
Tip: All differences between lists and sets apply here, such as sorting, repetition, random search, etc.
5) Singleton modeWhat is the double check in?
Answer: www.java67.com/2016/04/why…
Tip: Check whether the instance is initialized twice. The first time there is no lock, and the second time there is a lock.
6)How do I create thread-safe singletons in Java?
Answer: (javarevisited. Blogspot. Sg / 2012/12 / how…).
Tip: Many methods, such as using Enum or using double-checked locking mode or using nested static classes.
7)When do YOU use volatile variables in Java?
Answer: www.java67.com/2012/08/wha… Hint: When you need to indicate to the JVM that a variable can be modified by multiple threads and prompt the JVM not to cache its value.
8)When to use transient variables in Java?
Answer: www.java67.com/2012/08/wha…
Tip: When you want to create a non-serializable variable in a class that implements the Serializable interface. In other words, you can use it for variables whose values you don’t want to keep. See The Complete Java MasterClass for transient variables in Java.
9)What is the difference between transient and volatile variables in Java?
Answer: www.java67.com/2012/11/dif…
Tip: Totally different, one for serialization, the other for concurrency.
10)The difference between Serializable and Externalizable in Java?
Answer: www.java67.com/2012/10/dif…
Tip: Externalizable gives you better control over the serialization process.
11)Can we override private methods in Java?
Answer: www.java67.com/2013/08/can…
Tip: No, because it is not visible in subclasses, which is the main requirement for override methods in Java.
12)What is the difference between Java Hashtable and HashMap?
Answer: (javarevisited. Blogspot. Sg / 2010/10 / dif…
Tip: The most important thing is that Hashtable is synchronous, while HashMap is not. In comparison, it is also a traditional and slow HashMap.
13)The difference between a Java List and a Set?
Answer: (javarevisited. Blogspot. Sg / 2012/04 / dif…
Tip: List allows repetition. Sets are unordered and do not allow duplicate elements.
14) Differences between Java ArrayList and Vector
Answer: www.java67.com/2012/09/arr…
Tip: There are many, but the most important ArrayList is asynchronous and fast; Vector is synchronous and slow. It is also a traditional class like Hashtable.
15) What is the difference between Java Hashtable and ConcurrentHashMap?
Answer: (javarevisited. Blogspot. Sg / 2011/04 / dif…
Tip: More scalable
16)How is ConcurrentHashMap scalable?
Answer: (javarevisited. Blogspot. Sg / 2017/08 / top…).
Tip: By dividing the map into segments and locking it only during write operations.
17)For an Object to be used as the Key of a HashMap, which two methods do you need to override?
Answer: www.java67.com/2013/06/how…
Tip: Equals and Hashcode
18) What is the difference between Wait and sleep in Java?
Answer: www.java67.com/2012/08/wha…
Tip: The wait() method releases the lock or monitor, but the sleep does not.
19) What is the difference between Java Notify and notifyAll?
Answer: www.java67.com/2013/03/dif…
Hint: Notify notifies a random thread that is waiting for the lock, and notifyAll notifies all waiting threads. If you are sure that only one thread is waiting, use notify, otherwise notifyAll is better. See Threading Essentials Mini-Course for more information on Threading basics from Java Champion Heinz Kabutz.
20) Why override HashCode in Java with equals()?
Answer: (javarevisited. Blogspot. Sg / 2015/01 / according to…).
Tip: To comply with the Equals and HashCode conventions, you need to do so if you store objects in a collection class, such as HashMap or ArrayList.
21) What does HashMap load factor mean?
Answer: www.java67.com/2017/08/top…
Tip: The threshold that triggers resizing is typically 0.75 for a HashMap, meaning that the HashMap resizes itself if it reaches 75% storage.
22) The difference between Java ArrayList and LinkedList?
Answer: www.java67.com/2012/12/dif…
Tip: As with arrays and linked lists, one allows random searching and the other does not. It’s easier to insert and delete on linked lists, but easy to query on arrays. See Java Fundamentals: Collections Richard Warburton’s course on Pluralsight for more information on the basic Collection data structures in Java.
23) What is the difference between Java CountDownLatch and CyclicBarrier?
Answer: www.java67.com/2012/08/dif…
Tip: You can repeat after the CyclicBarrier is broken, but CountDownLatch cannot repeat after the count reaches zero.
24) When to use Runnable vs Thread in Java?
Answer :(www.java67.com/2016/01/7-d…)
Hint: Forever
25) What does Enum mean by type safety in Java?
Answer: www.java67.com/2014/04/wha…
Tip: This means that you cannot assign instances of different enumeration types to Enum variables. For example, if you have a variable like DayOfWeek, you can’t assign with the DayOfMonth enumeration.
26)How to implement automatic boxing of Integer in Java?
Answer: (javarevisited. Blogspot. Sg / 2012/07 / aut…).
Tip: Use the valueOf() method
27)What is the difference between PATH and Classpath in Java?
Answer: www.java67.com/2012/08/wha…
Tip: PATH is used by the operating system, while Classpath is used by the JVM to locate Java binaries (such as JAR files or class files). See Java Fundamentals: The Core Platform to learn more about PATH, Classpath, and other Java environment variables.
28)What’s the difference between method overloading and overwriting in Java?
Answer: www.java67.com/2015/08/top…
Tip: Overrides occur in subclasses, while overloading occurs in the same class. In addition, overrides are run-time activities, while overloads are resolved at compile time.
29)How do I prevent a class from being inherited in Java?
Answer: www.java67.com/2017/06/10-… Tip: Final decorates classes
30)How do you limit the use of your class by clients?
Answer: (javarevisited. Blogspot. Sg / 2016/01 / according to…).
Tip: Make the constructor private or throw an exception from the constructor
31)The difference between Java StringBuilder and StringBuffer?
Answer :(www.java67.com/2016/10/5-d…)
Tip: StringBuilder does not synchronize while StringBuffer does.
32)What is the difference between polymorphism and inheritance in Java?
Answer: www.java67.com/2014/04/dif…
Tip: Inheritance allows code reuse and builds relationships between classes, which is needed for polymorphism, which provides dynamic behavior. See Java Fundamentals: Object-oriented Design
To learn more about OOP functionality.
33)Can we override static methods in Java?
Answer: www.java67.com/2012/08/can…
Tip: No, because overrides are resolved at run time, whereas static method calls are resolved at compile time.
34)Can we access private methods in Java?
Answer: www.java67.com/2012/08/can…
Tip: Yes, in the same class but outside of it
35)The difference between an interface and an abstract class in Java?
Answer: www.java67.com/2017/08/dif…
Tip: Starting with Java 8, the differences are ambiguous. However, Java classes can still implement multiple interfaces, but only extend one class.
36)What’s the difference between a DOM and SAX parser in Java?
Answer: www.java67.com/2012/09/dom…
Tip: DOM loads the entire XML file in memory, while SAX does not. It is an event-based parser that can be used to parse large files, but DOM is fast and should be preferred for small files.
37)Throws throws (throw); throws (throw); throws (throw); throws (throw);
Answer: www.java67.com/2012/10/dif…
Tip: Throws declares an exception that a method can throw if an error occurs, but the throw keyword actually throws an exception. See Java Fundamentals: Exception Handling to learn more about Exception Handling in Java.
38)What is the difference between fail-safe and Fail-fast iterators in Java?
Answer: www.java67.com/2015/06/wha…
Tip: when they are iterative detection to the underlying collection of external changes, fail – fast throw ConcurrentModificationException, fail – safe won’t sell them.
39)What is the difference between Iterator and Enumeration in Java?
Answer: (javarevisited. Blogspot. Sg / 2010/10 / wha…).
Tip: Iterator also allows you to delete elements during iteration, which Enumeration does not.
40)What is IdentityHashMap in Java?
Answer: www.java67.com/2016/09/dif…
Tip: A Map that uses the == equality operator to check for equality instead of the equals() method.
41)What is a String pool in Java?
Answer: (javarevisited. Blogspot. Sg / 2016/07 / dif…
Hint: a String pool. It has been moved from the PERm Gen space in JDK 7 to the heap.
42)Can a Serializable class in Java contain a non-serializable field?
Answer: (javarevisited. Blogspot. Sg / 2016/09 / how…).
Tip: Yes, but you need to set it to static or TRANSIENT.
43)The difference between Java this and super?
Answer: www.java67.com/2013/06/dif…
Tip: This refers to the current instance, while super refers to an instance of the superclass.
Comparable between Java comparators and Comparable?
Answer: www.java67.com/2013/08/dif…
Note: The Comparator defines a custom sort, whereas Comparable defines the natural order of objects, such as the alphabetical order of a String. For more information about sorting using Java, see The Complete Java MasterClass.
45)Date: java.util.Date: java.sql.Date: java.util.Date: java.sql.
Answer: (javarevisited. Blogspot. Sg / 2012/04 / dif…
Tip: the former contains the date and time, the latter only contains the date part.
46)Why are wait and notify declared in Java’s Object class?
Answer: (javarevisited. Blogspot. Sg / 2012/02 / according to…).
Tip: Any object can be used as a lock.
47)Why doesn’t Java support multiple inheritance?
Answer: (javarevisited. Blogspot. Sg / 2011/07 / according to…).
48) The difference between checked and unchecked exceptions in Java?
Answer: (javarevisited. Blogspot. Sg / 2011/12 / che…).
Tip: If it is checked for an exception, you must use a catch block to handle the exception. If it is not checked, it is up to you. Compilation will not prompt you.
49)What is the difference between errors and exceptions in Java?
Answer: www.java67.com/2012/12/dif…
50) The difference between a race condition and a deadlock in Java?
Answer: (javarevisited. Blogspot. Sg / 2012/02 / wha…).
More resources
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
130+ Java Interview Questions of Last 5 Years
Thank you for reading this! If you enjoy these core Java questions, please share them with your friends and colleagues. If you have any questions or feedback, please leave a comment below.
Click on the link to the original English text
More articles are welcome to visit http://www.apexyun.com
Public id: Galaxy 1
Contact email: [email protected]
(Please do not reprint without permission)