0% found this document useful (0 votes)
5 views31 pages

Variables Practice Questions

The document contains a comprehensive list of conceptual and coding questions related to variables in Java, covering topics such as variable types, scope, lifetime, and initialization. It includes 100 questions, with the first 50 focusing on theoretical concepts and the latter 50 on practical coding exercises. Additionally, there are beginner and intermediate level programming tasks designed to reinforce the understanding of variable usage in Java.

Uploaded by

tanayakhirabdi20
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)
5 views31 pages

Variables Practice Questions

The document contains a comprehensive list of conceptual and coding questions related to variables in Java, covering topics such as variable types, scope, lifetime, and initialization. It includes 100 questions, with the first 50 focusing on theoretical concepts and the latter 50 on practical coding exercises. Additionally, there are beginner and intermediate level programming tasks designed to reinforce the understanding of variable usage in Java.

Uploaded by

tanayakhirabdi20
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

VARIABLES

🧠 Conceptual Questions (1–50)


1.​ What is a variable in Java?​

2.​ Why are variables required in Java programs?​

3.​ What is the syntax to declare a variable in Java?​

4.​ What is initialization of a variable?​

5.​ What is the difference between declaration and initialization?​

6.​ What are the different types of variables in Java?​

7.​ What is a local variable?​

8.​ What is an instance variable?​

9.​ What is a static variable?​

10.​Where are local variables stored?​

11.​Where are instance variables stored?​

12.​Where are static variables stored?​

13.​What is the default value of local variables?​

14.​What is the default value of instance variables?​

15.​What is the default value of static variables?​

16.​What is scope of a local variable?​

17.​What is scope of an instance variable?​

18.​What is scope of a static variable?​

19.​What is lifetime of a local variable?​


20.​What is lifetime of an instance variable?​

21.​What is lifetime of a static variable?​

22.​Can two variables have same name in Java?​

23.​What is shadowing of variables?​

24.​What is hiding of variables?​

25.​Difference between local and instance variable.​

26.​Difference between instance and static variable.​

27.​Difference between static and local variable.​

28.​Can we use a variable without initialization?​

29.​What is final variable?​

30.​Can final variable be changed?​

31.​What is naming convention for variables?​

32.​What is implicit initialization?​

33.​What is explicit initialization?​

34.​What is the default value of reference type instance variable?​

35.​Can local variables be static?​

36.​Can instance variables be accessed in static method?​

37.​Can static variables be accessed in non-static method?​

38.​Why local variables must be initialized before use?​

39.​What is memory allocation for variables?​

40.​What is class variable?​

41.​Why static variables are shared?​


42.​What happens if we do not initialize instance variable?​

43.​Can we declare multiple variables in one line?​

44.​What is variable scope?​

45.​What is variable lifetime?​

46.​What is this keyword used for with variables?​

47.​Can variables be declared inside a constructor?​

48.​Can we use same variable name in different methods?​

49.​What is compile-time initialization?​

50.​What is run-time initialization?​

💻 Coding & Output-Based Questions (51–100)


51.​Write a program to declare and initialize a local variable.​

52.​Write a program to declare and initialize an instance variable.​

53.​Write a program to declare and initialize a static variable.​

54.​Write a program to access instance variable using object.​

55.​Write a program to access static variable using class name.​

56.​Write a program to show difference between local and instance variable.​

57.​Write a program to show difference between instance and static variable.​

58.​Write a program to demonstrate variable shadowing.​

59.​Write a program to demonstrate variable hiding.​

60.​Write a program to demonstrate final variable.​


61.​Write a program to show that local variable must be initialized.​

62.​Write a program to show default value of instance variable.​

63.​Write a program to show default value of static variable.​

64.​Write a program to declare multiple variables in one line.​

65.​Write a program to demonstrate scope of local variable.​

66.​Write a program to demonstrate scope of instance variable.​

67.​Write a program to demonstrate scope of static variable.​

68.​Write a program to demonstrate lifetime of local variable.​

69.​Write a program to demonstrate lifetime of instance variable.​

70.​Write a program to demonstrate lifetime of static variable.​

71.​Write a program to access instance variable inside static method.​

72.​Write a program to access static variable inside instance method.​

73.​Write a program to declare variable inside constructor.​

74.​Write a program to use this keyword to refer instance variable.​

75.​Write a program to use class name to refer static variable.​

76.​Write a program to show that final variable cannot be changed.​

