You are on page 1of 21

1) Below class ABC doesn’t have even a single abstract method, but it has

been declared as abstract. Is it correct?

1 abstract class ABC


2 {
3 void firstMethod()
4 {
5 System.out.println("First Method");
6 }
7
8 void secondMethod()
9 {
10 System.out.println("Second Method");
11 }
12 }

2) Why the below class is showing compilation error?

1 abstract class AbstractClass


2 {
3 abstract void abstractMethod()
4 {
5 System.out.println("First Method");
6 }
7 }

3) Which class is instantiable? Class A or Class B?

1 abstract class A
2 {
3
4 }
5
6 class B extends A
7 {
8
9 }

4) Below code snippet is showing compilation error? Can you suggest the
corrections?
1 abstract class A
2 {
3 abstract int add(int a, int b);
4 }
5
6 class B extends A
7 {
8
9 }

5) Is the following program written correctly? If yes, what value “result”


variable will hold if you run the program?

1 abstract class Calculate


2 {
3 abstract int add(int a, int b);
4 }
5
6 public class MainClass
7 {
8 public static void main(String[] args)
9 {
10 int result = new Calculate()
11 {
12 @Override
13 int add(int a, int b)
14 {
15 return a+b;
16 }
17 }.add(11010, 022011);
18 }
19 }

6) Can we write explicit constructors in an abstract class?

7) Can you identify the error in the below code?

1 abstract class AbstractClass


2 {
3 private abstract int abstractMethod();
4 }
8) Can we declare protected methods in an interface?

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

abstract class A
1
{
2
abstract void firstMethod();
3
4
5 void secondMethod()
6 {
System.out.println("SECOND");
7
8
9 firstMethod();
10 }
11 }
12
abstract class B extends A
13
{
14
@Override
15
void firstMethod()
16
{
17
System.out.println("FIRST");
18
19
thirdMethod();
20
}
21
22
23 abstract void thirdMethod();
24 }
25
26 class C extends B
{
27
@Override
28
void thirdMethod()
29
{
30
System.out.println("THIRD");
31
}
32
}
33
34
public class MainClass
35 {
36 public static void main(String[] args)
37 {
38 C c = new C();
39
40 c.firstMethod();
41
42 c.secondMethod();
43
44 c.thirdMethod();
45 }
46 }
47

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

1 abstract class X
2 {
3 public X()
4 {
5 System.out.println("ONE");
6 }
7
8 abstract void abstractMethod();
9 }
10
11 class Y extends X
12 {
13 public Y()
14 {
15 System.out.println("TWO");
16 }
17
18 @Override
19 void abstractMethod()
20 {
21 System.out.println("THREE");
22 }
23 }
24
25 public class MainClass
26 {
27 public static void main(String[] args)
28 {
29 X x = new Y();
30
31 x.abstractMethod();
32 }
33 }

11) Can we declare abstract methods as static?

12) Is the below program written correctly? If yes, what will be the output?

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

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

abstract class A
1
{
2 abstract int firstMethod(int i);
3
4 abstract int secondMethod(int i);
5
6 int thirdMethod(int i)
7 {
8 return secondMethod(++i);
9 }
10 }
11
12 abstract class B extends A
13 {
14 @Override
15 int secondMethod(int i)
16 {
17 return firstMethod(++i);
18 }
19 }
20
21 class C extends B
22 {
23 @Override
24 int firstMethod(int i)
25 {
26 return ++i;
27 }
28 }
29
30 public class MainClass
31 {
32 public static void main(String[] args)
33 {
34 C c = new C();
35
36 System.out.println(c.thirdMethod(121121));
37 }
38 }
39

14) Can we keep static initialization blocks inside an abstract class?


15) Is the below program written correctly? If yes, what will be the output?

1 abstract class XYZ


2 {
3 {
4 System.out.println(1);
5 }
6
7 public XYZ()
8 {
9 System.out.println(2);
10
11 abstractMethod();
12 }
13
14 abstract void abstractMethod();
15 }
16
17 class PQR extends XYZ
18 {
19 {
20 System.out.println(3);
21 }
22
23 public PQR()
24 {
25 System.out.println(4);
26 }
27
28 @Override
29 void abstractMethod()
30 {
31 System.out.println(5);
32 }
33 }
34
35 public class MainClass
36 {
37 public static void main(String[] args)
38 {
39 PQR pqr = new PQR();
40 }
41 }
16) Can you identify the error in the below code?

