Project Management Professional: Core Java Interview questions

Tuesday, July 21, 2020

Core Java Interview questions


4. What is difference between StringBuffer and StringBuilder in Java ? Classic Java questions which some people thing tricky and some consider very easy. StringBuilder in Java is introduced in Java 5 and only difference between both of them is that Stringbuffer methods are synchronized while StringBuilder is non synchronized.


5. Can we use multiple main methods?

Yes. While starting the application we mention the class name to be run. The JVM will look for the main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.

We can overload main method but we can not override it. 

So , we can have many main methods in a class.


6.Why doesn’t importing static final from other class , doesn’t actually make a reference call to class holding static final ?

In Java we use final keyword with variables to specify its values are not to be changed. But I see that you can change the value in the constructor / methods of the class. Again, if the variable is static then it is a compilation error.

7.Why Char array is preferred over String for storing password?

String is immutable in java and stored in String pool. Once it's created it stays in the pool until unless garbage collected, so even though we are done with password it's available in memory for longer duration and there is no way to avoid it. It's a security risk because anyone having access to memory dump can find the password as clear text.

8.How does volatile affect code optimization by compiler?

Volatile is an instruction that the variables can be accessed by multiple threads and hence shouldn't be cached. As volatile variables are never cached and hence their retrieval cannot be optimized.


No comments: