Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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.



Last Updated : 02 Oct, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads