Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a static method named greet.
Java
public class Hello { public static void [1]() { System.out.println("Hello, world!"); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using
main instead of greet.Forgetting the method name or misspelling it.
✗ Incorrect
The method name should be
greet as specified. Static methods are declared with the static keyword.2fill in blank
mediumComplete the code to call the static method greet from the main method.
Java
public class Hello { public static void greet() { System.out.println("Hello, world!"); } public static void main(String[] args) { [1](); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using
this.greet() which is invalid in static context.Trying to create an object to call a static method.
✗ Incorrect
Inside the same class, static methods can be called directly by their name without class name or object.
3fill in blank
hardFix the error in calling the static method greet from another class named App.
Java
public class Hello { public static void greet() { System.out.println("Hello from Hello class!"); } } public class App { public static void main(String[] args) { [1](); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Calling
greet() without class name from another class.Trying to call
App.greet() when greet is not in App.✗ Incorrect
To call a static method from another class, use the class name followed by the method name.
4fill in blank
hardFill both blanks to create a static method square that returns the square of an integer.
Java
public class MathUtils { public static int [1](int num) { return num [2] num; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using
+ instead of * which adds instead of multiplies.Naming the method incorrectly.
✗ Incorrect
The method name should be
square. The multiplication operator * calculates the square.5fill in blank
hardFill all three blanks to create a static method isEven that returns true if a number is even, otherwise false.
Java
public class NumberUtils { public static boolean [1](int n) { return n [2] 2 [3] 0; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using
!= instead of == which reverses the logic.Incorrect method name.
✗ Incorrect
The method name is
isEven. The modulus operator % checks remainder. If remainder equals zero, the number is even.