Challenge - 5 Problems
Static Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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); } }
Attempts:
2 left
💡 Hint
Remember that static variables belong to the class, not instances.
✗ Incorrect
Static variables are shared by all instances. Changing staticVar via obj1 changes it for obj2 too. Instance variables are separate per object.
🧠 Conceptual
intermediateStatic method access to instance variables
Which statement about static methods in Java is true?
Attempts:
2 left
💡 Hint
Think about what belongs to the class versus the object.
✗ Incorrect
Static methods belong to the class and do not have access to instance variables unless through an object reference.
🔧 Debug
advancedIdentify 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(); } }
Attempts:
2 left
💡 Hint
Static methods cannot access instance variables directly.
✗ Incorrect
The static method printX tries to access instance variable x without an object, causing a compile-time error.
❓ Predict Output
advancedOutput 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(); } }
Attempts:
2 left
💡 Hint
Static blocks run once when class loads; instance blocks run before constructor each time.
✗ Incorrect
Static block runs once first. Each new object triggers instance block then constructor.
🚀 Application
expertPredict 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); } }
Attempts:
2 left
💡 Hint
Count how many times constructors and call() increment counters.
✗ Incorrect
Each constructor increments totalCalls and instanceCalls once. Each call() increments both counters once. So totalCalls increments 2 (constructors) + 2 (calls) = 4. Each instanceCalls increments twice (constructor + call).
