Thursday, June 24, 2010

CORE JAVA CONTINUES ... 5

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.

QUESTION: What is the difference between a while statement and a do statement?
ANSWER: A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.

QUESTION: What modifiers can be used with a local inner class?
ANSWER: A local inner class may be final or abstract.

QUESTION: What is the purpose of the File class?
ANSWER: The File class is used to create objects that provide access to the files and directories of a local file system.

QUESTION: Can an exception be rethrown?
ANSWER: Yes, an exception can be rethrown.

QUESTION: When does the compiler supply a default constructor for a class?
ANSWER: The compiler supplies a default constructor for a class if no other constructors are provided.

QUESTION: If a method is declared as protected, where may the method be accessed?
ANSWER: A protected method may only be accessed by classes or interfaces of the same package or by subclasses of the class in which it is declared.

QUESTION: Which non-Unicode letter characters may be used as the first character of an identifier?
ANSWER: The non-Unicode letter characters $ and _ may appear as the first character of an identifier

QUESTION: What restrictions are placed on method overloading?
ANSWER: Two methods may not have the same name and argument list but different return types.

QUESTION: What is casting?
ANSWER: There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.

QUESTION: What is the return type of a program's main() method?
ANSWER: A program's main() method has a void return type.

QUESTION: What class of exceptions are generated by the Java run-time system?
ANSWER: The Java runtime system generates RuntimeException and Error exceptions.