Bird
Raised Fist0
Javaprogramming~20 mins

Static vs non-static behavior in Java - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Static Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict 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
💡 Hint
Remember that static variables belong to the class, not instances.
🧠 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
💡 Hint
Think about what belongs to the class versus the object.
🔧 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
💡 Hint
Static methods cannot access instance variables directly.
❓ Predict 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
💡 Hint
Static blocks run once when class loads; instance blocks run before constructor each time.
🚀 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
💡 Hint
Count how many times constructors and call() increment counters.

Practice

(1/5)
1. Which statement correctly describes a static method in Java?
easy
A. It belongs to the class and can be called without creating an object.
B. It belongs to an object and requires an instance to be called.
C. It can only access non-static variables directly.
D. It cannot be called from the main method.

Solution

  1. Step 1: Understand static method ownership

    A static method belongs to the class itself, not to any object instance.
  2. Step 2: Check how static methods are called

    Static methods can be called directly using the class name without creating an object.
  3. Final Answer:

    It belongs to the class and can be called without creating an object. -> Option A
  4. Quick Check:

    Static method = class-level method [OK]
Hint: Static methods belong to class, call without object [OK]
Common Mistakes:
  • Thinking static methods need an object to call
  • Confusing static with instance methods
  • Assuming static methods access instance variables directly
2. Which of the following is the correct way to declare a static variable in Java?
easy
A. int count static = 0;
B. int static count = 0;
C. count static int = 0;
D. static int count = 0;

Solution

  1. Step 1: Recall Java syntax for static variables

    The keyword static comes before the data type in variable declaration.
  2. Step 2: Match the correct syntax

    static int count = 0; uses static int count = 0; which is the correct order.
  3. Final Answer:

    static int count = 0; -> Option D
  4. Quick Check:

    static before type = correct syntax [OK]
Hint: Put static before the type when declaring variables [OK]
Common Mistakes:
  • Placing static after the variable name
  • Using invalid order of keywords
  • Confusing static with final keyword
3. What will be the output of the following Java code?
class Test {
    static int count = 0;
    int id;

    Test() {
        count++;
        id = count;
    }

    void display() {
        System.out.println("ID: " + id + ", Count: " + count);
    }
}

public class Main {
    public static void main(String[] args) {
        Test a = new Test();
        Test b = new Test();
        a.display();
        b.display();
    }
}
medium
A. ID: 0, Count: 2 ID: 0, Count: 2
B. ID: 1, Count: 1 ID: 2, Count: 2
C. ID: 1, Count: 2 ID: 2, Count: 2
D. ID: 1, Count: 0 ID: 2, Count: 0

Solution

  1. Step 1: Understand static and instance variables in constructor

    Each time a Test object is created, count (static) increments and id (instance) is set to current count.
  2. Step 2: Trace object creation and display calls

    First object: count=1, id=1; Second object: count=2, id=2. Both display show current count=2.
  3. Final Answer:

    ID: 1, Count: 2 ID: 2, Count: 2 -> Option C
  4. Quick Check:

    Static count shared, id unique per object [OK]
Hint: Static variables shared, instance variables unique per object [OK]
Common Mistakes:
  • Assuming static variable resets per object
  • Confusing instance variable values
  • Ignoring static variable's shared nature
4. Identify the error in the following Java code snippet:
public class Example {
    static int number;

    public void setNumber(int number) {
        this.number = number;
    }

    public static void main(String[] args) {
        Example ex = new Example();
        ex.setNumber(5);
        System.out.println(Example.number);
    }
}
medium
A. Cannot use 'this' to access static variable inside non-static method.
B. Static variable 'number' cannot be accessed in main method.
C. 'this' cannot be used in static context like main method.
D. No error; code runs and prints 5.

Solution

  1. Step 1: Analyze use of 'this' keyword in setNumber method

    'this' refers to the current object instance and cannot be used to access static variables.
  2. Step 2: Understand static variable access rules

    Static variables belong to the class, so they should be accessed using the class name or directly without 'this'.
  3. Final Answer:

    Cannot use 'this' to access static variable inside non-static method. -> Option A
  4. Quick Check:

    'this' cannot access static variables [OK]
Hint: Use class name, not 'this', to access static variables [OK]
Common Mistakes:
  • Thinking 'this' can access static variables
  • Confusing static and instance variable access
  • Assuming no error in code
5. You want to count how many objects of a class Book have been created. Which approach correctly uses static and non-static members to achieve this?
class Book {
    // Fill in the blanks
}
hard
A. Use a non-static int counter and static title; increment counter in main method.
B. Use a static int counter incremented in the constructor; each object has a non-static title.
C. Use a static int counter incremented in a static method; title is static too.
D. Use a non-static int counter incremented in the constructor; title is static.

Solution

  1. Step 1: Identify what should be shared and what is unique

    The count of objects should be shared by all objects, so it must be static. The title is unique per book, so non-static.
  2. Step 2: Implement counting in constructor

    Increment the static counter inside the constructor to count each new object created.
  3. Final Answer:

    Use a static int counter incremented in the constructor; each object has a non-static title. -> Option B
  4. Quick Check:

    Static counter for all objects, non-static title per object [OK]
Hint: Count objects with static variable incremented in constructor [OK]
Common Mistakes:
  • Making counter non-static so it resets per object
  • Making title static so all objects share same title
  • Incrementing counter outside constructor
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