1 class X
2 {
3 public X()
4 {
5 System.out.println("Constructor One");
6 }
7
8 abstract X(int i)
9 {
10 System.out.println("Constructor Two");
11 }
12 }

17) Abstract methods can be declared as final. True or False?

18) Is the below code written correctly?

1 class X
2 {
3 abstract class Y
4 {
5 class Z
6 {
7
8 }
9 }
10 }

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

1 class ClassOne
2 {
3 int methodOne(int i, int j)
4 {
5 return i++ + ++j - ++i - j++;
6 }
7 }
8
9 abstract class ClassTwo extends ClassOne
10 {
11 abstract int methodOne(int i, int j, int k);
12
13 @Override
14 int methodOne(int i, int j)
15 {
16 return methodOne(i, j, i+j);
17 }
18 }
19
20 class ClassThree extends ClassTwo
21 {
22 @Override
23 int methodOne(int i, int j, int k)
24 {
25 return --i - j-- + ++k - i++ + ++j - k--;
26 }
27 }
28
29 public class MainClass
30 {
31 public static void main(String[] args)
32 {
33 ClassOne one = new ClassOne();
34
35 ClassThree three = new ClassThree();
36
37 System.out.println(three.methodOne(one.methodOne(10101, 20202),
38 one.methodOne(20202, 10101)));
39 }
}

20) Is the below code written correctly?

1 class A
2 {
3 void methodOfA()
4 {
5 abstract class B
6 {
7
8 }
9 }
10 }

21) Can we declare abstract method with throws clause?

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

1 abstract class A
2 {
3 int i = 111, j = 222;
4
5 abstract void methodOne();
6
7 abstract void methodTwo();
8 }
9
10 abstract class B extends A
11 {
12 @Override
13 void methodOne()
14 {
15 System.out.println(i);
16
17 System.out.println(j);
18
19 i = ++i;
20
21 j = --j;
22 }
23 }
24
25 class C extends B
26 {
27 @Override
28 void methodTwo()
29 {
30 System.out.println(i);
31
32 System.out.println(j);
33
34 i = i++;
35
36 j = j--;
37 }
38 }
39
40 public class MainClass
41 {
42 public static void main(String[] args)
43 {
44 C c = new C();
45
46 c.methodOne();
47
48 c.methodTwo();
49
50 System.out.println(c.i);
51
52 System.out.println(c.j);
53 }
54 }

23) Which of the following shows 100% abstractness?


a) Abstract Classes b) Interfaces

24) Can you identify the error in the below code?

1 abstract class A
2 {
3 synchronized abstract void method();
4 }

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

1 abstract class X
2 {
3 int i = 111;
4
5 int methodX()
6 {
7 return methodX(i);
8 }
9
10 abstract int methodX(int i);
11 }
12
13 class Y extends X
14 {
15 @Override
16 int methodX(int i)
17 {
18 return ++i + i++;
19 }
20 }
21
22 public class MainClass
23 {
24 public static void main(String[] args)
25 {
26 Y y = new Y();
27
28 System.out.println(y.methodX());
29 }
30 }

26) Is the below code written correctly?

1 class A
2 {
3 abstract class B
4 {
5 class C
6 {
7 abstract class D
8 {
9 abstract void method();
10 }
11 }
12 }
13 }
27) Write a code which implements abstract method “methodY()” of class
Y in the below code?

1 class X
2 {
3 abstract static class Y
4 {
5 abstract void methodY();
6 }
7 }

28) Can we instantiate a class which has only concrete methods but
declared as abstract?

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

abstract class ABC


1
{
2
abstract void methodOne();
3
}
4
5
abstract class XYZ extends ABC
6
{
7
int i;
8
9
@Override
10
void methodOne()
11
{
12
methodOne(i *= i);
13
}
14
15
abstract void methodOne(int i);
16
}
17
18
class PQR extends XYZ
19
{
20
public PQR(int i)
21 {
22 this.i = i;
23 }
24
25
@Override
26 void methodOne(int i)
27 {
28 System.out.println(i++ * ++i);
29 }
30 }
31
32 public class MainClass
33 {
34 public static void main(String[] args)
35 {
36 PQR pqr = new PQR(1);
37
38 pqr.methodOne();
39 }
40 }
41

30) Is the following program written correctly? If yes, what will be the
output?

1 abstract class A
2 {
3 {
4 methodA();
5 }
6
7 abstract void methodA();
8 }
9
10 class B extends A
11 {
12 @Override
13 void methodA()
14 {
15 System.out.println("methodA");
16 }
17 }
18
19 public class MainClass
20 {
21 public static void main(String[] args)
22 {
23 new B();
24 }
25 }

31) Can you find out the error in the following code?

1 class A
2 {
3 class B
4 {
5 static void methodB()
6 {
7 System.out.println("Method B");
8 }
9 }
10 }

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

1 class X
2 {
3 static int x = 3131;
4
5 static class Y
6 {
7 static int y = x++;
8
9 static class Z
10 {
11 static int z = y++;
12 }
13 }
14 }
15
16 public class MainClass
17 {
18 public static void main(String[] args)
19 {
20 System.out.println(X.x);
21
22 System.out.println(X.Y.y);
23
24 System.out.println(X.Y.Z.z);
25 }
26 }

33) Static nested classes can have only static members in them. True OR
False?

34) How do you access field ‘i’ of class ‘XYZ’ in the below example?

1 class ABC
2 {
3 class XYZ
4 {
5 int i = 111;
6 }
7 }

35) Does below program print “SUCCESS” on the console when you run
it?

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

36) Is the below code written correctly?

1 class A
2 {
3 String s = "AAA";
4
5 void methodA()
6 {
7 System.out.println(s);
8 }
9
10 static class B
11 {
12 void methodB()
13 {
14 methodA();
15 }
16 }
17 }

37) Is the below program written correctly? If yes, what will be the output?

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

38) Which of the following is true about anonymous inner classes?

a) You can create ‘n’ number of objects to anonymous inner classes.


b) Anonymous inner classes will not have the name.
c) You can instantiate anonymous inner classes only once.
d) b and c

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

1 class A
2 {
3 static String s = "AAA";
4
5 class B
6 {
7 String s = "BBB";
8
9 void methodB()
10 {
11 System.out.println(s);
12 }
13 }
14 }
15
16 public class MainClass
17 {
18 public static void main(String[] args)
19 {
20 A a = new A();
21
22 System.out.println(a.s);
23
24 A.B b = a.new B();
25
26 System.out.println(b.s);
27
28 b.methodB();
29 }
30 }

40) Can you find out the error in the following code?

1 class A
2 {
3 void methodOne()
4 {
5 class B
6 {
7 void methodTwo()
8 {
9 System.out.println("Method Two");
10 }
11 }
12 }
13
14 void methodThree()
15 {
16 new B().methodTwo();
17 }
18 }

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

1 class ABC extends XYZ


2 {
3 class XYZ
4 {
5
6 }
7 }

42) In the below code, Class C extends Class A which has Class B as a
member inner class. Then, can you tell whether the members of Class B are
also inherited to Class C or not?
1 class A
2 {
3 int a;
4
5 class B
6 {
7 int b;
8 }
9 }
10
11 class C extends A
12 {
13
14 }

43) Can interfaces have inner classes in them?


44) How do you access hidden outer class variable in inner class?

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

1 class A
2 {
3 interface I
4 {
5 int i = 4444;
6
7 void show(int i);
8 }
9
10 class B implements I
11 {
12 public void show(int i)
13 {
14 System.out.println(this.i);
15 }
16 }
17
18 void methodA(int i)
19 {
20 new B().show(i);
21 }
22 }
23
24 public class MainClass
25 {
26 public static void main(String[] args)
27 {
28 new A().methodA(1111);
29 }
30 }

46.Create an abstract class pen with methods write () and refill () as abstract
methods

47.Use the pen class from Q46 to create a concrete class fountain pen with
additional method change Nib ()

48.Create a class monkey with jump ( ) and bite ( ) methods Create a


class human which inherits this monkey class and implements basic
animal interface with eat ( ) and sleep methods.

49.Create a class telephone with ( ) , lift ( ) and disconnected ( ) methods


as abstract methods create another class smart telephone and demonstrate
polymorphism

50.Demonstrate polymorphism using using monkey class from Q48

51.Create an interface TVremote and use it to inherit another interface


smart TVremote

52.Create a class TV which implements TVremote interface from Q51.

You might also like