Core Java Interview Questions and Answers

Ques 41. What does the keyword "synchronize" mean in java. When do you use it? What are the disadvantages of synchronization?

Ans.Synchronize is used when you want to make your methods thread safe. The disadvantage of synchronize is it will end up in slowing down the program. Also if not handled properly it will end up in dead lock.

Is it helpful?
YesNoAdd CommentView Comments

Ques 42. What gives java it's "write once and run anywhere" nature?

Ans. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platorm specific and hence can be fed to any platform. After being fed to the JVM, which is specific to a particular operating system, the code platform specific machine code is generated thus making java platform independent.

Is it helpful?
YesNoAdd CommentView Comments

Ques 43. What are native methods? How do you use them?

Ans. Native methods are methods written in other languages like C, C++, or even assembly language. You can call native methods from Java using JNI. Native methods are used when the implementation of a particular method is present in language other than Java say C, C++. To use the native methods in java we use the keyword native public native method_a(). This native keyword is signal to the java compiler that the implementation of this method is in a language other than java. Native methods are used when we realize that it would take up a lot of rework to write that piece of already existing code in other language to Java.

Ans.public : Public class is visible in other packages, field is visible everywhere (class must be public too)private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.default : What you get by default ie, without any access modifier (ie, public private or protected).It means that it is visible to all within a particular package.

Is it helpful?
YesNoAdd CommentView Comments

Ques 45. What does the "final" keyword mean in front of a variable? A method? A class?

Ans.FINAL for a variable : value is constant FINAL for a method : cannot be overridden FINAL for a class : cannot be derived A final variable cannot be reassigned, but it is not constant. For instance, final StringBuffer x = new StringBuffer() x.append("hello"); is valid. X cannot have a new value in it,but nothing stops operations on the object that it refers, including destructive operations. Also, a final method cannot be overridden or hidden by new access specifications.This means that the compiler can choose to in-line the invocation of such a method.(I don't know if any compiler actually does this, but it's true in theory.) The best example of a final class is String, which defines a class that cannot be derived.