Solutions of Core Java Interview Questions

Long back I posted few core java interview questions. But for one or the other reason, I always got a bit preoccupied and never managed to post the answers for those questions. So here I am today, with my view points on those questions. However, my explanations would be very brief and I would still recommend you to search and read as much as you can about these topics.

Q. Why do you think, Java is such a popular programming language? Or it may be reiterated like,“What makes Java different from C, C++?”

A. In my viewpoint Portability and Automatic Memory management are the two properties that made Java such a popular language. It is important that you understand how java is made portable and how automatic memory management is done by JVM.

Q. What exactly is “Code once and run anywhere” property of Java? How does Java achieve this behavior? Or it may be reiterated like, “What makes java a platform independent language?”

A. There you go, the interviewer wants to know the life cycle of a java program and what role the JVM plays in that. So you should remember, that Java Code is first compiled by the java compiler into class files and it is these class files which are portable and can be executed on any JVM platform. The core java concept implied here is that, it is the JVM that adds portability to Java Code.

Q. Can you explain how garbage collection happens in java? Or it may be reiterated like what is so special about Garbage collection in Java? 

A. Garbage Collection is automatic in Java. While writing your code, you never have to worry about memory management. The JVM has a garbage collector thread which automatically takes care of Garbage collection. However still, there are certain good programming practices which as Java programmer is expected from you and those programming practices will make your code perform better.

Q. What will happen if I call ‘System.gc()’ from my code?

A. Many people reply to this interview question, stating that the garbage collector thread would start running. However that is not correct. Calling System.gc() is like making a request to the VM to perform garbage collection, it does not mean that the VM would start Garbage collection immediately after the call. However when you call System.gc(), the next time the garbage collector thread is run, it will perform a full garbage collection, which means, all the new and old generations would be collected. Although this means longer time and more CPU cycles. So calling System.gc() makes sense only when you know your application is sitting idle.

Q. Can you explain some algorithms that are used by JVM for garbage collection?

A. The garbage collection algorithm has evolved over the period and precisely there is no single algorithm that is followed by all the JVM’s. Since you can write your own JVM’s you can also implement garbage collection the way you like it. However all the algorithms follow a basic principle, which you can say as the core java concept of Garbage collection. The JVM essentially tracks every piece of heap memory. Periodically it runs a check and scans all the objects and variables part of the program. It tries to determine if it could reach the memory for the allocated object. Any memory that cannot be reached is reclaimed and freed. This is what is called as Garbage Collection and this how essentially it works in Java.

I hope the solutions for these few Core Java Interview Questions would give you a head start in preparing for your Java Interviews. However, I will also try to frequently update this website with more Java Interview Questions and their solutions. Till then keep posting your comments and reviews.

 

 

http://javaconcepts.in/java/solutions-of-core-java-interview-questions/