Prerequisite : static and instance variables
Question 1. What is the output of this question?
class Test1 { int x = 10; public static void main(String[] args) { Test1 t1 = new Test1(); Test1 t2 = new Test1(); t1.x = 20; System.out.print(t1.x + " "); System.out.println(t2.x); } } |
Option
A) 10 10
B) 20 20
C) 10 20
D) 20 10
Output: D
Explanation : instance variable is object level variable means for every object a separate copy of instance variable will be created.
Question 2. What is the output of this question?
class Test1 { static int i = 1; public static void main(String[] args) { for (int i = 1; i < 10; i++) { i = i + 2; System.out.print(i + " "); } } } |
Option
A) 3 6 9
B) 3 6 9 …. 27
C) Error
D) none
Output: A
Explanation : Here local variables are printed after execution. If we want to execute static variables, then we write Test1.i or we write Test1 object.i.
Question 3. What is the output of this question?
class Test1 { static int i = 1; public static void main(String[] args) { int i = 1; for (Test1.i = 1; Test1.i < 10; Test1.i++) { i = i + 2; System.out.print(i + " "); } } } |
Option
A) 1 3 9
B) 1 2 3 … 9
C) 3 5 7 9 11 13 15 17 19
D) None
Output: C
Explanation : Here, two different i copies of variable are declared, one is static and other one is local. If we write Test1.i then, static variable is executed and if we write only i, then local variable are executed.
Question 4. What is the output of this question?
class Test1 { static int i = 1; public static void main(String[] args) { static int i = 1; for (Test1.i = 1; Test1.i < 10; Test1.i++) { i = i + 2; System.out.print(i + " "); } } } |
Option
A)Error
B)1 3 9
C)3 5 7 9 11 13 15 17 19
D)1 2 3 … 9
Output: A
Explanation : We can not declare the static variable inside the block. If we declare static variable inside the block, then we will get the compile time error : illegal start of expression.
Question 5. What is the output of this question?
class Test1 { public static void main(String[] args) { static int arr1[] = { 11, 22, 33 }; static int arr2[] = { 11, 22, 33, 44, 55 }; static int ptr[]; ptr = arr1; arr1 = arr2; arr2 = ptr; System.out.print(arr1.length + " "); System.out.println(arr2.length); } } |
Option
A)Error
B)5 5
C)5 3
D)3 5
Output: A
Explanation :Here we are trying to declare array as static type but we can not declare the local array as static type. If we will try to declare the local variable as static, then will get error : illegal start of expression.
This article is contributed by Shivakant Jaiswal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Output of Java Programs | Set 48 (Static keyword)
- Are static local variables allowed in Java?
- Assigning values to static final variables in Java
- Output of Java Programs | Set 12
- Output of Java programs | Set 10 (Garbage Collection)
- Output of Java programs | Set 13 (Collections)
- Output of Java Programs | Set 14 (Constructors)
- Output of Java Programs | Set 21 (Type Conversions)
- Output of Java programs | Set 24 (Final Modifier)
- Output of Java programs | Set 29
- Output of Java Programs | Set 30
- Output of Java Programs | Set 31
- Output of Java Programs | Set 32
- Output of Java Programs | Set 34 (Collections)
- Output of Java Programs | Set 33 (Collections)
- Output of Java Programs | Set 35 (Decision Making)
- Output of Java Programs | Set 36 (do-while loop)
- Output of Java Programs | Set 37 (If-else)
- Output of Java Programs | Set 38 (Arrays)
- Output of Java Programs | Set 39 (throw keyword)

