Challenge - 5 Problems
Static Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate2: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); } }
Attempts:
2 left
🧠 conceptual
intermediate1:30remaining
Static method access to instance variables
Which statement about static methods in Java is true?
Attempts:
2 left
🔧 debug
advanced2: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(); } }
Attempts:
2 left
💻 code output
advanced2: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(); } }
Attempts:
2 left
🚀 application
expert3: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); } }
Attempts:
2 left
