More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. It means that the type of variables are checked at compile time in Java.The main advantage here is that all kinds of checking can be done by the compiler and hence will reduce bugs.
Help us improve. Please let us know the company, where you were asked this question :
Ans. 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.
Help us improve. Please let us know the company, where you were asked this question :
Ans. 1. Method local variables - These are declared and defined within a method ( instance or static methods ) and their scope is limited to the method itself. They are destructed once the execution of method completes. They are stored in stack memory.
2. Instance variables - These are declared as non static variables as part of the class.They are initialized as part of object creation ( constructor ) and are destructed by java's garbage collection mechanism and hence stored in heap.
3. Static variables - These are declared with the static keyword and are part of the class. They are initialized at the time of class loading and are destructed by java's garbage collection mechanism and hence stored in heap.
Help us improve. Please let us know the company, where you were asked this question :
Ans. final keyword have meaning only to referenced and not the value. It means that the specified reference cannot be dereferenced. It doesn't control the value assigned to the memory that's being referenced. This is the reason that final object references doesn't mean that the object is immutable but means that the reference cannot be changed to point to new object.
In case of primitive types too, when we assign a reference to another, values are passed and not the object reference, and hence a new placeholder is created in memory with the same value. That is why final to that context means that you cannot change the assigned memory and there is no way we can have that memory place have another value.
Help us improve. Please let us know the company, where you were asked this question :
Ans. member variable are loaded into heap, so they are initialized with default values when an instance of a class is created. In case of local variables, they are stored in stack until they are being used.
Help us improve. Please let us know the company, where you were asked this question :
Q11. Will the static block be executed in the following code ? Why ?
class Test { static { System.out.println("Why I am not executing "); } public static final int param=20; }
public class Demo { public static void main(String[] args) { System.out.println(Test.param); } }
Ans. No the static block won't get executed as the referenced variable in the Test class is final. Compiler replaces the content of the final variable within Demo.main method and hence actually no reference to Test class is made.
Help us improve. Please let us know the company, where you were asked this question :
Q13. What is the advantage of using arrays over variables ?
Ans. Arrays provide a structure wherein multiple values can be accessed using single reference and index. This helps in iterating over the values using loops.
Help us improve. Please let us know the company, where you were asked this question :
Ans. They should be named all in upper case with underscore separating words.
Moreover the name should be such that it identifies the value. For example, if we are using constant for employee Id 123, it should be named something like EMPLOYEE_ID_123 and not EMPLOYEE_ID
Help us improve. Please let us know the company, where you were asked this question :
Ans. Final makes sure that the value doesn't change after initialization and static makes sure that there is only one copy that can be shared across objects.
Making it non static will unnecessarily create a different copy per object wherein the same value will kept for all copies ( as its final and cannot be changed ).
Help us improve. Please let us know the company, where you were asked this question :
Q21. Which of the following is not true for final variables ?
a. They cannot be changed after initialization b. They can be initialized within static method c. They can be declared and initialized together at the same place d. They can be initialized within constructor
Ans. They can be initialized within static method
Help us improve. Please let us know the company, where you were asked this question :
Q24. Does compiler treats it differently if we don't prefix this to the member element and its implicit as its the only variable available with that name ?
Ans. It makes no difference whether we add this to the variable or not. The only use of this is in the cases where there are multiple variables with the same name and we want to distinguish between the member variable and local variable. In case this is not added ,this is automatically added by the compiler in the byte code.
Help us improve. Please let us know the company, where you were asked this question :
Ans. No, If both Parent and Derived are outer classes.
public class Vehicle {
private static String manufacturingDate = "2016";
}
public class Car extends Vehicle{
public static void main(String args[]){
System.out.println(manufacturingDate); // error - The field Vehicle.manufacturingDate is not visible
}
}
Yes, If derived is the inner class of Parent.
public class Vehicle {
private static String manufacturingDate = "2016";
static public class Car extends Vehicle{
public static void main(String args[]){
System.out.println(manufacturingDate); // no problem
}
}
}
Help us improve. Please let us know the company, where you were asked this question :
Ans. static methods and static elements are shared by all objects of a class and hence could create problem.
Static methods are not synchronized by default and hence could create problem when accessed through multiple threads. Static elements are shareable by class and hence state of one object could be altered by another object.
Some limitations with Unit testing as not all mocking framework facilitate mocking them. Power mock allows but Mockito doesn't
Help us improve. Please let us know the company, where you were asked this question :
Ans. Better Control - If the value is being used at multiple locations , that can be controlled better from single place. Any change would only require making single change.
Meaning , Aliasing and Better Readability - Sometimes its easy to read the value by its meaning or alias ( 0 as ZERO or 0 as NEUTRAL_VALUE ).
Help us improve. Please let us know the company, where you were asked this question :