Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a static variable in Java.
Java
public class Example { public [1] int count = 0; }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
2fill in blank
mediumComplete the code to call a static method from the class without creating an object.
Java
public class Calculator { public static int add(int a, int b) { return a + b; } public static void main(String[] args) { int sum = Calculator.[1](5, 3); System.out.println(sum); } }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
3fill in blank
hardFix the error in accessing a non-static variable from a static method by completing the code.
Java
public class Counter { static int count = 0; public static void increment() { [1]++; } }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
4fill in blank
hardFill both blanks to create a static method that returns the square of a number.
Java
public class MathUtil { public [1] int square(int num) { return num [2] num; } }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
5fill in blank
hardFill all three blanks to create a static block that initializes a static variable.
Java
public class Config { public static String setting; static [1] { setting = [2]; } public static void main(String[] args) { System.out.println([3]); } }
🎯 Drag options to blanks, or click blank then click option
Attempts:
3 left
