0% found this document useful (0 votes)
9 views6 pages

Variables IQs

The document discusses different types of variables in Java including instance variables, static variables, and local variables. It defines each type of variable and describes how they are stored, initialized, and accessed. It also discusses variable argument methods and compares single dimensional arrays to variable argument parameters.

Uploaded by

Sahil LBEP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Variables IQs

The document discusses different types of variables in Java including instance variables, static variables, and local variables. It defines each type of variable and describes how they are stored, initialized, and accessed. It also discusses variable argument methods and compares single dimensional arrays to variable argument parameters.

Uploaded by

Sahil LBEP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

============= Variables IQs ===============

Q What is a Variable in Java?


A variable in Java is a named memory location used to store data temporarily
during program execution. Variables have a data type that determines the type of
data they can hold and operations that can be performed on them. In Java, variables
must be declared before they can be used.

Q Types of Variables

Division 1 : Based on the type of value represented by a variable all


variables aredivided into 2 types. They are:

1. Primitive variables: Primitive variables can be used to represent


primitive values
2. Reference variables: Reference variables can be used to refer
objects

Division 2 : Based on the behaviour and position of declaration all variables


are divided into the following 3 types.

1. Instance variables
2. Static variables
3. Local variables

Q Instance variables

>. If the value of a variable is varied from object to object such type of
variables are called instance variables.

>. For every object a separate copy of instance variables will be created.

>. Instance variables will be created at the time of object creation and
destroyed at the time of object destruction hence the scope of instance variables
is exactly same as scope of objects.

>. Instance variables will be stored on the heap as the part of object.

>. Instance variables should be declared with in the class directly but
outside of anymethod or block or constructor.

>. Instance variables can be accessed directly from Instance area. But cannot
be accessed directly from static area.

>. But by using object reference we can access instance variables from static
area.

>. For the instance variables it is not required to perform initialization


JVM will always provide default values.

>. Instance variables also known as object level variables or attributes.

Q Static variables

>. If the value of a variable is not varied from object to object such type
of variablesis not recommended to declare as instance variables. We have to declare
such type of variables at class level by using static modifier.
>. In the case of instance variables for every object a separate copy will be
created but in the case of static variables for entire class only one copy will be
created and shared by every object of that class.

>. Static variables will be crated at the time of class loading and destroyed
at the time of class unloading hence the scope of the static variable is exactly
same as the scope of the .class file.

>. Static variables will be stored in method area. Static variables should be
declared with in the class directly but outside of any method or block or
constructor.

>. Static variables can be accessed from both instance and static areas
directly.

>. We can access static variables either by class name or by object reference
but usage of class name is recommended.

>. But within the same class it is not required to use class name we can
access directly.

>. For the static variables it is not required to perform initialization


explicitly, JVM will always provide default values.

>. Static variables also known as class level variables or fields.


java TEST
1. Start JVM.
2. Create and start Main Thread by JVM.
3. Locate(find) Test.class by main Thread.
4. Load Test.class by main Thread. // static variable creation
5. Execution of main() method.
6. Unload Test.class // static variable destruction
7. Terminate main Thread.
8. Shutdown JVM.

Q Local variables:

>. Some times to meet temporary requirements of the programmer we can declare
variables inside a method or block or constructors such type of variables are
called local variables or automatic variables or temporary variables or stack
variables.

>. Local variables will be stored inside stack.

>. The local variables will be created as part of the block execution in
which it is declared and destroyed once that block execution completes. Hence the
scope of the local variables is exactly same as scope of the block in which we
declared.

>. The local variables will be stored on the stack.

>. For the local variables JVM won't provide any default values compulsory we
should perform initialization explicitly before using that variable.

Example:
class Test
{
public static void main(String[] args)
{
int x;
if(args.length>0){
x=10;
}
System.out.println(x); //C.E:variable x might not have been
initialized
}
}

>. It is never recommended to perform initialization for the local variables


insidelogical blocks because there is no guarantee of executing that block always
at runtime.

>. It is highly recommended to perform initialization for the local variables


at the time of declaration at least with default values.

Note: The only applicable modifier for local variables is final. If we are
using any other modifier we will get compile time error.

Q Diff b/w Instance, Static & Local Variables

1. For the static and instance variables it is not required to perform


initialization
explicitly JVM will provide default values. But for the local variables JVM
won't
provide any default values compulsory we should perform initialization
explicitly
before using that variable.

2. For every object a separate copy of instance variable will be created


whereas for
entire class a single copy of static variable will be created. For every
Thread a
separate copy of local variable will be created.

