DEV Community

Deepikandas
Deepikandas

Posted on

#16 Known is a Drop! Tricky Coding question in Method Overloading JAVA

Cheatsheet before seeing Tricky Questions

Method Overloading Tricky Questions :
1.
class Test {
void show(int a) { }
void show(long a) { }
public static void main(String[] args) {
Test t = new Test();
t.show(10);
}
}
Answer: void show(int a) is called.


2.
class Test {
void show(Integer a) { }
void show(int a) { }
public static void main(String[] args) {
Test t = new Test();
t.show(10);
}
}
Answer: void show(int a) is called.


3.
class Test {
void show(int a, float b) { }
void show(float a, int b) { }
public static void main(String[] args) {
Test t = new Test();
t.show(10, 10);
}
}

Answer: compilation error:The method show(int, float) is ambiguous for the type Test
No float type arguments passed


4.
class Test {
void show(int... a) { }
void show(int a) { }
public static void main(String[] args) {
Test t = new Test();
t.show(10);
}
}
Answer: void show(int a) is called.


5.
class Test {
void show(int a) { }
void show(int... a) { }
public static void main(String[] args) {
Test t = new Test();
t.show();
}
}
*Answer: * void show(int... a) is called as varargs parameter methods accepts from 0 to many arguments


6.
class Test {
void show(double a) { }
void show(float a) { }
public static void main(String[] args) {
Test t = new Test();
t.show(10);
}
}
Answer:void show(double a)


7.
class Test {
void show(Object o) { }
void show(String s) { }
public static void main(String[] args) {
Test t = new Test();
t.show(null);
}
}
Answer: void show(String s) is called


8.
class Test {
void show(String s) { }
void show(Integer i) { }
public static void main(String[] args) {
Test t = new Test();
t.show(null);
}
}
Answer: compile error -The method show(String) is ambiguous for the type Test


9.
class Test {
void show(int a, int b) { }
void show(int... a) { }
public static void main(String[] args) {
Test t = new Test();
t.show(10,20);
}
}
Answer: show(int a, int b) called.


10.
class Test {
void show(int a) { }
static void show(int a) { }
public static void main(String[] args) {
Test t = new Test();
t.show(10);
}
}

Answer: Compilation error -duplicate methods with the same signature: show(int a).
Not return type/modifiers difference is considered


11.
class Test {
void show(long a) { }
void show(float a) { }
public static void main(String[] args) {
new Test().show(10);
}
}
Answer:void show(long a) called


12.
class Test {
void show(int a) { }
void show(int a, int... b) { }
public static void main(String[] args) {
new Test().show(10);
}
}
Answer:void show(int a) is called


13.
class Test {
void show(char c) { }
void show(int i) { }
public static void main(String[] args) {
new Test().show('A');
}
}
Answer:void show(char c) called


14.
class Test {
void show(byte b) { }
void show(short s) { }
public static void main(String[] args) {
byte b = 10;
new Test().show(b);
}
}
Answer:void show(byte b)


15.
class Test {
void show(Object o) { }
void show(String s) { }
void show(CharSequence c) { }
public static void main(String[] args) {
new Test().show("Java");
}
}

Answer:void show(String s)is called

Top comments (0)