What is the result?

Given:

1. public class ClassA {
2. public void methodA() {
3. ClassB classB = new ClassB();
4. classB.getValue();
5. }
6. }
7.
8. class ClassB {
9. public ClassC classC;
10. public String getValue() {
11. return classC.getValue();
12. }
13. }
14.
15. class ClassC {
16. public String value;
17. public String getValue() {
18. value = “ClassC”;
19. return value;
20. }
21. }

and:

ClassA a = new ClassA();
a.methodA();

What is the result?

Given:

1. public class ClassA {
2. public void methodA() {
3. ClassB classB = new ClassB();
4. classB.getValue();
5. }
6. }
7.
8. class ClassB {
9. public ClassC classC;
10. public String getValue() {
11. return classC.getValue();
12. }
13. }
14.
15. class ClassC {
16. public String value;
17. public String getValue() {
18. value = “ClassC”;
19. return value;
20. }
21. }

and:

ClassA a = new ClassA();
a.methodA();

What is the result?

A.
Compilation fails.

B.
ClassC is displayed.

C.
The code runs with no output.

D.
An exception is thrown at runtime.

Explanation:
Exception in thread “main” java.lang.NullPointerException
at ClassB.getValue(Main.java:11)
at ClassA.methodA(Main.java:2)
at a.methodA();



Leave a Reply 0

Your email address will not be published. Required fields are marked *