0
0
Javaprogramming~15 mins

Static vs non-static behavior in Java - Interactive Practice

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.

Java
public class Example {
    static int [1] = 10;
}
🎯 Drag options to blanks, or click blank then click option
Afinal
Bthis
Ccount
Dvoid
Attempts:
3 left
2fill in blank
medium

Complete the code to call a static method from the class.

Java
public class Calculator {
    public static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        int result = Calculator.[1](5, 3);
        System.out.println(result);
    }
}
🎯 Drag options to blanks, or click blank then click option
Amultiply
Badd
Csubtract
Dsum
Attempts:
3 left
3fill in blank
hard

Fix the error in accessing a non-static variable from a static method.

Java
public class Counter {
    int count = 0;

    public static void main(String[] args) {
        Counter c = new Counter();
        System.out.println(c.[1]);
    }
}
🎯 Drag options to blanks, or click blank then click option
Acount
Bthis.count
Cstatic count
DCounter.count
Attempts:
3 left
4fill in blank
hard

Fill both blanks to create a static method that returns the static variable.

Java
public class Data {
    static int value = 100;

    public static [1] getValue() {
        return [2];
    }
}
🎯 Drag options to blanks, or click blank then click option
Aint
Bvalue
Cvoid
Dthis.value
Attempts:
3 left
5fill in blank
hard

Fill both blanks to create a class with a static variable, a non-static variable, and a method that prints both.

Java
public class Info {
    static String [1] = "StaticVar";
    String [2] = "InstanceVar";

    public void printInfo() {
        System.out.println([1] + " and " + [2]);
    }
}
🎯 Drag options to blanks, or click blank then click option
AstaticVar
BinstanceVar
Attempts:
3 left