Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is compile-time polymorphism in Java?
Compile-time polymorphism is when the method to be called is decided during the compilation of the program. It is also called method overloading.
Click to reveal answer
beginner
How does method overloading demonstrate compile-time polymorphism?
Method overloading allows multiple methods with the same name but different parameters. The compiler decides which method to call based on the arguments used.
Click to reveal answer
intermediate
Can compile-time polymorphism be achieved using method overriding?
No, method overriding is runtime polymorphism because the method to call is decided during program execution, not at compile time.
Click to reveal answer
intermediate
What are the requirements for method overloading in Java?
Methods must have the same name but differ in the number, type, or order of parameters. Return type alone is not enough to overload a method.
Click to reveal answer
beginner
Example of compile-time polymorphism using method overloading:
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
The compiler chooses which add method to call based on argument types.
Click to reveal answer
What decides which overloaded method to call in Java?
AThe programmer manually
BThe JVM at runtime
CThe operating system
DThe compiler based on method parameters
✗ Incorrect
Compile-time polymorphism means the compiler decides which method to call based on the method's parameters.
Which of the following is NOT a valid way to overload a method?
AChanging only the return type
BChanging the number of parameters
CChanging the order of parameters
DChanging the type of parameters
✗ Incorrect
Changing only the return type does not overload a method because the compiler cannot distinguish methods by return type alone.
Compile-time polymorphism is also known as:
AMethod overloading
BMethod overriding
CDynamic binding
DInheritance
✗ Incorrect
Method overloading is compile-time polymorphism because the method call is resolved during compilation.
Which polymorphism type is determined at runtime?
AMethod overloading
BRuntime polymorphism
CCompile-time polymorphism
DStatic polymorphism
✗ Incorrect
Runtime polymorphism is when the method to call is decided during program execution, such as with method overriding.
In Java, which keyword is NOT related to compile-time polymorphism?
Astatic
Bfinal
C@Override
Dpublic
✗ Incorrect
The '@Override' annotation relates to method overriding (runtime polymorphism), not compile-time polymorphism.
Explain compile-time polymorphism and how Java achieves it.
Think about how Java chooses which method to run before the program starts.
You got /3 concepts.
Describe the difference between compile-time and runtime polymorphism with examples.
One is decided by the compiler, the other by the program while running.
You got /3 concepts.
Practice
(1/5)
1.
What is compile-time polymorphism in Java?
easy
A. Using different method names for different tasks
B. Changing the method behavior at runtime based on object type
C. Using the same method name with different parameters in the same class
D. Creating multiple classes with the same name
Solution
Step 1: Understand method overloading
Compile-time polymorphism is also called method overloading, where methods share the same name but differ in parameters.
Step 2: Differentiate from runtime polymorphism
Runtime polymorphism uses method overriding, changing behavior based on object type at runtime, not compile-time.
Final Answer:
Using the same method name with different parameters in the same class -> Option C
Hint: Same method name, different parameters means compile-time polymorphism [OK]
Common Mistakes:
Confusing compile-time with runtime polymorphism
Thinking method overriding is compile-time polymorphism
Believing different method names are polymorphism
2.
Which of the following is the correct syntax for method overloading in Java?
public class Calculator {
public int add(int a, int b) { return a + b; }
public int add(int a, int b, int c) { ? }
}
easy
A. return a + b + c;
B. return a + b;
C. return a * b * c;
D. return a - b - c;
Solution
Step 1: Check method parameters
The second add method has three parameters, so it should add all three values.
Step 2: Write correct return statement
Return the sum of a, b, and c to correctly overload the add method.
Final Answer:
return a + b + c; -> Option A
Quick Check:
Overloaded method sums all parameters [OK]
Hint: Overloaded methods must handle all their parameters correctly [OK]
Common Mistakes:
Returning sum of only two parameters in three-parameter method
Using wrong operators like multiplication or subtraction
Syntax errors like missing semicolon
3.
What will be the output of the following Java code?
class Demo {
void show(int a) { System.out.println("Int: " + a); }
void show(String a) { System.out.println("String: " + a); }
}
public class Test {
public static void main(String[] args) {
Demo d = new Demo();
d.show(5);
d.show("Hello");
}
}
medium
A. Int: 5\nString: Hello
B. String: 5\nInt: Hello
C. Int: 5\nInt: Hello
D. Compilation error
Solution
Step 1: Identify overloaded methods
There are two show methods: one takes int, the other takes String.
Method overloading calls correct method by parameter type [OK]
Hint: Method chosen by parameter type at compile time [OK]
Common Mistakes:
Confusing parameter types and outputs
Expecting runtime polymorphism behavior
Thinking it causes compilation error
4.
Find the error in this code snippet related to compile-time polymorphism:
class Test {
void display(int a) { System.out.println(a); }
void display(int a, int b) { System.out.println(a + b); }
void display(int a) { System.out.println(a * 2); }
}
medium
A. Incorrect method parameter types
B. Duplicate method display(int a) causes compilation error
C. Missing return type in one method
D. No error, code compiles fine
Solution
Step 1: Check method signatures
Two methods have the exact same name and parameter list: display(int a).
Step 2: Understand method overloading rules
Method overloading requires different parameter lists; duplicate signatures cause compilation error.
Final Answer:
Duplicate method display(int a) causes compilation error -> Option B
Quick Check:
Duplicate method signatures cause compile error [OK]
Hint: Overloaded methods must differ in parameter list [OK]
Common Mistakes:
Thinking method bodies affect overloading
Ignoring duplicate parameter lists
Assuming code compiles without error
5.
Consider this class:
class Printer {
void print(int a) { System.out.println("Number: " + a); }
void print(String a) { System.out.println("Text: " + a); }
void print(int a, String b) { System.out.println(a + " and " + b); }
}
Which call will cause a compile-time error?
hard
A. print("Test")
B. print(10, "Hello")
C. print(5)
D. print("Hello", 10)
Solution
Step 1: Check method signatures
Methods accept (int), (String), and (int, String) parameters.
Step 2: Match call parameters
print("Hello", 10) tries to call (String, int), which does not exist, causing compile error.
Final Answer:
print("Hello", 10) -> Option D
Quick Check:
No matching method for (String, int) call [OK]
Hint: Check parameter order and types carefully for overloaded methods [OK]