Hi all, I recently attended the Java Certification Day in one of the Asian countries. One of the speakers quizzed the audience for fun. I think the 3 questions that were asked by the speaker that day was really important for the exam, as they are hot questions. Here are the questions and answers. If you want to test yourself, do not read the answer immediately after you have read the question. Questions : Q1)How do you force garbage collection to happen ? Q2)How to change the value of a String object ? Q3)How do you start a thread immediately ? Answers: A1)You cannot force GC. This is because GC is actually a low priority thread, and we know that threads are under the mercy of the thread scheduler. All we can do is only to suggest to the JVM to garbage collect - we cannot force garbage collection. The way to suggest is this : 1) set all references to null for objects that you want to garbage collect. Then call System.gc() or Runtime.gc() to suggest to the JVM to recycle all objects which do not have any references pointing to them. A2)Value of String objects cannot be changed as Strings are immutable objects. In other words, after you have instantiated a String with a value, you will not be able to change that value. Eg. String s = new String("abc"); - you are not allowed to change the "abc" value. What you can do is to create another string, ie String s = new String("123"); A1)The running of threads are under the mercy of the thread scheduler. You can suggest to the thread scheduler to start running a thread but you are not able to force a thread to start IMMEDIATELY. The way to SUGGEST to the thread scheduler to start is to call the start() method of the thread object. Eg threadA.start(); Refer to the API and you will see the word “suggest”.
The answers written here are not the exact words of the speaker when he gave the answers. They are “replicated” by me. As such, if you know that any part of the answer is wrong/inaccurate, pls let me (and whoever who is reading this) know ! Thanks fellow ranchers !