Anda di halaman 1dari 5

http://javaconceptoftheday.

com/java-practice-questions-on-method-overloading-and-overriding/
1) What will be the output of the following program?
?

1
2
3
class A
4
{
5
}
6
7
class B extends A
8
{
9
10 }
11
12 class C extends B
13 {
14
15 }
16
17 public class MainClass
18 {
static void overloadedMethod(A a)
19
{
20
System.out.println("ONE");
}
21
22
static void overloadedMethod(B b)
23
{
24
System.out.println("TWO");
25
}
26
27
static void overloadedMethod(Object obj)
{
28
System.out.println("THREE");
29
}
30
31
public static void main(String[] args)
32
{
33
C c = new C();
34
overloadedMethod(c);
35
}
36
}
37
38
39
View Answer
2) In a class, one method has two overloaded forms. One form is defined as static and another form is
defined as non-static. Is that method properly overloaded?

View Answer
3) In the below class, is method overloaded or duplicated?
?

1
public class MainClass
2
{
3
void method(int ... a)
4
{
5
System.out.println(1);
}
6
7
void method(int[] a)
8
{
9
System.out.println(2);
10
}
11
}
12
View Answer
4) Method signature consists of
a) Method Name, Return Type and Number Of Arguments
b) Access Modifier, Method Name and Types Of Arguments
c) Method Name, Number Of Arguments, Types Of Arguments and Order Of Arguments
d) Return Type, Access Modifier and Order Of Arguments

View Answer
5) In the below Class X, is method properly overloaded?
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

class X
{
int method(int i, int d)
{
return i+d;
}
static int method(int i, double d)
{
return (int)(i+d);
}
double method(double i, int d)
{
return i+d;
}
static double method(double i, double d)
{
return i+d;

18
19
}
20 }
21
22
View Answer
6) What will be the output of the following program?
?

1
2
3
class X
{
4
void method(int a)
5
{
6
System.out.println("ONE");
7
}
8
9
void method(double d)
{
10
System.out.println("TWO");
11
}
12 }
13
14 class Y extends X
15 {
@Override
16
void method(double d)
17
{
18
System.out.println("THREE");
19
}
20 }
21
22 public class MainClass
23 {
public static void main(String[] args)
24
{
25
new Y().method(100);
}
26
27 }
28
29
View Answer
7) What will be the outcome of the below program?
?

1
2
3
4
5
6

public class MainClass


{
double overloadedMethod(double d)
{
return d *= d;
}

7
8
int overloadedMethod(int i)
9
{
10
return overloadedMethod(i *= i);
}
11
12
float overloadedMethod(float f)
13
{
14
return overloadedMethod(f *= f);
15
}
16
17
public static void main(String[] args)
{
18
MainClass main = new MainClass();
19
20
System.out.println(main.overloadedMethod(100));
21
}
22 }
23
24
View Answer
8) Method Overriding shows static polymorphism. True or false?

View Answer
9) In a class, One method has 4 overloaded forms. All have different access modifiers (private, default,
protected and public). Is that method properly overloaded?

View Answer
10) What will be the outcome of the following program?
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

class X
{
void calculate(int a, int b)
{
System.out.println("Class X");
}
}
class Y extends X
{
@Override
void calculate(int a, int b)
{
System.out.println("Class Y");
}
}
class Z extends Y
{
@Override
void calculate(int a, int b)

18
19
20
21
{
System.out.println("Class Z");
22
}
23
24 }
25 public class MainClass
26 {
27
public static void main(String[] args)
{
28
X x = new Y();
29
30
x.calculate(10, 20);
31
32
Y y = (Y) x;
33
34
y.calculate(50, 100);
35
36
Z z = (Z) y;
37
z.calculate(100, 200);
38
}
39
40 }
41
42
43
View Answer

Anda mungkin juga menyukai