10 Core Java Interview Questions & Answers for Senior Experienced Developer

Core Java Interview Questions

This is list of some Java fundamental questions and answers, which are commonly asked in a Core Java interview for Experienced Developers.

As a senior and matured Java Programmer you must know the answers to these questions to demonstrate basic understanding of Java language and depth of knowledge.

The competition for getting a Java job is getting fierce, and its really important that you be well prepared for you dream interview. We highly recommend you to also refer a good java book to get in depth knowledge on each of the below mentioned topic.

To get and edge over other Java developers, you may also want to checkout Must Read Core Java Books for Developers.

Below is a list of some tricky core java interview questions that may give you an edge in your next Java interview.

What is immutable object in Java? Can you change values of a immutable object?

A Java object is considered immutable when its state cannot change after it is created. Use of immutable objects is widely accepted as a sound strategy for creating simple, reliable code. Immutable objects are particularly useful in concurrent applications. Since they cannot change state, they cannot be corrupted by thread interference or observed in an inconsistent state. java.lang.String and java.lang.Integer classes are the Examples of immutable objects from the Java Development Kit. Immutable objects simplify your program due to following characteristics :

  • Immutable objects are simple to use test and construct.
  • Immutable objects are automatically thread-safe.
  • Immutable objects do not require a copy constructor.
  • Immutable objects do not require an implementation of clone.
  • Immutable objects allow hashCode to use lazy initialization, and to cache its return value.
  • Immutable objects do not need to be copied defensively when used as a field.
  • Immutable objects are good Map keys and Set elements (Since state of these objects must not change while stored in a collection).
  • Immutable objects have their class invariant established once upon construction, and it never needs to be checked again.
  • Immutable objects always have "failure atomicity" (a term used by Joshua Bloch) : if an immutable object throws an exception, it's never left in an undesirable or indeterminate state.

How to create a immutable object in Java? Does all property of immutable object needs to be final?

To create a object immutable You need to make the class final and all its member final so that once objects gets crated no one can modify its state. You can achieve same functionality by making member as non final but private and not modifying them except in constructor. Also its NOT necessary to have all the properties final since you can achieve same functionality by making member as non final but private and not modifying them except in constructor.

What is difference between String, StringBuffer and StringBuilder? When to use them?

The main difference between the three most commonly used String classes as follows.

  • StringBuffer and StringBuilder objects are mutable whereas String class objects are immutable.
  • StringBuffer class implementation is synchronized while StringBuilder class is not synchronized.
  • Concatenation operator "+" is internally implemented by Java using either StringBuffer or StringBuilder.
Criteria to choose among String, StringBuffer and StringBuilder
  • If the Object value will not change in a scenario use String Class because a String object is immutable.
  • If the Object value can change and will only be modified from a single thread, use a StringBuilder because StringBuilder is unsynchronized(means faster).
  • If the Object value may change, and can be modified by multiple threads, use a StringBuffer because StringBuffer is thread safe(synchronized).

Why String class is final or immutable?

It is very useful to have strings implemented as final or immutable objects. Below are some advantages of String Immutability in Java

  • Immutable objects are thread-safe. Two threads can both work on an immutable object at the same time without any possibility of conflict.
  • Security: the system can pass on sensitive bits of read-only information without worrying that it will be altered
  • You can share duplicates by pointing them to a single instance.
  • You can create substrings without copying. You just create a pointer into an existing base String guaranteed never to change. Immutability is the secret that makes Java substring implementation very fast.
  • Immutable objects are good fit for becoming Hashtable keys. If you change the value of any object that is used as a hash table key without removing it and re-adding it you will lose the object mapping.
  • Since String is immutable, inside each String is a char[] exactly the correct length. Unlike a StringBuilder there is no need for padding to allow for growth.
  • If String were not final, you could create a subclass and have two strings that look alike when "seen as Strings", but that are actually different.

Is Java Pass by Reference or Pass by Value?

