Output of Java Programs | Set 45 (static and instance variables)

Prerequisite : static and instance variables

Question 1. What is the output of this question?

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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 + " ");
        }
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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 + " ");
        }
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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 + " ");
        }
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


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.



My Personal Notes arrow_drop_up


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.