Archive

Posts Tagged ‘core java interview question’

interview Questions : Java

June 15th, 2009

1. what are the Native methods in Java?

a. When you want to call functions or subroutines which are written in some other language other than Java like C, C++ or VB6 etc.., These methods are Native methods in Java.

2. Explain Garbage Collection in Java?

a. Garbage collection is the process of automatically freeing object that are no longer referenced by the program. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many bugs. Thus making programmers more productive as they can put more effort in coding rather than worrying about memory management. The only disadvantage of garbage collector is it adds overheads. Because the JVM has to keep a constant track of the objects which are not referenced and then free these unreferenced objects on fly. This whole process has slight impact on the application performance.

3. How can we force the garbage collector to run?

a. Garbage collector can be run forcibly using “System.gc()” or “Runtime.gc()”

4. What is use of method “finalize()” in Java?

a. Sometimes the objects needs to perform some action before the object is destroyed. For instance object is holding some non-java resources such as file handle which you might want to make sure are released before the object is destroyed. For such conditions java provides mechanism called finalization. You can add finalizer in a class by simply defining the “finalize()” method. JVM calls that method whenever it is about to recycle an object of that class.

5. Are String objects immutable, can you explain the concept?

a. Yes String object is immutable, Once you have assigned a String a value, that value can never change. Lets try to understand the concept by the below code snippet

String s1 = “hello”;

s1 = s1 + ” World”;

The VM took the value of String s (which was “hello”), and tacked ” world” onto the end, giving us the value “abcdef more stuff”. Since Strings are immutable, the VM couldn’t stuff this new value into the old String referenced by s1, so it created a new String object, gave it the value “hello world”,and made s1 refer to it. At this point in our example, we have two String objects: the first one we created, with the value “hello”, and the second one with the value ” world”. Technically there are now three String objects, because the literal argument to concat, ” world”, is itself a new String object.

6. Why String objects are immutable in Java?

a.To make Java more memory efficient, the JVM sets aside a special area of memory called the “String constant pool.” When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created. (The existing String simply has an additional reference.) Now we can start to see why making String objects immutable is such a good idea. If several reference variables refer to the same String without even knowing it, it would be very bad if any of them could change the String’s value.

You might say, “Well that’s all well and good, but what if someone overrides the String class functionality; couldn’t that cause problems in the pool?” That’s one of the main reasons that the String class is marked final. Nobody can override the behaviors of any of the String methods, so you can rest assured that the String objects you are counting on to be immutable will, in fact, be immutable.

7. What is StringBuffer class and how does it different from String class?

a. StringBuffer is a peer class of String that provides almost all functionality of Strings. String represent fixed-length, immutable character sequences. Comparatively StringBuffer represents mutable, growable and write-able character sequences. But StringBuffer does not create new instance as String so it’s more efficient when it comes to intensive concatenation operation.

8. What is the difference between StringBuilder and StringBuffer class?

a. StringBuffer is synchronized and StringBuilder is not.

9. What are the packages?

a. Packages group related class and interfaces together and thus avoiding any name conflicts. Classes are group together in package using “package” keyword.

10. What is use of JAVAP tool?

a. “javap” disassembles compiled Java files and spits out the representation of the Java program. This is a useful option when the original source code is unavailable.

S.Chandru Java ,