Core Java Interview
Core Java Interview
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 :
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).
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.
-Xms1024m -Xmx1024m
1. java.lang.OutOfMemoryError: PermGen space
The solution is to add these flags to JVM command line when Java runtime is started:
-XX:+CMSClassUnloadingEnabled-XX:+CMSPermGenSweepingEnabled
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.
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:
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.