Search Interview Questions | 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. |
|
| ||||
Core Java - Interview Questions and Answers for 'Variable' - 44 question(s) found - Order By Rating | ||||
| ||||
Ans. Yes the value of a static variable can be changed. It's the final keyword that restrict the modification of value not static. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  static variables | ||||
| ||||
Ans. Yes new Properties().putAll(System.getenv()); | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java.util.properties  environment variables | ||||
| ||||
Ans. System.getEnv gets the environment variables where System.getProperties gets Java properties. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  System.getEnv  System.getProperties  environment variables | ||||
| ||||
Ans. Cloneable and Serializable are Marker Interfaces, So these are empty interfaces with no variables. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  cloneable  serializable  static variables Asked in 1 Companies | ||||
| ||||
Ans. Because when we say y = x, the value of x is copied to another memory location and then y points to that new memory location. Unlike object references that once assigned point to same object in memory, value is copied in case of variable assignment. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  variable assignment | ||||
| ||||
Ans. final when assigned to object references doesn't make them immutable. It means that the references cannot be de-referenced. final when applied to variables means that the value cannot be changed. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  final keyword  final variable | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  final keyword  final variables  references | ||||
| ||||
Ans. Yes, it will compile without error. final in this context means that the reference hm cannot be assigned to a new hash map but didn't restrict from changing the state of collection already held by hm. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  final  final variable Asked in 1 Companies | ||||
| ||||
Ans. By using static and final modifiers. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  constant variables   static final basic   frequent | ||||
| ||||
Ans. Making an outer class as private will make it inaccessible from anywhere and would make it useless. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  private variables | ||||
| ||||
Ans. Private variables are not inherited. They are simply ignored and not made part of the derived object body. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  private variables  inheritance  object oriented programming (oops)  oops concepts | ||||
| ||||
Ans. A final variable that is not initialized while declaration is a blank final variable. Yes, we can initialize it within a constructor. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  blank final variable  final variable | ||||
| ||||
Ans. Objective of constant variable is to provide a context to the value and provide a single point of change. In terms of context, i don't see much need for having constant name for null as null is already self explanatory. In terms of providing single point of change, it doesn't make much sense as it would necessitate that all such placeholders either hold null or a common value. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  constant variables  static final variable  java conventions  java naming conventions | ||||
| ||||
Ans. NUMBER_OF_DAYS_IN_MONTH_31 Constant should be such that it uniquely identifies the value it holds and context of the value. THIRTY_ONE uniquely identifies the value but doesn't express the context. NUMBER_OF_DAYS_IN_MONTH expresses the context but doesn't specify the value clearly as different month can have different number of days. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  constant variables  static final variable  java conventions  java naming conventions | ||||
| ||||
Ans. NUMBER_OF_DAYS_IN_WEEK Constant should be such that it uniquely identifies the value it holds and context of the value. SEVEN uniquely identifies the value but doesn't express the context. Though NUMBER_OF_DAYS_IN_WEEK and NUMBER_OF_DAYS_IN_WEEK_7 makes sense in terms of unique value identification and context but 7 at the end of constant name appears useless as all weeks have 7 days and hence it's the only value it can hold. If it would have been "number of days in a month", it would have made sense to include number of days at the end like NUMBER_OF_DAYS_IN_MONTH_30 NUMBER_OF_DAYS_IN_MONTH_31 NUMBER_OF_DAYS_IN_MONTH_27 NUMBER_OF_DAYS_IN_MONTH_28 | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  constant variables  static final variable  java conventions  java naming conventions | ||||
| ||||
Ans. It will print the garbage value as compiler doesn't provide a default value to local variables. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  local variable  default values basic | ||||
| ||||
Ans. PATH, CLASSPATH and JAVA_HOME | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  environment variables  path  classpath  java_home Asked in 3 Companies basic   frequent | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  java variables | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  static final  final variable  variable constants | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  static  static method  static variables | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  private members   private methods   private variables   inheritance  object oriented programming (oops)  oops concepts   members visibility   inner classes  nexted classes basic   frequent | ||||
| ||||
Ans. It is a class level variable that is shared among the objects of that class. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  static   static variables   class level variables Asked in 1 Companies basic   frequent | ||||
Try 1 Question(s) Test | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  this keyword  member elements  member variables | ||||
| ||||
Ans. 1. Function Local Variables 2. Instance Variables 3. Class variables or static variables | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  variables Asked in 1 Companies basic   frequent | ||||
| ||||
Ans. Though Static methods cannot access the instance variables directly, They can access them using instance handler. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  static   static method   java   oop   variables Asked in 1 Companies basic   frequent | ||||
Very frequently asked. Favorite question in Walk in Drive of many Indian service companies. | ||||
| ||||
Ans. Final variable is a variable constant that cannot be changed after initialization. | ||||
Sample Code for final variable | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  oops   java   final   final variable   basic interview question Asked in 12 Companies basic   frequent | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. They are loaded at runtime when the respective Class is loaded. | ||||
Sample Code for static variable | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   static   static variable   memory intermediate | ||||
| ||||
Ans. No. Only Object and its members are serialized. Static variables are shared variables and doesn't correspond to a specific object. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  serialization   java   oops   static   static variables Asked in 1 Companies intermediate   rare | ||||
Try 2 Question(s) Test | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  java   jvm   memory management   variables   stack memory   heap memory   default values expert | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  java   arrays   loops   variables   basic interview question | ||||