Q1) What are different types of access modifiers in Java?
Ans) There are four different types of modifiers:
| |||||||||||||||
Q2) What is the use of final keyword?
Ans) The final keyword can be assigned to
If a final is assigned to a variable, the variable behaves as a constant. It means that the value of variable once set cannot be changed.
If a final is assigned to a method then it cannot be overridden in its child class.
class Parent {
}
class Child extends Parent{
}
If a class is made as final, then no other class can extend it and make it as parent class. E.g. String Class.
Final objects are instantiated only once. i.e
final Map map = new HashMap();
map.put(“key”,”value”);
map = new HashMap(); // error
| |||||||||||||||
Q3) What is use of synchronized keyword?
Ans) This keyword is used to prevent concurrency. Synchronized keyword can be applied to static/non-static methods or a block of code. Only one thread at a time can access synchronized methods and if there are multiple threads trying to access the same method then other threads have to wait for the execution of method by one thread. Synchronized keyword provides a lock on the object and thus prevents race condition. E.g.
public void synchronized method(){}
public void synchronized staticmethod(){} public void myMethod(){
synchronized (this){ // synchronized keyword on block of code
}
}
| |||||||||||||||
Q4) What is volatile keyword?
Ans) Essentially, volatile is used
to indicate that a variable's value will be modified by
different threads.
Declaring a volatile Java variable means:
| |||||||||||||||
Q5) What is a transient variable?
Ans) If some of the properties of a class are not required to be serialized then the varaibles are marked as transient. When an object is deserialized the transient variables retains the default value depending on the type of variable declared and hence lost its original value.
| |||||||||||||||
Q6) What is a strictfp modifier?
Ans) Strictfp is used with variable only . It is used to restrict floating point calculations ( fp ) to ensure portability ( platform Independent ). When this modifier is specified, the JVM adheres to the Java specifications ( IEEE-754 floating-point specification ) and returns the consistent value independent of the platform. That is, if you want the answers from your code (which uses floating point values) to be consistent in all platforms, then you need to specify the strictfp modifier.
|
Wednesday, September 3, 2014
Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment