Challenge - 5 Problems
Static Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 conceptual
intermediate2:00remaining
Why use static methods in Java?
In Java, why do we use static methods instead of instance methods sometimes?
Attempts:
2 left
💻 code output
intermediate2:00remaining
Output of static vs instance variable access
What is the output of this Java code?
Java
class Test { static int count = 0; int id; Test() { count++; id = count; } public static void main(String[] args) { Test a = new Test(); Test b = new Test(); System.out.println(a.id + "," + b.id + "," + Test.count); } }
Attempts:
2 left
💻 code output
advanced2:00remaining
What error occurs when accessing instance variable from static method?
What error does this Java code produce?
Java
class Demo { int x = 5; static void show() { System.out.println(x); } public static void main(String[] args) { show(); } }
Attempts:
2 left
🧠 conceptual
advanced2:00remaining
Why is static block used in Java?
What is the main purpose of a static block in Java?
Attempts:
2 left
🚀 application
expert3:00remaining
How many objects are created and what is output?
Consider this Java code. How many objects are created and what is printed?
Java
class Counter { static int count = 0; Counter() { count++; } public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); Counter c3 = c2; System.out.println(count); } }
Attempts:
2 left
