
Incorrect Core Java Interview Answers - cfontes
http://java.dzone.com/articles/incorrect-core-java-interview
======
dpark
_> Which is the base class for all classes?

> java.lang.Object

> This is true for custom classes. For primitive types such as int.class,
> void.class and Object itself have no super class._

This is misleading. Primitives (and void) are not classes at all, which is why
they do not have a superclass. Interfaces also don't have superclasses for the
same reason. These all _do_ have a representation of the type
'java.lang.Class', and they all support the class literal (X.class)
expression, but they are not classes. java.lang.Object is still the base class
for all _classes_.

If I had an interviewee throw this stuff at me, I'd consider it a negative. If
you're going to be pedantic, you'd better be correct.

As for this:

 _> Which one is faster in Java ?

> for(int i = 100000; i > 0; i--) {}

> for(int i = 1; i < 100001; i++) {}

> Answer: Which ever is run second with be fastest. The server JVM can detect
> and eliminate loops which don't do anything..._

Will the compiler itself not detect and eliminate these?

Edit: Apparently no, the compiler won't optimize those away. I can't imagine
why. Whether the JIT will optimize away the second loop isn't as clear as he
indicates, though. Assuming they are in separate methods, optimizing the first
away (after it runs) will have no effect on the second.

~~~
peter_lawrey
A Class is a proxy for the underlying representation of the type/class. IMHO,
int.class is just as much a Class as Integer.class.

The compiler does next to no optimisations and the "client" JIT doesn't remove
loops which don't do anything. Only the "server" JIT eliminates such loops,
after it has compiled the method which can be triggered by loop which loops
many times.

~~~
dpark
> A Class is a proxy for the underlying representation of the type/class.
> IMHO, int.class is just as much a Class as Integer.class.

There are two separate things here: Class and class. Capital-C "Class" is a
proxy for a type. Small-c "class" is a kind of type in Java. When someone asks
why 'int' does not have a super class, the answer is because 'int' is not a
class. It does have a corresponding Class* (which might have been more
appropriated named "Type"), though. (There's also the '.class' literal which
yields a Class. Again, the naming could have been better.)

* The Class returned by int.class actually does have a super class, because Class is a class. e.g.: int.class.getClass().getSuperclass() yields a Class representing Object.

------
JoeAltmaier
I understood that in Java, objects are passed by reference and scalars by
value?

~~~
Confusion
Everything in Java is passed by value, as is the case in, for example, C[1]
and Ruby. That the value that is passed is also called a reference does not
change the fact that it is not call-by-reference.

There is a simple way to check whether a language does call-by-reference: can
you write a swap function such that after calling swap(a, b), the variables a
and b in the calling function refer to different values than before? In Java
this is impossible.

Because the terminology is so confusing when object reference values are being
passed-by-value, Barbara Liskov suggested we call it call-by-sharing.

[1] you can make C look like it's doing pass-by-reference by throwing some &
and __* operators around. That doesn't change the fact that it is purely call-
by-value.

~~~
JoeAltmaier
I believe any object passed in Java can be modifed by the receiving method,
right? So that is pass-by-reference. If we want to say we're really passing a
reference by value, that begins to be silly - if the object passed can be
modified, its a reference.

------
mrspandex
Original article: [http://vanillajava.blogspot.com/2011/07/incorrect-core-
java-...](http://vanillajava.blogspot.com/2011/07/incorrect-core-java-
interview-answers.html)

------
AmazingBytecode
How to tell if your interviewer is an ass.

~~~
peter_lawrey
A pedantic interviewer won't tell you if mix your terminology incorrectly. The
more experience you have, the more pedantic you tend to get because computers
are pedantic. They do exactly what you tell them to, not what you meant it to
do. ;)

