Core Java - Interview Questions and Answers for 'Variable' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

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.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'Variable' - 44 question(s) found - Order By Rating

next 30
 Q1. Can the value of a static variable be changed ? Core Java
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


 Q2. Can we load environment variables into Properties in Java ? How ?Core Java
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


 Q3. Difference between System.getEnv and System.getProperties ?Core Java
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


 Q4. What is the default data type of state variables in Clonable interface and Serializable interface ?Core Java
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


 Q5. Why the following code produced this output ?

int x = 5;
int y = x;
y = 10;
      
System.out.println(x); // prints 5 and not 10
Core Java
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


 Q6. How different is it when final applied to variables and object references ? Core Java
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


 Q7. When we say final x = 10, is the reference final or the value ?Core Java
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


 Q8. Will the following code compile ?

final HashMap hm = new HashMap();
hm.put("Ravi", 221);
Core Java
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


 Q9. How do we define constant variables in Java ?Core Java
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


 Q10. Why can't we declare on outer class as private ?Core Java
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


 Q11. What happens to private variables during inheritance ?Core Java
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


 Q12. What is a blank final variable ? Can we initialize a blank final variable ?Core Java
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


 Q13. Would you consider creating a constant name for "null" ?

Core Java
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


 Q14. If we have to maintain a value "31" denoting "number of days in a month" as constant variable, Which of the following is the best constant name and Why ?

THIRTY_ONE
NUMBER_OF_DAYS_IN_MONTH
NUMBER_OF_DAYS_IN_MONTH_31
Core Java
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


 Q15. If we have to maintain a value "7" denoting "number of days in a week" as constant variable, Which of the following is the best constant name and Why ?

SEVEN
NUMBER_OF_DAYS_IN_WEEK
NUMBER_OF_DAYS_IN_WEEK_7
Core Java
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


 Q16. What will we get if we try to print local variable that hasn't been initialized ?Core Java
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


 Q17. Which environment variables do we need to set in order to run Java programs?Core Java
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


 Q18. What are different types of variables a class can have ?Core Java
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


 Q19. What is the advantage of using static final or constant variables in Java ? Core Java
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


 Q20. What could be the problems due to the usage of static methods and elements ? Core Java
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


 Q21. Can we access private members of the parent class ? i.e Are private members of the class visible in the derived class ?Core Java
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


 Q22. What is a static variable in Java ?Core Java
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


 Q23. 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 :   

   Like         Discuss         Correct / Improve     this keyword  member elements  member variables


 Q24. What are different types of variables in Java ?
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


 Q25. Can static method access instance variables ?Core Java
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.
  Q26. What is a Final Variable ?Core Java
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


 Q27. When are static variables loaded in memory ?Core Java
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


 Q28. Can we serialize static variables ?Core Java
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


 Q29. Why do member variables have default values whereas local variables don't have any default value ?
Core Java
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


 Q30. 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 :   

   Like         Discuss         Correct / Improve     java   arrays   loops   variables   basic interview question


next 30

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: