0
0
Javaprogramming~15 mins

Static vs non-static behavior in Java - Practice Questions

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Static Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
Output of static and instance variable access
What is the output of this Java program?
Java
public class Test {
    static int staticVar = 5;
    int instanceVar = 10;

    public static void main(String[] args) {
        Test obj1 = new Test();
        Test obj2 = new Test();
        obj1.staticVar = 20;
        obj2.instanceVar = 30;
        System.out.println(obj1.staticVar + "," + obj1.instanceVar);
        System.out.println(obj2.staticVar + "," + obj2.instanceVar);
    }
}
A
5,30
20,10
B
20,10
20,30
C
20,30
20,10
D
5,10
5,30
Attempts:
2 left
🧠 conceptual
intermediate
1:30remaining
Static method access to instance variables
Which statement about static methods in Java is true?
AStatic methods cannot access any variables.
BStatic methods can access instance variables only if they are final.
CStatic methods can only access static variables directly.
DStatic methods can directly access instance variables without an object.
Attempts:
2 left
🔧 debug
advanced
2:00remaining
Identify the error in static and non-static context
What error will this code produce?
Java
public class Demo {
    int x = 10;
    static void printX() {
        System.out.println(x);
    }
    public static void main(String[] args) {
        printX();
    }
}
ACompile-time error: non-static variable x cannot be referenced from a static context
BRuntime NullPointerException
CPrints 10
DCompile-time error: missing return statement
Attempts:
2 left
💻 code output
advanced
2:30remaining
Output of static block and instance block execution order
What is the output when this Java program runs?
Java
public class Blocks {
    static {
        System.out.println("Static block");
    }
    {
        System.out.println("Instance block");
    }
    public Blocks() {
        System.out.println("Constructor");
    }
    public static void main(String[] args) {
        new Blocks();
        new Blocks();
    }
}
A
Static block
Instance block
Constructor
Instance block
Constructor
B
Instance block
Constructor
Instance block
Constructor
Static block
C
Static block
Constructor
Instance block
Constructor
Instance block
D
Constructor
Instance block
Static block
Constructor
Instance block
Attempts:
2 left
🚀 application
expert
3:00remaining
Predict the value of static and instance counters
Consider this Java class that counts instances and total calls. What will be the output after main runs?
Java
public class Counter {
    static int totalCalls = 0;
    int instanceCalls = 0;

    public Counter() {
        totalCalls++;
        instanceCalls++;
    }

    public void call() {
        totalCalls++;
        instanceCalls++;
    }

    public static void main(String[] args) {
        Counter c1 = new Counter();
        Counter c2 = new Counter();
        c1.call();
        c2.call();
        System.out.println("c1: " + c1.instanceCalls + ", c2: " + c2.instanceCalls + ", total: " + totalCalls);
    }
}
Ac1: 2, c2: 2, total: 6
Bc1: 1, c2: 1, total: 4
Cc1: 3, c2: 3, total: 8
Dc1: 2, c2: 2, total: 4
Attempts:
2 left