Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a method named greet that returns nothing.
Java
public [1] greet() { System.out.println("Hello!"); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a return type like int or String when the method does not return anything.
Using 'class' instead of a return type.
✗ Incorrect
The keyword 'void' is used to declare a method that does not return any value.
2fill in blank
mediumComplete the code to declare a method named add that returns an integer.
Java
public [1] add(int a, int b) { return a + b; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' when the method returns a value.
Using 'String' or 'double' as return type incorrectly.
✗ Incorrect
The method returns the sum of two integers, so the return type must be 'int'.
3fill in blank
hardFix the error in the method declaration to make it valid.
Java
public [1] calculateSum(int a, int b) { return a + b; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' as return type when the method returns a value.
Using the method name as return type.
✗ Incorrect
The method returns an integer sum, so the return type must be 'int'. Using 'void' causes a compile error because the method returns a value.
4fill in blank
hardFill both blanks to declare a method named multiply that returns the product of two doubles.
Java
public [1] multiply([2] a, [2] b) { return a * b; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' or 'float' instead of 'double'.
Using different types for return and parameters.
✗ Incorrect
The method returns a double and takes two double parameters, so both blanks must be 'double'.
5fill in blank
hardFill all three blanks to declare a method named isEven that returns true if a number is even.
Java
public [1] isEven([2] num) { return num [3] 2 == 0; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' or 'int' as return type instead of 'boolean'.
Using '/' instead of '%' for modulus.
Using 'double' instead of 'int' for the parameter.
✗ Incorrect
The method returns a boolean, takes an int parameter, and uses the modulus operator '%' to check if num is divisible by 2.