3. Instance and static variables can be accessed by multiple Threads


simultaneously
and hence these are not Thread safe but local variables can be accessed by
only
one Thread at a time and hence local variables are Thread safe.

4. If we are not declaring any modifier explicitly then it means default


modifier but
this rule is applicable only for static and instance variables but not local
variable.

Q Var- arg methods (variable no of argument methods)

Until 1.4v we can't declared a method with variable no. Of arguments.


 If there is a change in no of arguments compulsory we have to define a new
method.
 This approach increases length of the code and reduces readability.
 But from 1.5 version onwards we can declare a method with variable no. Of
arguments such type of methods are called var-arg methods.

eg: m1(int...x){}
We can call or invoke this method by passing any no. Of int values including
zeronumber also.

Internally var-arg parameter implemented by using single dimensional array


hence within the var-arg method we can differentiate arguments by using index.

>. if we mix var-arg parameter with general parameter then var-arg parameter
should be
the last parameter.
Example:
methodOne(int a,int... b) //valid
methodOne(int... a,int b) //(invalid)

>. With in the var-arg method we can take only one var-arg parameter. i.e.,
if we are
trying to more than one var-arg parameter we will get CE.
Example:
methodOne(int... a,int... b) //(invalid)

Q Which of the following var-arg method declarations are valid?


1. methodOne(int... x) (valid)
2. methodOne(int ...x) (valid)
3. methodOne(int...x) (valid)
4. methodOne(int x...) (invalid)
5. methodOne(int. ..x) (invalid)
6. methodOne(int .x..) (invalid)

Q class Test
{
public static void methodOne(int i)
{
System.out.println("general method");
}
public static void methodOne(int... i)
{
System.out.println("var-arg method");
}
public static void main(String[] args)
{
methodOne();//var-arg method
methodOne(10,20);//var-arg method
methodOne(10);//general method
}
}

In general var-arg method will get least priority that is if no other method
matched then
only var-arg method will get the chance this is exactly same as default case
inside a
switch.

Q class Test
{
public void methodOne(int[] i){}
public void methodOne(int... i){}
}
Output:
Compile time error.
Cannot declare both methodOne(int...) and methodOne(int[]) in Test
Q Single Dimensional Array Vs Var-Arg Method:

>. Wherever single dimensional array present we can replace with var-arg
parameter.

public static void main(String... args)


{
System.out.println("var-arg main method");//var-arg main method
}

>. Wherever var-arg parameter present we can't replace with single


dimensional array.

Q Which of the following main() method declarations are valid ?


1. public static void main(String args){} (invalid)
2. public synchronized final strictfp void main(String[] args){} (invalid)
3. public static void Main(String... args){} (invalid)
4. public static int main(String[] args){} //int return type we can't take
//(invalid)
5. public static synchronized final strictfp void main(String... args){}(valid)
6. public static void main(String... args){}(valid)
7. public void main(String[] args){}(invalid)

Q Coding Standards:

Coding standards for classes:


 Usually class names are nouns.
 Should starts with uppercase letter and if it contains multiple words
every inner
word should starts with upper case letter.

Coding standards for interfaces:


 Usually interface names are adjectives.
 Should starts with upper case letter and if it contains multiple
words every inner
word should starts with upper case letter.

Coding standards for methods:


 Usually method names are either verbs or verb-noun combination.
 Should starts with lowercase character and if it contains multiple
words every
inner word should starts with upper case letter.(camel case convention)

Coding standards for variables:


 Usually variable names are nouns.
 Should starts with lowercase alphabet symbol and if it contains
multiple words
every inner word should starts with upper case character.(camel case
convention)

Coding standards for constants:


 Usually constants are nouns.
 Should contain only uppercase characters and if it contains multiple
words then
these words are separated with underscore symbol.
 Usually we can declare constants by using public static final
modifiers.

Java bean coding standards:


A java bean is a simple java class with private properties and public
getter and setter
methods.

Coding standards for listeners:


To register a listener:
Method name should be prefixed with add.
1. public void addMyActionListener(MyActionListener l) (valid)

public void removeMyActionListener(MyActionListener l) (valid)

Q: Various Memory areas present inside JVM

1. Class level binary data includung static variables will be stored in


method area.
2. Objects and corresponding instance variables will be stored in Heap area.
3. For every method the JVM will create a Runtime stack all method calls
performed by that Thread and corresponding local variables will be stored in
that stack.
Every entry in stack is called Stack Frame or Action Record.
4. The instruction which has to execute next will be stored in the
corresponding PC
Registers.
5. Native method invocations will be stored in native method stacks.

You might also like