♥♦ Q1-Q10: Top 50+ Core Java Interview Questions and Answers

Java interview questions and answers with detailed diagrams & working code to fast-track your Java interview preparation. In addition to the Java Interview FAQs that capture the most noteworthy Q&As, a very vast Java/JEE concepts are categorized by topics. OOP | GC | Multithreading | Collection and Data structures are key to Java interview preparation for both interviews and written tests. Java generics become more important for the written tests.

Q1. What is the difference between “==” and “equals(…)” in comparing Java String objects?
A1. When you use “==” (i.e. shallow comparison), you are actually comparing the two object references to see if they point to the same object. When you use “equals(…)”, which is a “deep comparison” that compares the actual string values. For example:

The variable s1 refers to the String instance created by “Hello”. The object referred to by s2 is created with s1 as an initializer, thus the contents of the two String objects are identical, but they are 2 distinct objects having 2 distinct references s1 and s2. This means that s1 and s2 do not refer to the same object and are, therefore, not ==, but equals( ) as they have the same value “Hello”. The s1 == s3 is true, as they both point to the same object due to internal caching. The references s1 and s3 are interned and points to the same object in the string pool.

String Pool Caches and you create a String object as a literal without the "new" keyword for caching

Create a String object as a literal without the “new” keyword for caching

In Java 6 — all interned strings were stored in the PermGen – the fixed size part of heap mainly used for storing loaded classes and string pool.

In Java 7 – the string pool was relocated to the heap. So, you are not restricted by the limited size.

How about comparing the other objects like Integer, Boolean, and custom objects like “Pet”? Object equals Vs “==“, and pass by reference Vs value.

Q2. Can you explain how Strings are interned in Java?
A2. String class is designed with the Flyweight design pattern in mind. Flyweight is all about re-usability without having to create too many objects in memory.

[ Further Reading: Flyweight pattern and improve memory usage & performance ]

A pool of Strings is maintained by the String class. When the intern( ) method is invoked, equals(..) method is invoked to determine if the String already exist in the pool. If it does then the String from the pool is returned instead of creating a new object. If not already in the string pool, a new String object is added to the pool and a reference to this object is returned. For any two given strings s1 & s2, s1.intern( ) == s2.intern( ) only if s1.equals(s2) is true.

Two String objects are created by the code shown below. Hence s1 == s2 returns false.

s1.intern() == s2.intern() returns true, but you have to remember to make sure that you actually do intern() all of the strings that you’re going to compare. It’s easy to forget to intern() all strings and then you can get confusingly incorrect results. Also, why unnecessarily create more objects?

Instead use string literals as shown below to intern automatically:

s1 and s2 point to the same String object in the pool. Hence s1 == s2 returns true.

Since interning is automatic for String literals String s1 = “A”, the intern( ) method is to be used on Strings constructed with new String(“A”).

Q3. Can you describe what the following code does and what parts of memory the local variables, objects, and references to the objects occupy in Java?

A3. The above code outputs “Hello! John you are now 26”. The following diagram depicts how the different variables & objects get stored.

Stack: local variables (both primitive & reference) and method parameters.

Heap: objects (Strings will be in String Pool within the heap)

Java Stack, Heap, and String Pool

Java Stack, Heap, and String Pool

Q4. Why String class has been made immutable in Java?
A4. For performance, thread-safety & error proneness/security.

1. Performance: Immutable objects are ideal for representing values of abstract data (i.e. value objects) types like numbers, enumerated types, etc. If you need a different value, create a different object. In Java, Integer, Long, Float, Character, BigInteger and BigDecimal are all immutable objects. Optimization strategies like caching of hashcode, caching of objects, object pooling, etc can be easily applied to improve performance. If Strings were made mutable, string pooling would not be possible as changing the string with one reference will lead to the wrong value for the other references.

[ Further reading: Java immutable objects interview Q&As | Builder pattern and immutability in Java ]

2. Thread safety as immutable objects are inherently thread safe as they cannot be modified once created. They can only be used as read only objects. They can easily be shared among multiple threads for better scalability.

3. Errors & Security Vulnerabilities: In Java you pass sensitive information like file names, host names, login names, passwords, customer account numbers, etc as a string object. If String were not immutable, a password or account number can be accidentally & easily changed, which can cause errors and security vulnerabilities.

Shouldn’t there be more FAQs on Java strings? : 10 FAQ Java String class interview questions and answers as it is one of the very frequently used data types.

Let’s move on to the next set of FAQ Core Java interview questions and answers focusing on the java modifiers final, finally and finalize.

Q5. In Java, what purpose does the key words final, finally, and finalize fulfill?
A5. final‘ makes a variable reference not changeable, makes a method not overridable, and makes a class not inheritable. Learn more about the drill down question on 6 Java modifiers that interviewers like to quiz you on…

finally‘ is used in a try/catch statement to almost always execute the code. Even when an exception is thrown, the finally block is executed. This is used to close non-memory resources like file handles, sockets, database connections, etc till Java 7. This is is no longer true in Java 7.

