0
0
Javaprogramming~15 mins

Why methods are 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 define a method that prints a greeting.

Java
public class HelloWorld {
    public static void [1]() {
        System.out.println("Hello, world!");
    }

    public static void main(String[] args) {
        greet();
    }
}
🎯 Drag options to blanks, or click blank then click option
Agreet
Bmain
Cprint
Dhello
Attempts:
3 left
2fill in blank
medium

Complete the code to call the method that calculates the sum.

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

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

Fix the error by completing the method call correctly.

Java
public class Printer {
    public static void printMessage(String message) {
        System.out.println(message);
    }

    public static void main(String[] args) {
        [1]("Welcome!");
    }
}
🎯 Drag options to blanks, or click blank then click option
Aprint
Bprintln
CprintMessage
Dmessage
Attempts:
3 left
4fill in blank
hard

Fill both blanks to create a method that returns the square of a number and call it.

Java
public class MathUtils {
    public static int [1](int num) {
        return num [2] num;
    }

    public static void main(String[] args) {
        int sq = square(4);
        System.out.println(sq);
    }
}
🎯 Drag options to blanks, or click blank then click option
Asquare
B+
C*
Dmultiply
Attempts:
3 left
5fill in blank
hard

Fill all three blanks to create a method that checks if a number is even and call it.

Java
public class NumberCheck {
    public static boolean [1](int n) {
        return n [2] 2 [3] 0;
    }

    public static void main(String[] args) {
        boolean result = isEven(10);
        System.out.println(result);
    }
}
🎯 Drag options to blanks, or click blank then click option
AisEven
B%
C==
D!=
Attempts:
3 left