0
0
Javaprogramming~10 mins

Default methods in Java - Interactive Code Practice

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

Complete the code to declare a default method in an interface.

Java
public interface MyInterface {
    default void [1]() {
        System.out.println("Default method");
    }
}
Drag options to blanks, or click blank then click option'
Avoid
Bdefault
CdefaultMethod
Dmethod
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'default' as the method name causes a syntax error.
Omitting the method name entirely.
2fill in blank
medium

Complete the code to call the default method from an interface inside a class.

Java
public class MyClass implements MyInterface {
    public void callDefault() {
        [1].super.defaultMethod();
    }
}
Drag options to blanks, or click blank then click option'
Ainterface
BMyInterface
Cthis
Dsuper
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'super' alone causes a compile error.
Using 'this' does not call the interface default method.
3fill in blank
hard

Fix the error in the code to correctly override a default method in a class.

Java
public class MyClass implements MyInterface {
    @Override
    public void [1]() {
        System.out.println("Overridden method");
    }
}
Drag options to blanks, or click blank then click option'
Adefaultmethod
Bdefault_method
CDefaultMethod
DdefaultMethod
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Changing the case of the method name causes it not to override.
Adding underscores or other characters changes the method signature.
4fill in blank
hard

Fill both blanks to define a default method with a return type and a return statement.

Java
public interface Calculator {
    default int [1](int a, int b) {
        return a [2] b;
    }
}
Drag options to blanks, or click blank then click option'
Aadd
B+
C-
D*
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using subtraction or multiplication operator instead of addition.
Using a method name that does not match the operation.
5fill in blank
hard

Fill all three blanks to implement a default method that checks if a number is even.

Java
public interface NumberCheck {
    default boolean [1](int num) {
        return num [2] 2 [3] 0;
    }
}
Drag options to blanks, or click blank then click option'
AisEven
B%
C==
D!=
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '!=' instead of '==' causes incorrect logic.
Using wrong method names or operators.