Bird
Raised Fist0
Javaprogramming~20 mins

Static variables in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Static Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of static variable increment
What is the output of the following Java code?
Java
public class Test {
    static int count = 0;
    public Test() {
        count++;
    }
    public static void main(String[] args) {
        Test t1 = new Test();
        Test t2 = new Test();
        Test t3 = new Test();
        System.out.println(Test.count);
    }
}
ACompilation error
B0
C1
D3
Attempts:
2 left
💡 Hint
Static variables are shared among all instances of a class.
❓ Predict Output
intermediate
2:00remaining
Static variable behavior with multiple classes
What will be printed when running this Java code?
Java
class A {
    static int x = 5;
}
class B extends A {
    static int x = 10;
}
public class Main {
    public static void main(String[] args) {
        System.out.println(A.x);
        System.out.println(B.x);
    }
}
A10\n10
B5\n5
C5\n10
DCompilation error
Attempts:
2 left
💡 Hint
Static variables are not overridden but hidden in subclasses.
❓ Predict Output
advanced
2:00remaining
Static variable initialization order
What is the output of this Java code?
Java
public class Counter {
    static int count;
    static {
        count = count + 1;
    }
    public static void main(String[] args) {
        System.out.println(count);
    }
}
A1
BCompilation error: variable count might not have been initialized
CRuntime error: NullPointerException
D0
Attempts:
2 left
💡 Hint
Static variables of type int are initialized to 0 by default before static blocks execute.
❓ Predict Output
advanced
2:00remaining
Static variable shared across threads
What is the output of this Java program?
Java
public class SharedCounter implements Runnable {
    static int counter = 0;
    public void run() {
        for(int i = 0; i < 1000; i++) {
            counter++;
        }
    }
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new SharedCounter());
        Thread t2 = new Thread(new SharedCounter());
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter);
    }
}
A2000
BLess than 2000
C1000
DCompilation error
Attempts:
2 left
💡 Hint
Incrementing a shared static variable without synchronization can cause race conditions.
🧠 Conceptual
expert
2:00remaining
Static variable memory allocation
Which statement about static variables in Java is correct?
AStatic variables are stored in the method area and shared among all instances.
BStatic variables are stored in the stack and unique to each thread.
CStatic variables are stored in the heap and shared among all instances of the class.
DStatic variables are stored inside each object instance separately.
Attempts:
2 left
💡 Hint
Think about where class-level data is stored in Java memory model.

Practice

(1/5)
1. What is a key characteristic of a static variable in Java?
easy
A. It belongs to the class and is shared by all instances.
B. It is unique to each object created from the class.
C. It can only be accessed inside instance methods.
D. It is automatically deleted when an object is destroyed.

Solution

  1. Step 1: Understand static variable ownership

    A static variable belongs to the class itself, not to any individual object.
  2. Step 2: Recognize sharing behavior

    Because it belongs to the class, all objects share the same static variable value.
  3. Final Answer:

    It belongs to the class and is shared by all instances. -> Option A
  4. Quick Check:

    Static variable = shared by all objects [OK]
Hint: Static means shared by class, not per object [OK]
Common Mistakes:
  • Thinking static variables are unique per object
  • Confusing static with instance variables
  • Assuming static variables are deleted with objects
2. Which of the following is the correct way to declare a static variable in Java?
easy
A. static int count;
B. int static count;
C. int count static;
D. count static int;

Solution

  1. Step 1: Recall Java syntax for static variables

    The keyword static comes before the type when declaring a static variable.
  2. Step 2: Check each option

    Only static int count; follows the correct syntax.
  3. Final Answer:

    static int count; -> Option A
  4. Quick Check:

    static keyword before type = correct syntax [OK]
Hint: Put 'static' before the type when declaring [OK]
Common Mistakes:
  • Placing static after the variable name
  • Mixing order of keywords
  • Using invalid syntax order
3. What will be the output of the following Java code?
class Test {
    static int count = 0;
    Test() {
        count++;
    }
    public static void main(String[] args) {
        new Test();
        new Test();
        System.out.println(Test.count);
    }
}
medium
A. Compilation error
B. 1
C. 0
D. 2

Solution

  1. Step 1: Understand static variable incrementation

    Each time a new Test object is created, the constructor increments count by 1.
  2. Step 2: Count the number of objects created

    Two objects are created, so count becomes 2.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Two objects increment count twice = 2 [OK]
Hint: Static count increments with each new object [OK]
Common Mistakes:
  • Assuming count resets for each object
  • Confusing instance and static variables
  • Expecting compilation error due to static usage
4. Identify the error in the following code snippet:
class Counter {
    static int count;
    void increment() {
        count++;
    }
    public static void main(String[] args) {
        Counter c = new Counter();
        c.increment();
        System.out.println(count);
    }
}
medium
A. Cannot access static variable from instance method.
B. No error; code runs and prints 1.
C. Cannot access static variable directly in static method without class name.
D. Static variable count is not initialized.

Solution

  1. Step 1: Check static variable initialization

    Static int variables default to 0 if not explicitly initialized.
  2. Step 2: Verify method calls and access

    The instance method increment() correctly increments the static variable. The static method main accesses count directly, which is allowed.
  3. Final Answer:

    No error; code runs and prints 1. -> Option B
  4. Quick Check:

    Static variable accessible and increments correctly [OK]
Hint: Static vars default to 0; accessible in static main [OK]
Common Mistakes:
  • Thinking static vars must be explicitly initialized
  • Assuming static vars can't be accessed without class name inside same class
  • Confusing instance and static method access rules
5. Consider this Java class:
class Employee {
    static int employeeCount = 0;
    String name;
    Employee(String name) {
        this.name = name;
        employeeCount++;
    }
    static void printCount() {
        System.out.println("Total employees: " + employeeCount);
    }
}

public class Main {
    public static void main(String[] args) {
        Employee e1 = new Employee("Alice");
        Employee e2 = new Employee("Bob");
        e1.printCount();
        Employee.printCount();
    }
}

What will be the output when running Main?
hard
A. Total employees: 1 Total employees: 2
B. Total employees: 2 Total employees: 1
C. Total employees: 2 Total employees: 2
D. Compilation error due to calling static method on instance

Solution

  1. Step 1: Count employees created

    Two Employee objects are created, so employeeCount becomes 2.
  2. Step 2: Understand static method calls

    Calling printCount() on instance e1 or class Employee both print the same static variable value.
  3. Final Answer:

    Total employees: 2 Total employees: 2 -> Option C
  4. Quick Check:

    Static method prints shared count = 2 [OK]
Hint: Static method prints shared count; instance call allowed [OK]
Common Mistakes:
  • Thinking static method can't be called on instance
  • Assuming employeeCount resets per object
  • Confusing output order or values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard