What is the result?

Given:

public class MyFor3 {
public static void main(String [] args) {
int [] xx = null;
System.out.println(xx);
}
}

What is the result?

Given:

public class MyFor3 {
public static void main(String [] args) {
int [] xx = null;
System.out.println(xx);
}
}

What is the result?

A.
null

B.
compilation fails

C.
Java.lang.NullPointerException

D.
0

Explanation:
An array variable (here xx) can very well have the null value.
Note:
Null is the reserved constant used in Java to represent a void reference i.e a pointer to nothing. Internally it is just a binary 0, but in the high level Java language, it is a magic constant, quite distinct from zero, that internally could have any representation.



Leave a Reply 7

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


Jazz

Jazz

“A”- null

James

James

The Answer is A.

regueifa

regueifa

The answer is C.
teste@localhost:~> cat MyFor3.java
public class MyFor3 {
public static void main(String[] args) {
int[] xx = null;
for (int ii:xx) {
System.out.println(ii);
}
}
}
teste@localhost:~> /opt/jdk1.7.0_79/bin/javac MyFor3.java
teste@localhost:~> /opt/jdk1.7.0_79/bin/java MyFor3
Exception in thread “main” java.lang.NullPointerException
at MyFor3.main(MyFor3.java:4)
teste@localhost:~>

obrain

obrain

C.
Java.lang.NullPointerException

Kevin

Kevin

Ans:A

Because we print x array

if you use loop,it will throw NullpointerException(C)

Sucuk

Sucuk

People are so stupid on the internet. This question misses the following line: for (int ii : xx)

Including that line it will throw a NullpointerException, so answer C is correct. I have an exam dump where they took a picture of the question and the question is WITH the loop, so again, answer C is correct!