77.​Predict the output:​

int a = 10;

[Link](a);

78.​Predict the output:​

int a = 5;

int b = a;
b = 20;

[Link](a);

79.​Predict the output:​

static int x = 10;

public static void main(String[] args){

[Link](x);

80.​Predict the output:​

class Test{

int a = 10;

public static void main(String[] args){

Test t = new Test();

[Link](t.a);

81.​Predict the output:​

class Test{

int x = 10;

public static void main(String[] args){

int x = 20;

[Link](x);

}
82.​Predict the output:​

class Test{

static int x = 10;

public static void main(String[] args){

int x = 20;

[Link](x);

83.​Predict the output:​

class Test{

int x = 10;

public static void main(String[] args){

Test t1 = new Test();

Test t2 = new Test();

t1.x = 20;

[Link](t2.x);

84.​Predict the output:​

class Test{

static int x = 10;

public static void main(String[] args){

Test t1 = new Test();


Test t2 = new Test();

t1.x = 20;

[Link](t2.x);

85.​Predict the output:​

class Test{

final int x = 10;

public static void main(String[] args){

Test t = new Test();

[Link](t.x);

86.​Predict the output:​

class Test{

int a;

public static void main(String[] args){

Test t = new Test();

[Link](t.a);

87.​Predict the output:​

class Test{
static int a;

public static void main(String[] args){

[Link](a);

88.​Predict the output:​

class Test{

public static void main(String[] args){

int a;

// [Link](a);

89.​Predict the output:​

class Test{

int a = 10;

void show(){

int a = 20;

[Link](a);

public static void main(String[] args){

Test t = new Test();

[Link]();

}
}

90.​Predict the output:​

class Test{

static int x = 10;

static void show(){

[Link](x);

public static void main(String[] args){

show();

91.​Write a program to show that static variable is shared among objects.​

92.​Write a program to show instance variable is separate for each object.​

93.​Write a program to declare local variable inside loop.​

94.​Write a program to declare variable inside if block.​

95.​Write a program to demonstrate block scope.​

96.​Write a program to demonstrate variable re-initialization.​

97.​Write a program to take runtime input and store in variable.​

98.​Write a program to swap two variables.​

99.​Write a program to count number of objects using static variable.​

100.​ Write a program to show difference between class variable and instance variable.

_________________________________________________________________________________
✅ Answers (1–50) – Conceptual
1.​ A variable is a container used to store data.​

2.​ To store and manipulate values during program execution.​

3.​ datatype variableName;​

4.​ Assigning a value to a variable.​

5.​ Declaration defines type & name; initialization assigns value.​

6.​ Local, Instance, Static.​

7.​ Variable declared inside a method/block.​

8.​ Variable declared inside class but outside method.​

9.​ Variable declared using static keyword.​

10.​Stack memory.​

11.​Heap memory.​

12.​Method area / Class area.​

13.​No default value (must be initialized).​

14.​0, 0.0, false, '\u0000', null.​

15.​Same as instance variable.​

16.​Within the block/method.​

17.​Throughout the object.​

18.​Throughout the program execution.​

19.​Till method execution ends.​

20.​Till object is destroyed.​


21.​Till program ends.​

22.​Yes, but in different scopes.​

23.​Local variable hides instance variable.​

24.​Static variable hides superclass static variable.​

25.​Local: method level; Instance: object level.​

26.​Instance: object-wise; Static: class-wise.​

27.​Static: class area; Local: stack.​

28.​Local – No; Instance & static – Yes.​

29.​Variable whose value cannot be changed.​

30.​No.​

31.​camelCase (e.g. totalMarks).​

32.​Automatic default initialization.​

33.​Manual initialization by programmer.​

34.​null​

35.​No.​

36.​Only using object reference.​

37.​Yes.​

38.​Because no default value is given.​

39.​Assigning memory for variable.​

40.​Static variable.​

41.​Because only one copy exists per class.​

42.​Default value is assigned.​


43.​Yes (int a,b,c;).​

44.​Area where variable is accessible.​

45.​Time for which variable exists.​

46.​To refer instance variable of current object.​

47.​Yes.​

48.​Yes.​

49.​Initialization at compile time.​

50.​Initialization during program execution.​

💻 Answers (51–100) – Coding / Output


51.​

int a = 10;

[Link](a);

52.​

class Test{

int x = 10;

53.​

class Test{

static int x = 20;

54.​
Test t = new Test();

[Link](t.x);

55.​

[Link](Test.x);

56.​

class Test{

int a=10;

void show(){

int b=20;

[Link](a+" "+b);

57.​

class Test{

int a=10;

static int b=20;

58.​

class Test{

int x=10;

void show(){

int x=20;

[Link](x);

}
}

59.​

class A{ static int x=10; }

class B extends A{ static int x=20; }

60.​

final int x=10;

61.​Compile-time error (local variable not initialized).​

62.​

class Test{ int a; }

Output → 0

63.​

class Test{ static int a; }

Output → 0

64.​

int a=1,b=2,c=3;

65.​

void show(){

int a=10;

66.​

class Test{ int x=10; }

67.​

class Test{ static int x=10; }

68.​Local variable exists till method ends.​


69.​Instance variable exists till object destroyed.​

70.​Static variable exists till program ends.​

71.​

class Test{

int a=10;

static void show(){

Test t=new Test();

[Link](t.a);

72.​

class Test{

static int a=10;

void show(){

[Link](a);

73.​

class Test{

Test(){

int x=10;

74.​
this.x = x;

75.​

Test.x;

76.​Compile-time error (final variable cannot change).​

77.​Output: 10​

78.​Output: 5​

79.​Output: 10​

80.​Output: 10​

81.​Output: 20​

82.​Output: 20​

83.​Output: 10​

84.​Output: 20​

85.​Output: 10​

86.​Output: 0​

87.​Output: 0​

88.​Compile-time error.​

89.​Output: 20​

90.​Output: 10​

91.​

class Test{

static int x=10;

public static void main(String[] args){

Test a=new Test();


Test b=new Test();

a.x=20;

[Link](b.x);

92.​

class Test{

int x=10;

public static void main(String[] args){

Test a=new Test();

Test b=new Test();

a.x=20;

[Link](b.x);

93.​

for(int i=0;i<5;i++){

[Link](i);

94.​

if(true){

int a=10;

95.​
{

int x=10;

96.​

int a=10;

a=20;

97.​

Scanner sc=new Scanner([Link]);

int a=[Link]();

98.​

int a=10,b=20;

int t=a; a=b; b=t;

99.​

class Test{

static int count=0;

Test(){ count++; }

public static void main(String[] args){

new Test(); new Test();

[Link](count);

100.​

class Test{

int a=10;
static int b=20;

🟢 Beginner Level (1–15)


1.​ Write a program to declare and print an integer variable.​

2.​ Write a program to declare and print a float variable.​

3.​ Write a program to declare and print a character variable.​

4.​ Write a program to declare and print a boolean variable.​

5.​ Write a program to declare and print multiple variables in one line.​

6.​ Write a program to initialize a variable and then update its value.​

7.​ Write a program to store your age in a variable and print it.​

8.​ Write a program to store two numbers in variables and print their sum.​

9.​ Write a program to swap two variables using a third variable.​

10.​Write a program to swap two variables without using a third variable.​

11.​Write a program to calculate area of a rectangle using variables.​

12.​Write a program to calculate simple interest using variables.​

13.​Write a program to store and print your name using String variable.​

14.​Write a program to convert Celsius to Fahrenheit using variables.​

15.​Write a program to calculate square and cube of a number using variables.​

🟡 Intermediate Level (16–35)


16.​Write a program to show difference between local and instance variables.​
17.​Write a program to show difference between instance and static variables.​

18.​Write a program to show default value of instance variables.​

19.​Write a program to show default value of static variables.​

20.​Write a program to demonstrate variable shadowing.​

21.​Write a program to demonstrate variable hiding (static variables).​

22.​Write a program to count number of objects using static variable.​

23.​Write a program to show that static variable is shared among objects.​

24.​Write a program to show that instance variable is separate for each object.​

25.​Write a program to show that local variable must be initialized before use.​

26.​Write a program to demonstrate scope of a variable inside a method.​

27.​Write a program to demonstrate block scope of variables.​

28.​Write a program to declare variable inside constructor and print it.​

29.​Write a program to demonstrate this keyword for instance variables.​

30.​Write a program to demonstrate class name usage for static variables.​

31.​Write a program to take runtime input and store it in a variable.​

32.​Write a program to show re-initialization of variable.​

33.​Write a program to use final variable and show it cannot be changed.​

34.​Write a program to print values of variables from different methods.​

35.​Write a program to demonstrate lifetime of local variable.​

🔴 Advanced Level (36–50)


36.​Write a program to show memory allocation of local, instance, and static variables.​

37.​Write a program to show variable scope in nested blocks.​

38.​Write a program to show how variables behave in recursion.​

39.​Write a program to demonstrate variable shadowing using constructor.​

40.​Write a program to show difference between compile-time and runtime initialization of


variables.​

41.​Write a program to show how static variable behaves in inheritance.​

42.​Write a program to demonstrate variable hiding between parent and child class.​

43.​Write a program to count total objects created using static variable.​

44.​Write a program to show that instance variable gets separate memory for each object.​

45.​Write a program to show that static variable gets single memory location.​

46.​Write a program to demonstrate use of final with instance variable.​

47.​Write a program to demonstrate use of final with static variable.​

48.​Write a program to demonstrate use of final with local variable.​

49.​Write a program to show effect of changing static variable using one object reference.​

50.​Write a program to demonstrate difference between variable scope and variable lifetime.

_________________________________________________________________________________

🟢 Beginner Level Solutions (1–15)


1.

int a = 10;

[Link](a);

2.
float f = 3.5f;

[Link](f);

3.

char c = 'A';

[Link](c);

4.

boolean b = true;

[Link](b);

5.

int a=10, b=20, c=30;

[Link](a+" "+b+" "+c);

6.

int x = 10;

x = 20;

[Link](x);

7.

int age = 21;

[Link](age);

8.

int a=10, b=20;

[Link](a+b);

9.

int a=10, b=20, t;


t=a; a=b; b=t;

[Link](a+" "+b);

10.

int a=10, b=20;

a=a+b; b=a-b; a=a-b;

[Link](a+" "+b);

11.

int l=10, w=5;

[Link](l*w);

12.

int p=1000, r=5, t=2;

[Link]((p*r*t)/100);

13.

String name = "Java";

[Link](name);

14.

double c=25;

double f=(c*9/5)+32;

[Link](f);

15.

int n=5;

[Link](n*n);

[Link](n*n*n);
🟡 Intermediate Level Solutions (16–35)
16.

class Test{

int x=10;

void show(){

int y=20;

[Link](x+" "+y);

17.

class Test{

int a=10;

static int b=20;

18.

class Test{

int a;

public static void main(String[] args){

Test t=new Test();

[Link](t.a);

}
19.

class Test{

static int a;

public static void main(String[] args){

[Link](a);

20.

class Test{

int x=10;

void show(){

int x=20;

[Link](x);

21.

class A{ static int x=10; }

class B extends A{ static int x=20; }

22.

class Test{

static int count=0;

Test(){ count++; }

23.
class Test{

static int x=10;

public static void main(String[] args){

Test a=new Test();

Test b=new Test();

a.x=20;

[Link](b.x);

24.

class Test{

int x=10;

public static void main(String[] args){

Test a=new Test();

Test b=new Test();

a.x=20;

[Link](b.x);

25.

int a;

// [Link](a); // compile error

26.

void show(){
int a=10;

27.

int a=10;

28.

class Test{

Test(){

int a=10;

[Link](a);

29.

class Test{

int x;

Test(int x){

this.x=x;

30.

[Link](Test.x);

31.

Scanner sc=new Scanner([Link]);


int a=[Link]();

32.

int a=10;

a=20;

33.

final int a=10;

// a=20; // error

34.

class Test{

int x=10;

void show(){ [Link](x); }

35.

Local variable exists till method ends.

🔴 Advanced Level Solutions (36–50)


36.

class Test{

int a=10;

static int b=20;

void show(){

int c=30;

}
}

37.

int a=10;

int b=20;

38.

int sum(int n){

int x=n;

return x;

39.

class Test{

int x;

Test(int x){

this.x=x;

40.

int a=10; // compile time

Scanner sc=new Scanner([Link]);

int b=[Link](); // runtime


41.

class A{ static int x=10; }

class B extends A{}

42.

class A{ static int x=10; }

class B extends A{ static int x=20; }

43.

class Test{

static int count=0;

Test(){ count++; }

44.

Test t1=new Test();

Test t2=new Test();

45.

static int x;

46.

final int a=10;

47.

final static int b=20;

48.

void show(){

final int a=10;


}

49.

Test t1=new Test();

Test t2=new Test();

t1.x=50;

[Link](t2.x);

50.

Scope = where variable is accessible​


Lifetime = how long variable exists

You might also like