What is the result?

Given:

1. public static void main(String[] args) {
2. String str = “null”;
3. if (str == null) {
4. System.out.println(“null”);
5. } else (str.length() == 0) {
6. System.out.println(“zero”);
7. } else {
8. System.out.println(“some”);
9. }
10. }

What is the result?

Given:

1. public static void main(String[] args) {
2. String str = “null”;
3. if (str == null) {
4. System.out.println(“null”);
5. } else (str.length() == 0) {
6. System.out.println(“zero”);
7. } else {
8. System.out.println(“some”);
9. }
10. }

What is the result?

A.
null

B.
zero

C.
some

D.
Compilation fails.

E.
An exception is thrown at runtime.

Explanation:
Main.java:5: not a statement
} else (str.length() == 0) {
^
Main.java:5: ‘;’ expected
} else (str.length() == 0) {
^
Main.java:7: ‘else’ without ‘if’
} else {
^
3 errors



Leave a Reply 1

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