Professional Documents
Culture Documents
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 }
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
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 }
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 }
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
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 }
1 class X
2 {
3 abstract class Y
4 {
5 class Z
6 {
7
8 }
9 }
10 }
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 }
}
1 class A
2 {
3 void methodOfA()
4 {
5 abstract class B
6 {
7
8 }
9 }
10 }
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 }
1 abstract class A
2 {
3 synchronized abstract void method();
4 }
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 }
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?
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 }
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
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 }
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?
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 }
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 ()