Java - Variables - Interview Questions

Java variables is an important, fundamental and core java programming topic. Many FAQs in Java interviews are based on your knowledge of Java variables. These include questions on topics such as primitive variables vs reference variables, variables types, static vs non-static variables, access modifiers and non-access modifiers that can be applied to variables, scope of variables, transient variables, volatile variables, variables vs primitive data types etc.

A thorough understanding of variables is absolutely necessary for being successful in your your Java interview; specially for beginner to mid level positions; and for hands-on programmer, developer, architect or manager roles.

Below interview questions, answers, coding exercises and tips will help you get grounded in the fundamental concepts of Java variables. A solid foundation on variables will help you out in clearing the screening level as well as coding levels in later part of your interview process.

Important keywords are provided at the end of the questions. Review them, make them a part of Java vocabulary, and talk about them confidently during your interview process.

What is the difference between primitive variables and reference variables?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ American Express, Calsoft, Citadel, Fidelity Investments, Morgan Stanley, NVIDIA, NVR, Synergy,

There are basically two different kinds of variables in Java programming language - Primitive variables and Reference variables. Primitive variables contain primitive literal values, where as reference variables contain a reference to an Object.

class MyClass {
//Primitive variable declaration - var1,
//var1 contains literal value 123.
int var1 = 123;

//Reference variable declaration - var2,
//var2 contains reference to object of type 'Box'.
Box var2 = new Box();
}

What are the different kinds of variables defined in java programming language?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ BT, Cerner, DEKA, EchoStar, Goldman Sachs, HCL, Infogain, Intuit, Karsum, Norfolk Southern,

There are basically two different kinds of variables in Java programming language - Primitive variables and Reference variables. Primitive variables contain primitive literal values, where as reference variables contain a reference to an Object.

Based on scope, variables can be of four different types - Class variables, Instance variables, Local variables and Parameters. Scope of a variable is determined based on where it is declared within a java class.

1. Class variable (Static fields) - Class variables are variables declared within the class body, outside of any methods or blocks, and declared with 'static' keyword.

Class variables have the longest scope. They are created when the class is loaded, and remain in memory as long as the class remains loaded in JVM.

2. Instance variables (Non-static fields) - Instance variable are variables declared within the class body, outside of any method or block, and declared without 'static' keyword.

Instance variables have the second highest scope. Instance variables are created when a new class instance is created, and live until the instance is removed from memory.

3. Local Variables - Local variables are variables declared within a method body. They live only as long as the method in which it is declared remains on the stack.

4. Block variables - Block variables are variables declared within a block such as an init block or within a for loop. They live only during the execution of the block and are the shortest living variables.

class MyClass {
//Static variable
static String string1 = 'test string 1';
//Instance variable
String string2 = 'test string 2';
//Block variable in init block
{String string3 = 'test string 3'}
void perform() {
//Local variable
String string4 = 'test string 4'
//Block variable in for loop
for (int i=0; i < 4; i++) {...}
}
}

What are static variables in Java programming language?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Abhra, Apple, Bloomberg LP, BMW, Broadcom, Citi, Citrix, Coviam, Equifax, F5 Networks, FedEx, FreshWorks, Google, Lenovo, Marlabs, Nokia, Nvidia, PayPal, Qualcomm, Subex, Synapse, Home Depot, Tradeweb, Twilo, Wipro,

Static variables (or fields) are variables declared within the class body, outside of any methods or blocks, and declared with 'static' keyword.

Static variables have the longest scope. They are created when the class is loaded, and remain in memory as long as the class remains loaded in JVM.

Static variables are,essentially, global variables. A single copy of the static variable is created and shared among all objects at class level. All instances of the class share the same static variable.

A static variable can be accessed using the class, and without creating an object instance.

Class variables are stored on the heap.

class MyClass {
//Static variable
static String string1 = 'test string 1';
}

What are instance variables in Java programming language?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Altisource, Cerner, DraftKings, Genpact, Reddwerks, Unbxd,

Instance variable are variables declared within the class body, outside of any method or block, and declared without 'static' keyword.

Instance variables have the second highest scope. Instance variables are created when a new class instance is created, and live until the instance is removed from memory.

Instance variables are stored on the heap.

class MyClass {
//instance variable
String string1 = 'test string 1';
}

What are local variables in Java programming language?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Arista Networks, Asana, Bloomberg L.P., Broadcom, Capgemini, Cisco Systems, eBay, Global Relay, Intel, Mathworks, Nuance, Qualcomm, Splunk, Veeva Systems, VMware, Worldquant, Yahoo,

Local variables are variables declared within a method body.

Local variables live only as long as the method in which it is declared remains on the stack.

Local variables are stored on the stack.

class MyClass {
void perform() {
//Local variable
Integer speed = 100;
}
}
Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

What are block variables in Java programming language?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Arista Networks, Asana, Broadcom, Capgemini, Cisco Systems, eBay, Intel, Mathworks, Qualcomm, Splunk, Veeva Systems, VMware, Worldquant,

Block variables are variables declared within a block such as an init block or within a for loop.

Block variables live only during the execution of the block and are the shortest living variables...

*** See complete answer and code snippet in the Java Interview Guide.

What are final variables in Java programming language?

 FAQKey Concept

Final variables are variables declared with keyword 'final'. Once a value is assigned to a final variable it cannot be changed.

If final variable is a primitive variable, the primitive literal cannot be changed once it is assigned to the primitive variable...

*** See complete answer and code snippet in the Java Interview Guide.

What are transient variables in Java programming language?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Abhra, Calypso Technology, Citi, Eroad, RPM Technologies, Velopsys Technologies,

Transient variable is a variable whose value is not serialized during serialization of the object. During de-serialization of the object, transient primitive variables are initialized to their default values...

*** See complete answer and code snippet in the Java Interview Guide.

What are volatile variables in Java programming language?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ NVIDIA, OpenText, Qualcomm, Square, Uber, Valeo, ZF Group,

Volatile variables are relevant in multi-threaded Java programming, in which multiple threads access the variables of an object. A volatile variable is declared with the keyword 'volatile'...

*** See complete answer and code snippet in the Java Interview Guide.

 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example