The Java Spec says that everything in Java is pass-by-value. There is no such thing as "pass-by-reference" in Java. The difficult thing can be to understand that Java passes "objects as references" passed by value. This can certainly get confusing and I would recommend reading this article from an expert: http://javadude.com/articles/passbyvalue.htm Also read this interesting thread with example on StackOverflow : Java Pass By Ref or Value

What is OutOfMemoryError in java? How to deal with java.lang.OutOfMemeryError error?

This Error is thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. Note: Its an Error (extends java.lang.Error) not Exception. Two important types of OutOfMemoryError are often encountered

  1. java.lang.OutOfMemoryError: Java heap space

  2. The quick solution is to add these flags to JVM command line when Java runtime is started:
    -Xms1024m -Xmx1024m 
  3. java.lang.OutOfMemoryError: PermGen space

  4. The solution is to add these flags to JVM command line when Java runtime is started:
Long Term Solution: Increasing the Start/Max Heap size or changing Garbage Collection options may not always be a long term solution for your Out Of Memory Error problem. Best approach is to understand the memory needs of your program and ensure it uses memory wisely and does not have leaks. You can use a Java memory profiler to determine what methods in your program are allocating large number of objects and then determine if there is a way to make sure they are no longer referenced, or to not allocate them in the first place.

What is the use of the finally block? Is finally block in Java guaranteed to be called? When finally block is NOT called?

Finally is the block of code that executes always. The code in finally block will execute even if an exception is occurred. Finally block is NOT called in following conditions

  • If the JVM exits while the try or catch code is being executed, then the finally block may not execute. This may happen due to System.exit() call.
  • if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
  • If a exception is thrown in finally block and not handled then remaining code in finally block may not be executed.

Why there are two Date classes; one in java.util package and another in java.sql?

From the JavaDoc of java.sql.Date:

A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT. To conform with the definition of SQL DATE, the millisecond values wrapped inside a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero.
Explanation: A java.util.Date represents date and time of day, a java.sql.Date only represents a date (the complement of java.sql.Date is java.sql.Time, which only represents a time of day, but also extends java.util.Date).

What is Marker interface? How is it used in Java?

The marker interface is a design pattern, used with languages that provide run-time type information about objects. It provides a way to associate metadata with a class where the language does not have explicit support for such metadata. To use this pattern, a class implements a marker interface, and code that interact with instances of that class test for the existence of the interface. Whereas a typical interface specifies methods that an implementing class must support, a marker interface does not do so. The mere presence of such an interface indicates specific behavior on the part of the implementing class. There can be some hybrid interfaces, which both act as markers and specify required methods, are possible but may prove confusing if improperly used. Java utilizes this pattern very well and the example interfaces are

  • java.io.Serializable - Serializability of a class is enabled by the class implementing the java.io.Serializable interface. The Java Classes that do not implement Serializable interface will not be able to serialize or deserializ their state. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.
  • java.rmi.Remote - The Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. Any object that is a remote object must directly or indirectly implement this interface. Only those methods specified in a "remote interface", an interface that extends java.rmi.Remote are available remotely.
  • java.lang.Cloneable - A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.
  • javax.servlet.SingleThreadModel - Ensures that servlets handle only one request at a time. This interface has no methods.
  • java.util.EvenListener - A tagging interface that all event listener interfaces must extend.
The "instanceof" keyword in java can be used to test if an object is of a specified type. So this keyword in combination with Marker interface can be used to take different actions based on type of interface an object implements.

Why main() in java is declared as public static void main? What if the main method is declared as private?

Public - main method is called by JVM to run the method which is outside the scope of project therefore the access specifier has to be public to permit call from anywhere outside the application static - When the JVM makes are call to the main method there is not object existing for the class being called therefore it has to have static method to allow invocation from class. void - Java is platform independent language therefore if it will return some value then the value may mean different to different platforms so unlike C it can not assume a behavior of returning value to the operating system. If main method is declared as private then - Program will compile properly but at run-time it will give "Main method not public." error.

The only difference between experienced and inexperienced software developers is that the experienced ones realize when they're making a mistake.

Can you think of a questions which is not part of this post? Please don't forget to share it with me in comments section & I will try to include it in the list.

