Top 15 Core Java Interview Questions and Answers

Businessman giving an handshake

Job interviews can be stressful, especially during the early screening stages when you’re asked a series of in-depth questions that prove you’re knowledgeable about your subject matter. They may come up during the initial phone screening if you have one, or they may come later on during the face-to-face interview, which may take place one-on-one or in front of a panel.

If you’re applying for a job that requires a high level of technical skill such as Java, you should be prepared to answer the core Java interview questions the interviewers pose.

More...

What Is Core Java?

java screen


Core Java is a generic term used by Sun Microsystems to describe standard Java, or JSE. Core Java is the foundation for all other editions of Java, such as Java Enterprise and Java Micro. It’s also the foundation for many other technologies, such as COBRA.

Core Java is used to develop applications used on most desktops. Core Java is widely used by developers and requires programming knowledge. If you know Core Java, then you’ll be able to understand the fundamentals of other related applications such as multithreading, polymorphism, OOP and others.Enter your text here...

How To Learn Core Java

You can take courses online or in person to learn the basics of Java. You should expect your coursework to cover various topics, including basic syntax, exception handling, OOPs concepts, application monitoring, and Java naming and directory interface.

What To Expect In an Interview

If you understand the basics of core Java, you should be prepared to handle most types of interview questions that come your way during the screening process. Whether you’re interviewing for an IT job with a technology company or are meeting with a financial institution, the questions should be fairly similar regardless of industry.

While the questions may vary in difficulty, they should look similar no matter where you interview. Here are some of the most common you’ll likely come across during the interview process.

1. How Does Java Achieve Platform Independence?

The interviewer is looking to understand that you know how Java programs can operate independently of other platforms or operating systems such as Windows or Linux. Java is able to run independently thanks to a Java virtual machine, which happens when a program is compiled and then converted to a .class file. These files, which are a collection of byte codes, are readable by JVM which allows Java programs to operate independently.

2. What’s the Difference Between ArrayList and HashSet?

This is one of the most commonly asked questions in an interview for a Java programming position, so knowing how to answer it properly is important. You should be able to explain the key differences between these Java-related terms. While ArrayList uses a list interface, HashSet uses the set interface within Java. With ArrayList, you can also maintain an insertion order of elements with the ordered collection whereas with HashSet, you’ll be working with an unordered collection that doesn’t maintain any type of order.

True to their name, ArrayLists are backed by an Array and allow duplicates, while HashSets are backed by a HashMap and do not allow for duplicates. When you use ArrayList, you can retrieve or remove objects by calling get(index) or remove(index). With HashSet, you cannot use the get() method since it’s object based.

code

3. What Is ClassLoader?

This is another common question that may come up during the interview process. Class loading happens when a program is converted into a .class file through Java compiler. The collection of byte codes is then loaded from one system or network to another using class loader. Class loaders also come in three types: bootstrap, extension and system class loaders.

4. When Should You Use a Volatile Variable?

You should use the volatile keyword if you’re looking to achieve synchronization because using this keyword guarantees the value of the volatile variable will be read from the main memory and not the thread’s local cache. Using the volatile variable can also help secure you from reordering your code and provides a visibility guarantee.

5. How Do You Create Thread-Safe Singleton?

To create a thread-safe Singleton class using Java, you can create one instance during the class-loading process. Simply set up static fields during the class loading to guarantee that no instances will be visible unless they are specifically created.

6. Can Private Method Be Overridden?

If you declare a variable method private, then you cannot override private methods in Java. That variable will only be visible for that class if it’s been bonded with a class during the compiling process. Once done, it cannot be referenced using any object to override private method.

7. What’s the Difference Between Notify and Notify All?

With the notify method, you will only wake up, or notify, one thread at a time, whereas with notifyAll, you’ll notify all threads. If you’re only seeking to notify one thread, then the notify command will suffice; but if you know that you need to notify multiple threads in the CPU, then the notifyAll method is your best option.

8. What’s the Difference Between Transient and Volatile Variables?

Interviewers will expect you to be able to tell the difference between different variables such as these. With volatile variables, the keyword guarantees that the value of volatile variables will always be read from the CPU’s main memory rather than from the thread’s local cache. A transient variable, however, is used in instances when variables aren’t part of the serialization process.

9. When Would You Need To Use a Transient Variable?

You would use a transient variable to show that a particular variable shouldn’t be serialized, which is a process used to save an object’s state while in Java. You may run into situations in which you will want to avoid persisting some variables because they aren’t necessary to transfer from one network to another. To do that, simply declare those specific variables as transient and they won’t be persisted.

10. What’s the Difference Between PATH and ClassPath?

To answer this question, you’ll need to explain that PATH is considered an environment variable which helps Java program, compile and run, and that if you want to set the PATH Variable, you’ll need to include a specific code, JDK_HOME/bin directory, in the PATH environment variable in order for it to work. With a ClassPath variable, you can use a class loader to locate and load compiled codes that are stored within the .class file. PATH variables cannot be overridden, whereas ClassPath variables can.

11. Does Java Support Operator Overloading?

While C++ supports operator overloading, Java does not. This is because Java is a series of simplistic coding allowable through arithmetic operations, whereas other systems such as C++ are much more complex, which is why C++ allows operator overloading. You can, however, use a series of + using StringBuilder or StringBuffer to form strings to create either compile or execution-time concatenation of constants.

12. Explain How Substrings () Inside String Work.

When you use substrings, you create an entirely new object out of the original source string while using a portion of the original string. Before the launch of Java 1.7, creating a substring longer than five characters ran the risk of memory leak because it held the reference of the original character array. After 1.7 was released, this issue was fixed and the original character array was no longer referenced. However, creating a substring became a bit more time consuming as a result.

13. Explain How To Avoid a Deadlock in Java.

To avoid a deadlock, you’ll need to break the circular-wait condition. This can be accomplished by arranging the code to impose the ordering on acquisition and release of locks. If the lock is set to a consistent order and released in a separate order, you should be able to avoid a deadlock.

14. What’s the Difference Between Hashtable and ConcurrentHashMap?

While both are used in a multithreaded environment because they’re thread safe, Hashtable’s performance can suffer quickly if its size becomes too large. With a HashMap, however, you can lock only certain parts of the thread because HashMaps work on segmentation, so you can access other parts of the element without the entire piece needing to be complete.

15. Explain the Difference Between Extends Thread and Implements Runnable.

When answering this question, you’ll want to be sure you demonstrate you understand that Java has multiple inheritance support that allows you to implement more than one interface. If you try to extend your thread class to override run() method, you may lose the ability to extend another class. However, if you use runnable, you’ll still be able to use other interfaces or classes. The thread is also an abstract of a path of execution while runnable is an abstract of a task that can be executed by any thread.

 Final Thoughts


Knowing Java is a specialized skill that requires study and practice. You cannot expect to walk into an interview for a job that requires Java knowledge and think you’ll get the position without a thorough understanding of the subject matter. However, if you take the time to learn Java and prepare for your interview, you should be able to secure a job requiring this specialized skill.

Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.