0% found this document useful (0 votes)
3 views14 pages

Core Java For TCS IPA - Chapter 2 - Variables

Uploaded by

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

Core Java For TCS IPA - Chapter 2 - Variables

Uploaded by

maneruturaj18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CORE JAVA FOR TCS IPA – Chapter 2: Variables

(Detailed Notes + MCQs)

What is a Variable?
A variable is a named memory location used to store data. Every variable has:

• A data type (e.g., int, double, boolean)


• A name (identifier)
• A value

Example:

int age = 21;

Here:

• int → Data Type


• age → Variable Name
• 21 → Value

Types of Variables in Java


Java has three types of variables:

1. Local Variable
2. Instance Variable
3. Static Variable (Class Variable)

Understanding the difference between these is one of the most frequently tested concepts in TCS MCQs.

1. Local Variable
A local variable is declared inside a method, constructor, or block.

Example:

1
public class Test {
public static void main(String[] args) {

int x = 10;

[Link](x);

}
}

Here,

x is a local variable.

Characteristics

✔ Declared inside methods or blocks

✔ Memory allocated only when the method executes

✔ Destroyed after the method finishes

✔ Accessible only inside that method

✔ No default value

You must initialize a local variable before using it.

Example:

public class Test {

public static void main(String[] args) {

int x;

[Link](x);

Output:

2
Compile-Time Error
variable x might not have been initialized

Reason:

Local variables do not receive default values from Java.

2. Instance Variable
An instance variable is declared inside a class but outside all methods, constructors, and blocks.

Example:

public class Student {

int marks = 80;

marks is an instance variable.

Each object gets its own copy.

Example:

Student s1 = new Student();

Student s2 = new Student();

Memory:

s1 → marks = 80

s2 → marks = 80

If s1 changes its marks:

[Link] = 90;

3
Now:

s1 → 90

s2 → 80

Each object has its own separate copy.

Characteristics

✔ Declared inside class

✔ Outside methods

✔ Created when object is created

✔ Destroyed when object is destroyed

✔ Gets default value automatically

3. Static Variable (Class Variable)


Declared using the static keyword.

Example:

public class Student {

static String college = "KIT";

Now,

Every object shares the same variable.

Example:

4
Student s1 = new Student();

Student s2 = new Student();

Memory:

college

KIT

s1

s2

Only one copy exists.

If

[Link] = "MIT";

Then

Both objects see

MIT

Characteristics

✔ One copy for entire class

✔ Shared among all objects

✔ Memory allocated once when class loads

✔ Saves memory

✔ Gets default values automatically

5
Comparison Table
Feature Local Variable Instance Variable Static Variable

Declared Inside method Inside class Inside class using static

Memory Stack Heap Method Area (Class Area)

Default Value No Yes Yes

Scope Method only Entire object Entire class

Lifetime Until method ends Until object is destroyed Until program ends/class unloaded

Shared No No Yes

Default Values of Variables


Only

✔ Instance Variables

✔ Static Variables

receive default values.

Local variables never receive default values.

Data Type Default Value

byte 0

short 0

int 0

long 0L

float 0.0f

double 0.0

char '\u0000' (null character)

boolean false

Object null

6
Example

public class Test {

int x;

boolean flag;

char ch;

String name;

public static void main(String[] args) {

Test t = new Test();

[Link](t.x);

[Link]([Link]);

[Link]((int)[Link]);

[Link]([Link]);

Output:

false

null

Explanation:

Java automatically initializes instance variables.

7
Scope of Variables
Local Variable
Accessible only inside its method.

Instance Variable
Accessible through object.

Static Variable
Accessible using class name.

Example:

[Link]

Memory Allocation
Local Variable

Stack Memory

Instance Variable

Heap Memory

Static Variable

Method Area (Class Area)

8
Frequently Asked TCS MCQs
MCQ 1

Which variable does NOT get a default value?

A) Instance Variable

B) Static Variable

C) Local Variable

D) Object Variable

Answer: C

Explanation: Local variables must be initialized before use.

MCQ 2

Which variable is shared among all objects?

A) Local Variable

B) Instance Variable

C) Static Variable

D) Constructor

Answer: C

MCQ 3

Where is a local variable declared?

A) Inside method

B) Outside class

C) Inside package

D) Inside JVM

9
Answer: A

MCQ 4

Default value of an int instance variable?

A) null

B) 0

C) 0.0

D) Undefined

Answer: B

MCQ 5

Default value of boolean?

A) true

B) false

C) null

D) 0

Answer: B

MCQ 6

Default value of char?

A) '0'

B) '\u0000'

C) null

D) Empty String

10
Answer: B

MCQ 7

Default value of Object reference?

A) 0

B) false

C) null

D) '\u0000'

Answer: C

MCQ 8

Which memory stores local variables?

A) Heap

B) Stack

C) Method Area

D) JVM Cache

Answer: B

MCQ 9

Which memory stores instance variables?

A) Stack

B) Heap

C) Registers

D) Cache

11
Answer: B

MCQ 10

Which memory stores static variables?

A) Stack

B) Heap

C) Method Area (Class Area)

D) CPU Registers

Answer: C

MCQ 11 (Output Based)

public class Test {

int x;

public static void main(String[] args) {

Test t = new Test();

[Link](t.x);

Options:

A) Compile Error

B) null

C) 0

D) Garbage Value

Answer: C

12
Explanation: x is an instance variable, so it gets the default value 0 .

MCQ 12 (Output Based)

public class Test {

public static void main(String[] args) {

int x;

[Link](x);

Options:

A) 0

B) null

C) Compile-Time Error

D) Garbage Value

Answer: C

Explanation: Local variables must be initialized before use.

Memory Trick
LIS Rule

• L = Local → No default value


• I = Instance → Own copy for every object
• S = Static → Shared by all objects

13
One-Minute Revision
✔ Java has 3 variable types: Local, Instance, Static.

✔ Local variables:

• Declared inside methods


• Stored in Stack
• No default value

✔ Instance variables:

• Declared inside class


• Stored in Heap
• Separate copy for each object
• Default values available

✔ Static variables:

• Declared using static


• Shared among all objects
• Stored in Method Area (Class Area)
• Default values available

✔ Remember:

• Local → Stack → No Default


• Instance → Heap → Own Copy
• Static → Class Area → Shared Copy

14

You might also like