0
0
Javaprogramming~15 mins

Why static is needed in Java - Test Your Understanding

Choose your learning style8 modes available
ads_clickPractice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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
Astatic
Bfinal
Cprivate
Dvoid
Attempts:
3 left
2fill in blank
medium

Complete 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
Asum
Badd
Ccalculate
Dtotal
Attempts:
3 left
3fill in blank
hard

Fix 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
Acount
Bthis.count
CCounter.count
Dstatic count
Attempts:
3 left
4fill in blank
hard

Fill 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
Astatic
B*
C+
Dvoid
Attempts:
3 left
5fill in blank
hard

Fill 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
Ablock
B"default"
Csetting
Dstatic
Attempts:
3 left