Post a Comment Default Comments

  1. Nice Collection.. thanks for sharing.. very useful for interviews..


  2. Also check out immutability tips at http://extreme-java.com/how-to-create-an-immutable-class/

  3. 1. How and why do we need to create class loader?
    2. How to make a custom read-only list?

    1. Saravenabava - Thanks for your comment, will do more research on this and add them to the list.

  4. Really helpful , makes things crystal clear.Expecting more and more similar stuffs.

  5. Really help :)Please Post some more tricky questions and answers.

  6. If you can explain hashcode() implementation/usage.

  7. If any opening to your company

  8. nice repository...
    I have also added some 1000 core java questions in www.prepareforexam.com/Interview.jsp
    Have a look at them.

  9. how can we make a class immutable with out using 'final' keyword?

  10. Very Nice post, It's really helpful, thanks ;)

  11. Very helpful set of Q&A. Can you post some material on how reflection works in java -Vinay

  12. Nice question and Answers.
    Please visit http://www.techsampes.com also for more java info

  13. Thank you very much sharing

  14. i really like that you are giving information on core and advance java concepts. Being enrolled at http://www.wiziq.com/course/1779-core-and-advance-java-concepts i found your information very helpful indeed.thanks for it.

  15. Good Collections.

  16. Its really good......Thanks.....

  17. really good questions and answers..!!

  18. Tum logo ki ma ki chut

  19. Good Questions and Answer. It can help very much for developer.

  20. Good questions for Java interview techquestforintervie.wix.com/javainterviewclub

  21. Hello Everyone!! i have a question here..please try to answer it...
    What is the ultimate ancestor of all java classes?

  22. Example survey questionnaire related templates, surveys and news at surveytool.com. Sample survey questionnaire by surveytool.com for efficient and effective sample survey questions.

  23. immutable is incorrect

  24. This is very helpful and informative blog....

  25. Good collection of questions. Also just found a website where you can give online Java mock interviews. Its www.preparestreet.com

  26. i found this site very informative thank you

  27. It was really useful.

  28. Good article! Thank you so much for sharing this post. Your views truly open my mind. I will share it with other people.

  29. Really learnt something......thanks a lot for posting

  30. Hello.. very useful questions.. i want one que related to immutable objects..
    How ca we test if the objects of a class are immutable or not.. e.g. in below code - How can we check if i1 and i2 are immutable?


    final class Immutable {
    final int a;
    Immutable(int a)

    public class ImmutableTest{
    public static void main(String a[]){
    Immutable i1 = new Immutable(4);

    Immutable i2 = new Immutable(-5);

    1. Class clazz = i1.getClass();
      1) verify whether clazz has "final" modifier or not, clazz.getModifiers();
      2) verify whether all fields of clazz have "final" modifier or not, clazz.getFields()[index].getModifiers();
      3) use java.lang.reflect.Modifier.isFinal(int mod)

  31. Hey I love the way you explain Sachin. Can you please upload Advanced Java, J2EE interview questions too..It will be very helpful. Thanks..:)

  32. Hi..
    My friend faced an interview yesterday. She was asked to explain about Immutable Object..and that question was almost dragged to another question, How do your compare two Objects ? what is hashcode() and equals() method ?

    I know that equals() method is used to compare the objects of same reference and hashcode() gives an unique id which compares the two Objects..

    Is the answer true?? Can you please explain them using simple example??


  33. Hey got the answer about hashcode() and equals() method in your collections questions post. Thanks..:)


  34. @ Sankar :- The main differnce is we can extend 1 abstract class at a time but can implement multiple interfaces all together which helps us meet multiple requirements but this cannot be achieved with extending an abstract class as it meets only single type of requirements and secondly we have only abstract methods having no implementations in an interface but we can have both abstract as well as concrete methods(i.e methods with proper implementation) defined in an abstract class..

  35. why get method is designed send limited amount of data to server?
    what is idempotent behavior give one example?

  36. Good Q&A's ,,,, Guys you may refer www.programmers99.com for good java concepts


Individuals who comment on FromDev at regular basis, will be rewarded in Top Commenter section. (Comments are selectively moderated so please do not spam)