3A Java Basics for Selenium - Java Variables - Interview Questions Part 1

Опубликовано: 07 Октябрь 2024
на канале: subbus tech
38
2

This is part 1 interview questions on Java Variables.
What is meant by a variable?
Variables are locations in memory that can hold values.
How are the variables declared?
Variables can be declared anywhere in the method definition and can be initialized during their declaration. They are commonly declared before usage at the beginning of the definition. Variables with the same data type can be declared together. Local variables must be given a value before usage.
How do you assign values to variables?
Values are assigned to variables using the assignment operator =.
What are the kinds of variables in java and what are their uses?
Java has four kinds of variables.
• Block Variables
• Local Variables
• Instance Variables
• Class Variables
Block Variables are used inside blocks like init blocks or for loops and are used to store information needed by a single block.
Local variables are used inside methods as temporary variables and are used to store information needed by a single method.
Instance variables (member variables) are used to define attributes or the state of a particular object and are used to store information needed by multiple methods in the objects.
Class variables are global to a class and to all the instances of the class and are useful for communicating between different objects of all the same class or keeping track of global states.
What is a literal? How many types of literals are there?
A literal represents a value of a certain type where the type describes how that value behaves. There are different types of literals namely:
a) number literals (1,23)
b) character literals (‘c’, ‘d’)
c) boolean literals (true, false)
d) string literals and etc. (“hello”)
What are local variables?
Local variables are those which are declared within methods. Local variables should be initialised before accessing them.
What are instance variables?
Instance variables are those which are defined at the class level without static keyword. Instance variables need not be initialized before using them as they are automatically initialized to their default values.
What is the scope or life time of instance variables?
When object is instantiated using new operator variables get allocated in the memory. Instance variables remain in memory till the instance gets garbage collected.
Explain scope or life time of local variables in java?
Local variables are variables which are defined inside a method. When the method is created local variables gets created in stack memory and this variable gets deleted from memory once the method execution is done.
What are reference variables in java?
Variables which are used to access objects in java are called reference variables.
Ex: Employee emp=new Employee();
In the above example emp is reference variable.
How long do primitive variables exist in memory? Define the various scopes of primitive variables?
After a primitive variable is declared and initialized; how long it lives in memory is dependent on the scope of the variable. Scope of a variable is determined based on where it is declared within a java class. Following are the various scopes of a variable in a java program based on where they are declared.

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.