0
0
Javaprogramming~10 mins

Compile-time polymorphism 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 define a method named add that takes two integers and returns their sum.

Java
public int add(int a, int b) {
    return a [1] b;
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
2fill in blank
medium

Complete the code to overload the add method to accept two double values.

Java
public double add(double a, double b) {
    return a [1] b;
}
Drag options to blanks, or click blank then click option'
A-
B*
C+
D/
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using operators other than addition.
3fill in blank
hard

Fix the error in the method signature to correctly overload the add method with three integer parameters.

Java
public int add(int a, int b, [1] c) {
    return a + b + c;
}
Drag options to blanks, or click blank then click option'
Aint
Bdouble
CString
Dfloat
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different data type like double or String for the third parameter.
4fill in blank
hard

Fill both blanks to create two overloaded multiply methods: one for two integers and one for two doubles.

Java
public int multiply(int a, int b) {
    return a [1] b;
}

public double multiply(double a, double b) {
    return a [2] b;
}
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using addition or division instead of multiplication.
5fill in blank
hard

Fill all three blanks to create overloaded concatenate methods: one for two strings, one for three strings, and one for an array of strings.

Java
public String concatenate(String a, String b) {
    return a [1] b;
}

public String concatenate(String a, String b, String c) {
    return a [2] b [3] c;
}
Drag options to blanks, or click blank then click option'
A+
B-
D*
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using arithmetic operators other than plus for strings.