Java 7 has introduced the AutoCloseable interface to avoid the unsightly try/catch/finally(within finally try/catch) blocks to close a resource. It also prevents potential resource leaks due to not properly closing a resource.

//Pre Java 7

Java 7 – try can have AutoCloseble types. InputStream and OutputStream classes now implements the Autocloseable interface.

try can now have multiple statements in the parenthesis and each statement should create an object which implements the new java.lang.AutoClosable interface. The AutoClosable interface consists of just one method. void close() throws Exception {}. Each AutoClosable resource created in the try statement will be automatically closed without requiring a finally block. If an exception is thrown in the try block and another Exception is thrown while closing the resource, the first Exception is the one eventually thrown to the caller. Think of the close( ) method as implicitly being called as the last line in the try block. If using Java 7 or later editions, use AutoCloseable statements within the try block for more concise & readable code.

finalize‘ is called when an object is garbage collected. You rarely need to override it. It should not be used to release non-memory resources like file handles, sockets, database connections, etc because Java has only a finite number of these resources and you do not know when the Garbage Collection (i.e. GC) is going to kick in to release these non-memory resources through the finalize( ) method.

[ Further reading: Java Garbage Collection interview Q&As to ascertain your depth of Java knowledge ]

So, final and finally are used very frequently in your Java code, but the key word finalize is hardly or never used.

Q6. What will be the output of the following code & why?

A6. 1 is returned because ‘finally’ has the right to override any exception/returned value by the try..catch block. It is a bad practice to return from a finally block as it can suppress any exceptions thrown from a try..catch block. For example, the following code will not throw an exception.

Q7. What can prevent execution of a code in a finally block?
A7. #1. An end-less loop.

#2. System.exit(1) statement.

#3. Thread death or turning off the power to CPU.
#4. An exception arising in a finally block itself.
#5. Process p = Runtime.getRuntime( ).exec(“kill -9 ” +….);

Q8. Can you describe “method overloading” versus “method overriding”? Does it happen at compile time or runtime?
A8. Overloading deals with multiple methods in the same class with the same name but different method signatures. Both the below methods have the same method names but different method signatures, which mean the methods are overloaded.

This happens at compile-time. This is also called compile-time polymorphism because the compiler must decide which method to run based on the data types of the arguments. If the compiler were to compile the statement:

it could see that the argument was a “string” literal, and generates byte code that called method #1.

If the compiler were to compile the statement:

it could see that the argument was an “int”, and generates byte code that called method #2.

Overloading lets you define the same operation in different ways for different data.

Overriding deals with two methods, one in the parent class and the other one in the child class and has the same name and same signatures. Both the below methods have the same method names and the signatures but the method in the subclass “B” overrides the method in the superclass (aka the parent class) “A”.

Parent class

Child class

This happens at runtime. This is also called runtime polymorphism because the compiler does not and cannot know which method to call. Instead, the JVM must make the determination while the code is running.

The method compute(..) in subclass “B” overrides the method compute(..) in super class “A”. If the compiler has to compile the following method,

The compiler would not know whether the input argument ‘reference‘ is of type “A” or type “B”. This must be determined during runtime whether to call method #3 or method #4 depending on what type of object (i.e. instance of Class A or instance of Class B) is assigned to the input variable “reference”.

Overriding lets you define the same operation in different ways for different object types. It is determined by the “stored” object type, and NOT by the “referenced” object type.

It is important to understand compile-time vs runtime from core and enterprise Java interview questions & answers perspective. More detailed discussion: Compile-time Vs Run-time Interview Q&As

Q. Are overriding & polymorphism applicable static methods as well?
A. No. If you try to override a static method, it is known as hiding or shadowing.

More detailed discussion: Understanding Overriding, Hiding, and Overloading in Java? How does overriding give polymorphism?

Core Java Interview Questions and answers will not be complete without Class Loaders and Class Loading issues.

Q9. What do you know about class loading? Explain Java class loaders? If you have a class in a package, what do you need to do to run it? Explain dynamic class loading?
A9. Class loaders are hierarchical. Classes are introduced into the JVM as they are referenced by name in a class that is already running in the JVM. So, how is the very first class loaded? The very first class is specially loaded with the help of static main( ) method declared in your class. All the subsequently loaded classes are loaded by the classes, which are already loaded and running. A class loader creates a namespace. All JVMs include at least one class loader that is embedded within the JVM called the primordial (or bootstrap) class loader. The JVM has hooks in it to allow user defined class loaders to be used in place of primordial class loader. Let us look at the class loaders created by the JVM.

Java Class Loader Basics on Core Java Interview Questions and Answers

Java Class Loader Basics

Class loaders are hierarchical and use a delegation model when loading a class. Class loaders request their parent to load the class first before attempting to load it themselves. When a class loader loads a class, the child class loaders in the hierarchy will never reload the class again. Hence uniqueness is maintained. Classes loaded by a child class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not true as explained in the above diagram.

Q10. Explain static vs. dynamic class loading?
A10. Classes are statically loaded with Java’s “new” operator.

Dynamic loading is a technique for programmatically invoking the functions of a classloader at runtime. Let us look at how to load classes dynamically.

