Wednesday, October 24, 2007

CORE JAVA Interview Questions-Part15


Question: What is the difference between instanceof and isInstance?

Answer: instanceof is used to check to see if an object can be cast into a specified type without throwing a cast class exception.

isInstance()

Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.

Question: Wha is the output from System.out.println("Hello"+null);

Answer: Hellonull

Question: Whats the difference between notify() and notifyAll()?

Answer: notify() is used to unblock one waiting thread; notifyAll() is used to unblock all of them. Using notify() is preferable (for efficiency) when only one blocked thread can benefit from the change (for example, when freeing a buffer back into a pool). notifyAll() is necessary (for correctness) if multiple threads should resume (for example, when releasing a "writer" lock on a file might permit all "readers" to resume).

Question: Why can't I say just abs() or sin() instead of Math.abs() and Math.sin()?

Answer: The import statement does not bring methods into your local name space. It lets you abbreviate class names, but not get rid of them altogether. That's just the way it works, you'll get used to it. It's really a lot safer this way.
However, there is actually a little trick you can use in some cases that gets you what you want. If your top-level class doesn't need to inherit from anything else, make it inherit from java.lang.Math. That *does* bring all the methods into your local name space. But you can't use this trick in an applet, because you have to inherit from java.awt.Applet. And actually, you can't use it on java.lang.Math at all, because Math is a "final" class which means it can't be extended.

Question: Is "abc" a primitive value?

Answer: The String literal "abc" is not a primitive value. It is a String object.

Question: What restrictions are placed on the values of each case of a switch statement?

Answer: During compilation, the values of each case of a switch statement must evaluate to a value that can be promoted to an int value.

Question: What modifiers may be used with an interface declaration?

Answer: An interface may be declared as public or abstract.

Question: Is a class a subclass of itself?

Answer: A class is a subclass of itself.

0 comments:

Advertisement

 

Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com