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

Search Java Interview Questions


 2137 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Variable' - 43 question(s) found - Order By Newest

 Q1. What do you mean by "Java is a statically typed language" ?Core Java
Admin
info@buggybread.com
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      basic        frequent


 Q2. What are the advantages and disadvantages of static variables and static methods ?Core Java
Admin
info@buggybread.com
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


 Q3. What are the disadvantages of using arrays ?Core Java
Admin
info@buggybread.com
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


Advanced level question usually asked to senior developers , leads and architects.
 Q4. How does volatile affect code optimization by compiler?Core Java
Admin
info@buggybread.com
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     Embedded security  Driver engineer  Sokrati      expert


 Q5. What are different types of variables a class can have ?Core Java2017-01-07 16:58:43

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


Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q6. What is a Final Variable ?Core Java
Anonymous
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     IBM  Goldman Sachs  TrueCar  Intertek  RedMane  Antra  HCL Technologies  Tata Consultancy (TCS)  Accenture  Forte Group  Mastercard      basic        frequent

Try 1 Question(s) Test


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


 Q8. Can we serialize static variables ?Core Java
Anonymous
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     Deloitte      intermediate        rare

Try 2 Question(s) Test


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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. Can static method access instance variables ?Core Java
Anonymous
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     Infoview Technologies      basic        frequent


 Q11. What is the advantage of using arrays over variables ?
Admin
info@buggybread.com
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


 Q12. Are there any global variables in Java, which can be accessed by other part of your program?
Admin
info@buggybread.com
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


 Q13. What data type Variable can be used in a switch statement ?Core Java
Admin
info@buggybread.com
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


 Q14. What environment variables do I need to set on my machine in order to be able to run Java programs?
Admin
info@buggybread.com
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


 Q15. 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);
}
}
Admin
info@buggybread.com
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


 Q16. Will static block for Test Class execute in the following code ?

class Test
{
static
{
System.out.println("Executing Static Block.");
}
public final int param=20;

public int getParam(){
return param;
}
}

public class Demo
{
public static void main(String[] args)
{
System.out.println(new Test().param);
}
}
Admin
info@buggybread.com
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


 Q17. What are the conventions regarding constant variables or static final variables ?Core Java
admin
info@buggybread.com
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


 Q18. Why is a constant defined as a static final in Java?Core Java
Admin
info@buggybread.com
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


 Q19. Variables of an interface are intrinsically ...

a. transient
b. final
c. public
d. static
Anonymous
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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q20. 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
Anonymous
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


 Q21. What is an instance variable?

a. An instance or object of an class
b. The field of an object
c. Any variable of a class
d. None of the above
Anonymous
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


 Q22. What are different types of variables in Java ?2016-05-26 14:11:45

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     CaprusIT      basic        frequent


 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 ?2016-05-29 10:18:38

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 is a static variable in Java ?Core Java2016-07-19 11:20:35

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     IBM India      basic        frequent

Try 1 Question(s) Test


 Q25. Can we access private members of the parent class ? i.e Are private members of the class visible in the derived class ?Core Java2016-08-04 09:24:34

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   members visibility      basic        frequent


 Q26. What could be the problems due to the usage of static methods and elements ? Core Java2016-12-20 10:31:40

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


 Q27. What is the advantage of using static final or constant variables in Java ? Core Java2016-12-20 10:52:26

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


 Q28. Which environment variables do we need to set in order to run Java programs?Core Java2017-01-12 10:55:08

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     QUADRATIC SYSTEMS  Computer sciences corporation (csc)  HCL Technologies      basic        frequent


 Q29. What will we get if we try to print local variable that hasn't been initialized ?Core Java2017-04-04 14:40:31

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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q30. 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 Java2017-05-09 19:33:04

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


 Q31. 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 Java2017-05-09 19:36:14

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


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

Core Java2017-05-09 19:40:03

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


 Q33. What is a blank final variable ? Can we initialize a blank final variable ?Core Java2017-05-13 09:02:14

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


 Q34. What happens to private variables during inheritance ?Core Java2017-06-07 16:35:35

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


 Q35. Why can't we declare on outer class as private ?Core Java2017-06-07 16:41:57

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


 Q36. How do we define constant variables in Java ?Core Java2017-06-23 08:11:29

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


 Q37. Will the following code compile ?

final HashMap hm = new HashMap();
hm.put("Ravi", 221);
Core Java2017-07-17 08:08:58

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     Polaris


 Q38. When we say final x = 10, is the reference final or the value ?Core Java2017-07-29 13:27:04

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


 Q39. How different is it when final applied to variables and object references ? Core Java2017-07-29 13:31:59

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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q40. 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 Java2017-07-29 13:34:13

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


 Q41. What is the default data type of state variables in Clonable interface and Serializable interface ?Core Java2017-09-25 08:33:18

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     Interglobal Services


 Q42. Write a Program to swap 2 variables ( using 3rd variable )Core Java2017-07-25 19:41:20

Ans.
int x = 1;
int y = 2;
int z = x;
x = y;
y = z;

System.out.println("x="+x);
System.out.println("y="+y);

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     swap 2 variables  code  coding


 Q43. Write a Program to swap two variables ( without using 3rd variable )2017-07-25 19:42:32

Ans.
int x = 1;
int y = 2;

x = x+y;
y = x-y;
x = x-y;

System.out.println("x="+x);
System.out.println("y="+y);

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     swap 2 variables




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

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:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: