Sie sind auf Seite 1von 24

40 Java Inheritance Practice Coding

Questions

40 Java Inheritance Practice Coding Questions :

1) Tinku has written the code like below. But, it is showing compile time error. Can you
identify what mistake he has done?

?
1
2 class X
3 {
//Class X Members
4 }
5
6 class Y
7 {
8 //Class Y Members
9 }
10
class Z extends X, Y
11{
12 //Class Z Members
13}
14
View Answer
Answer :
In Java, a class can not extend more than one class. Class Z is extending two classes – Class
X and Class Y. It is a compile time error in java.

2) What will be the output of this program?

?
1 class A
{
2 int i = 10;
3 }
4
5 class B extends A
6 {
int i = 20;
7 }
8
9 public class MainClass
10{
11 public static void main(String[] args)
12 {
A a = new B();
13
14 System.out.println(a.i);
15 }
16}
17
18
19
View Answer
Answer :
10

3) What will be the output of the below program?

?
1
2
3 class A
4 {
5 {
System.out.println(1);
6 }
7 }
8
9 class B extends A
10{
11 {
System.out.println(2);
12 }
13}
14
15class C extends B
16{
{
17 System.out.println(3);
18 }
19}
20
21public class MainClass
22{
public static void main(String[] args)
23 {
24 C c = new C();
25 }
26}
27
28
View Answer
Answer :
1
2
3

4) Can a class extend itself?

View Answer
Answer :
No. A class can not extend itself.
5) What will be the output of the following program?

?
1
2
3 class A
4 {
5 String s = "Class A";
}
6
7
class B extends A
8 {
9 String s = "Class B";
10
11 {
12 System.out.println(super.s);
}
13}
14
15class C extends B
16{
17 String s = "Class C";
18
19 {
System.out.println(super.s);
20 }
21}
22
23public class MainClass
24{
public static void main(String[] args)
25 {
26 C c = new C();
27
28 System.out.println(c.s);
29 }
30}
31
32
View Answer
Answer :
Class A
Class B
Class C

6) What will be the output of this program?

?
1 class A
2 {
static
3 {
4 System.out.println("THIRD");
5 }
6 }
7
8 class B extends A
9 {
static
10 {
11 System.out.println("SECOND");
12 }
13}
14
15class
{
C extends B
16 static
17 {
18 System.out.println("FIRST");
19 }
}
20
21public class MainClass
22{
23 public static void main(String[] args)
24 {
C c = new C();
25 }
26}
27
28
29
30
31
View Answer
Answer :
THIRD
SECOND
FIRST

7) What will be the output of the below program?

?
1 class A
2 {
public A()
3 {
4 System.out.println("Class A Constructor");
5 }
6 }
7
8 class
{
B extends A
9 public B()
10 {
11 System.out.println("Class B Constructor");
12 }
}
13
14class C extends B
15{
16 public C()
17 {
18 System.out.println("Class C Constructor");
}
19}
20
21public class MainClass
22{
23 public static void main(String[] args)
{
24 C c = new C();
25 }
26}
27
28
29
30
31
View Answer
Answer :
Class A Constructor
Class B Constructor
Class C Constructor

8) Private members of a class are inherited to sub class. True or false?

View Answer
Answer :
false. Private members are not inherited to sub class.

9) What will be the output of the following program?

?
1 class X
2 {
3 static void staticMethod()
4 {
System.out.println("Class X");
5 }
6 }
7
8 class Y extends X
9 {
static void staticMethod()
10 {
11 System.out.println("Class Y");
12 }
13}
14
15public class MainClass
{
16 public static void main(String[] args)
17 {
18 Y.staticMethod();
19 }
20}
21
22
23
View Answer
Answer :
Class Y

10) Below code is showing compile time error. Can you suggest the corrections?

?
1
2 class X
3 {
4 public X(int i)
{
5 System.out.println(1);
6 }
7 }
8
9 class Y extends X
10{
public Y()
11 {
12 System.out.println(2);
13 }
14}
15
View Answer
Answer :
Write explicit calling statement to super class constructor in Class Y constructor.
?
1
2 class X
3 {
4 public X(int i)
5 {
System.out.println(1);
6 }
7 }
8
9 class Y extends X
10{
public Y()
11 {
12 super(10); //Correction
13
14 System.out.println(2);
15 }
16}
17
40 Java Inheritance Practice Coding
Questions
pramodbablad October 25, 2015 36

11) What is wrong with the below code? Why it is showing compile time error?

1
public class A
2
{
3 public A()

4 {

5 System.out.println(1);

7 super();

8
System.out.println(2);
9
}
10
}
11

View Answer

Answer :
Constructor calling statements ( super() or this() ), if written, must be the first statements in the
constructor.Correct Code…

1 public class A

2 {

3 public A()

4 {
5 super();

7 System.out.println(1);

8
System.out.println(2);
9
}
10
}
11

12) You know that compiler will keep super() calling statement implicitly as a first
statement in every constructor. What happens if we write this() as a first statement in
our constructor?

View Answer

Answer :
Compiler will not keep super() if you are writing this() as a first statement in your constructor.

13) Can you find out the error in the below code?

1
public class A
2
{
3 public A(int i)

4 {

6 }

7 }

9 class B extends A
{
10

11
}
12

View Answer
Answer :
Class B doesn’t have any constructors written explicitly. So, compiler will keep default constructor. In
that default constructor, there will be a calling statement to super class constructor (super()). But, it
is undefined in Class A.To remove the errors, either define A() constructor in class A or write B()
constructor in class B and call super class constructor explicitly.

1
public class A
2
{
3
public A()
4
{
5
//Either keep this constructor
6
}
7

8 public A(int i)

9 {

10

11 }

12}

13

14class B extends A
{
15
public B()
16
{
17
super(10); //or else write this statement
18
}
19}

20

14) Which class is a default super class for every class in java?

View Answer
Answer :
java.lang.Object class

15) Can you find out the error in the below code?

1
public class A
2
{
3
public A()
4 {

5 super();

7 this(10);

8 }

10 public A(int i)

{
11
System.out.println(i);
12
}
13
}
14

View Answer

Answer :
A constructor can have either super() or this() but not both.

16) What will be the output of this program?

class M
1
{
2
static
3
{
4
System.out.println('A');
5 }

7 {

System.out.println('B');
8
}
9

10
public M()
11
{
12
System.out.println('C');
13
}
14}

15

16class N extends M

17{

18 static

{
19
System.out.println('D');
20
}
21

22
{
23
System.out.println('E');
24
}
25

26
public N()

27 {

28 System.out.println('F');

29 }

30}

31
32public class MainClass

33{
public static void main(String[] args)
34
{
35
N n = new N();
36
}
37
}
38

39

40

41

View Answer

Answer :
A
D
B
C
E
F

17) What will be the output of the below program?

1 class M
{
2
int i;
3

4
public M(int i)
5
{
6
this.i = i--;
7
}
8 }

9
10class N extends M

11{
public N(int i)
12
{
13
super(++i);
14

15
System.out.println(i);
16
}
17
}
18

19public class MainClass

20{

21 public static void main(String[] args)

22 {

N n = new N(26);
23
}
24
}
25

26

27

View Answer

Answer :
27

18) What will be the output of the following program?

1 class M

2 {

3 int i = 51;

4
5 public M(int j)

6 {

System.out.println(i);
7

8
this.i = j * 10;
9
}
10
}
11

12
class N extends M
13
{
14 public N(int j)

15 {

16 super(j);

17

18 System.out.println(i);

19
this.i = j * 20;
20
}
21
}
22

23
public class MainClass
24
{
25
public static void main(String[] args)
26
{

27 N n = new N(26);

28

29 System.out.println(n.i);

30 }

31}
32

33

View Answer

Answer :
51
260
520

19) Why Line 10 in the below code is showing compilation error?

1
class X
2
{
3
private int m = 48;
4 }

6 class Y extends X

7 {

8 void methodOfY()

{
9
System.out.println(m);
10
}
11
}
12

View Answer

Answer :
Because, private field ‘m’ is not visible to class Y.

20) What will be the output of the below program?

1 class X

2 {
3 int m = 1111;

5 {

m = m++;
6

7
System.out.println(m);
8
}
9
}
10

11
class Y extends X
12
{
13 {

14 System.out.println(methodOfY());

15 }

16

17 int methodOfY()

{
18
return m-- + --m;
19
}
20
}
21

22
public class MainClass
23
{
24
public static void main(String[] args)

25 {

26 Y y = new Y();

27 }

28}

29
30

View Answer

Answer :
1111
2220

21) What will be the output of this program?

class A
1
{
2
static String s = "AAA";
3

4
static
5
{
6
s = s + "BBB";
7 }

9 {

10 s = "AAABBB";

11 }

}
12

13
class B extends A
14
{
15
static
16
{
17
s = s + "BBBAAA";
18 }
19

20 {

21 System.out.println(s);

}
22
}
23

24
public class MainClass
25
{
26
public static void main(String[] args)
27
{
28 B b = new B();

29 }

30}

31

32

33

View Answer

Answer :
AAABBB

22) What will be the output of the following program?

class X
1
{
2
int i = 101010;
3

4
public X()
5
{
6
i = i++ + i-- - i;
7 }
8

9 static int staticMethod(int i)

10 {

return --i;
11
}
12
}
13

14
class Y extends X
15
{
16
public Y()
17 {

18 System.out.println(staticMethod(i));

19 }

20}

21

22public class MainClass


{
23
public static void main(String[] args)
24
{
25
Y y = new Y();
26
}
27
}
28

29

30

View Answer

Answer :
101010

23) What happens if both super class and sub class have a field with same name?

View Answer
Answer :
Super class field will be hidden in the sub class.

24) Does the below program execute successfully? If yes, what will be the output?

2 class A

3 {
void A()
4
{
5
System.out.println(1);
6
}
7
}
8

9
class B extends A
10{

11 void B()

12 {

13 A();

}
14
}
15

16
public class MainClass
17
{
18
public static void main(String[] args)
19
{
20 new B().B();

21 }

22}

23

View Answer
Answer :
Yes, but with a warning that method has a constructor name. Output will be
1

25) How do you prevent a field or a method of any class from inheriting to sub classes?

View Answer

Answer :
By declaring that particular field or method as private.

26) What will be the output of the below program?

1
class A
2 {

3 int i = 1212;

4 }

6 class B extends A
{
7
A a;
8

9
public B(A a)
10
{
11
this.a = a;
12
}
13}

14

15public class MainClass

16{

17 public static void main(String[] args)

18 {

A a = new A();
19
20

21 B b = new B(a);

22

23 System.out.println(a.i);

24
System.out.println(b.i);
25

26
System.out.println(b.a.i);
27

28
b.a.i = 2121;
29

30
System.out.println("--------");
31

32
System.out.println(a.i);
33

34
System.out.println(b.i);
35 }

36}

37

38

View Answer

Answer :
1212
1212
1212
——–
2121
1212

27) Does java support multiple inheritance?

View Answer
Answer :
Yes, but only through interfaces. Classes can implement more than one interfaces but can not
extend more than one class.

28) What will be the output of this program?

1 class ClassOne
{
2
static int i, j = 191919;
3

4
{
5
--i;
6
}
7

8
{
9 j++;

10 }

11}

12

13public class ClassTwo extends ClassOne

14{
static
15
{
16
i++;
17
}
18

19
static
20
{
21 --j;

22 }
23

24 public static void main(String[] args)

25 {

System.out.println(i);
26

27
System.out.println(j);
28
}
29
}
30

31

32

View Answer

Answer :
1
191918

29) Does the fields with default visibility inherited to sub classes outside the package?

View Answer

Answer :
No. Fields with default access modifier are inherited to sub classes within the package.

30) Constructors are also inherited to sub classes. True or false?

View Answer

Answer :
false. Constructors, SIB and IIB are not inherited to sub classes. But, they are executed while
instantiating a subclass.

Das könnte Ihnen auch gefallen