Term
What are different types of access modifiers in Java? |
|
Definition
private, protected, default, public |
|
|
Term
Is a private class accessible in the same package? |
|
Definition
|
|
Term
Is a private class accessible in a different package? |
|
Definition
|
|
Term
Is a protected class accessible in the same package? |
|
Definition
|
|
Term
Is a protected class accessible in a different package? |
|
Definition
Yes, only if the class extends the main class |
|
|
Term
Is a default class accessible in the same package? |
|
Definition
|
|
Term
Is a default class accessible in a different package? |
|
Definition
|
|
Term
Is a public class accessible in the same package? |
|
Definition
|
|
Term
Is a public class accessible in a different package? |
|
Definition
|
|
Term
What happens if a final is assigned to a variable? |
|
Definition
The variable behaves as a constant. It means that the value of variable once set cannot be changed. |
|
|
Term
What happens if a final is assigned to a method? |
|
Definition
Then it cannot be overridden in its child class. |
|
|
Term
What happens if a class is made as final? |
|
Definition
Then no other class can extend it and make it as parent class. |
|
|
Term
What is use of synchronized keyword |
|
Definition
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. |
|
|
Term
What is volatile keyword? |
|
Definition
(one variable that is able to be accessed and edited by all threads)
In general each thread has its own copy of variable, such that one thread is not concerned with the value of same variable in the other thread. But sometime this may not be the case. Consider a scenario in which the count variable is holding the number of times a method is called for a given class irrespective of any thread calling, in this case irrespective of thread access the count has to be increased. In this case the count variable is declared as volatile. The copy of volatile variable is stored in the main memory, so every time a thread access the variable even for reading purpose the local copy is updated each time from the main memory. The volatile variable also have performance issues. |
|
|
Term
What is a transient variable? |
|
Definition
(the member will not be serialized so it is not persistant)
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. |
|
|
Term
What is a strictfp modifier? |
|
Definition
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. |
|
|
Term
What is a static variable? |
|
Definition
Static keyword can be used with the variables and methods but not with the class but there are static class. Anything declared as static is related to class and not objects. |
|
|
Term
|
Definition
A method defined as static is called static method. A static method can be accessed without creating the objects. Just by using the Class name the method can be accessed. |
|
|
Term
Why can static methods not access non static variables or methods? |
|
Definition
A static method cannot access non static variables or methods because static methods doesnt need the object to be accessed. So if a static method has non static variables or non static methods which has instantiated variables they will no be intialized since the object is not created and this could result in an error. |
|
|
Term
|
Definition
A class cannot be declared static. But a class can be said a static class if all the variables and methods of the class are static and the constructor is private. Making the constructor private will prevent the class to be instantiated. So the only possibility to access is using Class name only |
|
|
Term
|
Definition
Throw keyword is used to throw the exception manually. It is mainly used when the program fails to satisfy the given condition and it wants to warn the application.The exception thrown should be subclass of Throwable. |
|
|
Term
What is use of throws keyword? |
|
Definition
If the function is not capable of handling the exception then it can ask the calling method to handle it by simply putting the throws clause at the function declaration. |
|
|