The above static method “forName” & the below instance (i.e. non-static method) “loadClass”

return the class object associated with the class name.Once the class is dynamically loaded the following method returns an instance of the loaded class. It’s just like creating a class object with no arguments.

The string class name like “com.Car” can be supplied dynamically at runtime. Unlike the static loading, the dynamic loading will decide whether to load the class “com.Car” or the class “com.Jeep” at runtime based on a runtime condition.

Static class loading throws NoClassDefFoundError if the class is NOT FOUND whereas the dynamic class loading throws ClassNotFoundException if the class is NOT FOUND.

Q. What is the difference between “Class.forName(“com.SomeClass”)” and “classLoader.loadClass(“com.SomeClass”)”?
A.

Class.forName(“com.SomeClass”)

— Uses the caller’s classloader and initializes the class (runs static intitializers, etc.)

classLoader.loadClass(“com.SomeClass”)

— Uses the “supplied class loader”, and initializes the class lazily (i.e. on first use). So, if you use this way to load a JDBC driver, it won’t get registered, and you won’t be able to use JDBC.

The “java.lang.API” has a method signature that takes a boolean flag indicating whether to initialize the class on loading or not, and a class loader reference.

So, invoking Class.forName(“com.SomeClass”) is same as invoking forName(“com.SomeClass”, true, currentClassLoader)

Q. What are the different ways to create a “ClassLoader” object?
A.

Q. How to load property file from classpath?
A. getResourceAsStream() is the method of java.lang.Class. This method finds the resource by implicitly delegating to this object’s class loader.

Note: “Try with AutoCloseable resources” syntax introduced with Java 7 is used above.

Q. What is the benefit of loading a property file from classpath?
A. It is portable as your file is relative to the classpath. You can deploy the “jar” file containing your “property” file to any location where the JVM is.

Loading it from outside the classpath is NOT portable

As the above code is NOT portable, you must document very clearly in the installation or deployment document as to where the property file is loaded from because if you deploy your “jar” file to another location, it might not already have the path “dir” and “myapp” configured.

So, loading it via the classpath is recommended as it is a portable solution.

Question to ponder

Q. What tips would you give to someone who is experiencing a class loading or “Class Not Found” exception?
A.ClassNotFoundException” could be quite tricky to troubleshoot. When you get a ClassNotFoundException, it means the JVM has traversed the entire classpath and not found the class you’ve attempted to reference.

[ Further reading: Java class loading interview Q&As to ascertain your depth of Java knowledge and Debugging LinkageErrors in Java ]

Top 50+ FAQ Core Java Interview Questions & Answers:

1) Q11-Q23: Top 50+ Core on Java OOP Interview Questions & Answers

2) Q24-Q36: Top 50+ Core on Java classes, interfaces and generics interview questions & answers

3) Q37-Q42: Top 50+ Core on Java Garbage Collection Interview Questions & Answers

4) Q43-Q54: Top 50+ Core on Java Objects Interview Questions & Answers

5) Q01-Q28: Top 50+ EE Java interview questions & answers

6) Q29-Q53: Top 50+ JEE Interview Questions & Answers

Like to train on Java multithreading interview questions?

1) 40+ Core Java Multithreading Interview Q&As

2) 10+ Java multithreading scenarios interview Q&As.

Print Friendly
The following two tabs change content below.
Arulkumaran Kumaraswamipillai
Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.
Arulkumaran Kumaraswamipillai

Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.

Posted in FAQs Core Java, Top 50+ FAQ Core Java Interview Q&A
Tags: , , , ,
14 comments on “♥♦ Q1-Q10: Top 50+ Core Java Interview Questions and Answers
  1. Rajapriya says:

    wow nice explanation…. thanks for sharing

  2. Savita says:

    Thank you sir it really good

  3. Mala chanal says:

    Thank you so much sir

  4. Ashwini Baban Ghanghav says:

    dear sir,
    Thanks for posting very good information about the core java questions and answers

  5. marco says:

    Is this a typo or have I misunderstood?

    Answer to Q3 in paragraph under performance.

    “If String were made immutable, string pooling would not be possible as changing the string with one reference will lead to the wrong value for the other references.”

    Should that be mutable?

  6. Mark says:

    For question 3 is this sentence correct?

    “If String were made immutable, string pooling would not be possible as changing the string with one reference will lead to the wrong value for the other references.”

    Should that not be mutable, as String is immutable in Java?

  7. akansha mandloi says:

    Hello,sir
    Thank you so much
    it is very helpful.

  8. amrutha says:

    Tnq sir ……its rlly useful 2 us.

    .

  9. Badrinath says:

    Hi Arulkumaran,
    Thanks for posting very good information about the core java questions and answers.
    Where can I find remaining 40+ questions? as I am not able to find the link.
    Thanks,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

800+ Java Interview Q&As ♥Free | ♦FAQ

open all | close all

200+ Java FAQs – Quick Prep

open all | close all

16 Java Key Areas to be a top-notch

open all | close all

80+ Java Tutorials – Step by step

open all | close all

100+ Java Coding Exercises

open all | close all

How good are your

open all | close all