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 'Variables' - 44 question(s) found - Order By Newest | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  java   statically typed language   variable declaration Asked in 7 Companies basic   frequent | ||||
| ||||
Ans. Advantages Can do meta object operations ( like validating something before creating objects , keep count of number of objects ) Can do operations which have nothing to do with objects but still you want them to be tied to Class. Disadvantages Commonly used to static variables sometime leads to problems due to access by different objects. Are not tied to objects so doesn't reflect pure Object Oriented approach. Needs to be synchronized so as to avoid update conflicts by mutiple objects and threads. Some limitation in testing as not all frameworks have facility to mock them. Powermock has but Mockito doesnt | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   static   static variables   static methods | ||||
Advanced level question usually asked to senior developers , leads and architects. | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  java   java keywords   volatile   synchronization   compiler optimization   variable caching   architecture Asked in 3 Companies expert | ||||
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. Arrays are of fixed size and have to reserve memory prior to use. Hence if we don't know size in advance arrays are not recommended to use. Arrays can store only homogeneous elements. Arrays store its values in contentious memory location. Not suitable if the content is too large and needs to be distributed in memory. There is no underlying data structure for arrays and no ready made method support for arrays, for every requriment we need to code explicitly | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   arrays   disadvantages of arrays   arrays memory   arrays vs variables | ||||
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 : | ||||
Like Discuss Correct / Improve  java   oops   static   static block   final variable | ||||
Try 1 Question(s) Test | ||||
| ||||
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. 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. 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. 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 | ||||
| ||||
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 | ||||
| ||||
Ans. No. Global variables are not allowed as it wont fit good with the concept of encapsulation. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   variables   global variables   yes no | ||||
| ||||
Ans. Variables used in a switch statement can only be a byte, short, int, or char ( and String with Java 7 and above ) | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   switch   variable   data type basic   rare | ||||
| ||||
Ans. CLASSPATH and PATH are the two variables. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   classpath   path   environment variables   jvm | ||||
Ans. Yes. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   static   static block   final variable   yes no | ||||
Try 1 Question(s) Test | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  constants   variable names   java conventions | ||||
| ||||
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 : | ||||
Like Discuss Correct / Improve  java   static   final variables   variable constants basic   frequent | ||||
| ||||
Ans. b,c and d i.e final , public and static | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   interface   interface variables | ||||
Ans. They can be initialized within static method | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   final   final variable   java keywords | ||||
| ||||
Ans. The field of an object | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   oop   instance variable   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. 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. 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. 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. 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. 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. 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. 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 | ||||