Thursday, June 24, 2010

CORE JAVA CONTINUES ... 3

QUESTION: Why are there no global variables in Java?
ANSWER: Global variables are considered bad form for a variety of reasons:
· Adding state variables breaks referential transparency (you no longer can understand a statement or expression on its own: you need to understand it in the context of the settings of the global variables).

· State variables lessen the cohesion of a program: you need to know more to understand how something works. A major point of Object-Oriented programming is to break up global state into more easily understood collections of local state.

· When you add one variable, you limit the use of your program to one instance. What you thought was global, someone else might think of as local: they may want to run two copies of your program at once.

For these reasons, Java decided to ban global variables.

QUESTION: What does it mean that a class or member is final?
ANSWER: A final class can no longer be subclassed. Mostly this is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve. Methods may be declared final as well. This means they may not be overridden in a subclass. Fields can be declared final, too. However, this has a completely different meaning. A final field cannot be changed after it's initialized, and it must include an initializer statement where it's declared. For example,

public final double c = 2.998;
It's also possible to make a static field final to get the effect of C++'s const statement or some uses of C's #define, e.g.
public static final double c = 2.998;
QUESTION: What does it mean that a method or class is abstract?
ANSWER: An abstract class cannot be instantiated. Only its subclasses can be instantiated. You indicate that a class is abstract with the abstract keyword like this:
public abstract class Container extends Component { Abstract classes may contain abstract methods. A method declared abstract is not actually implemented in the current class. It exists only to be overridden in subclasses. It has no body. For example,
public abstract float price();

Abstract methods may only be included in abstract classes. However, an abstract class is not required to have any abstract methods, though most of them do. Each subclass of an abstract class must override the abstract methods of its superclasses or itself be declared abstract.
QUESTION: what is a transient variable?
ANSWER: transient variable is a variable that may not be serialized.

QUESTION: How are Observer and Observable used?
ANSWER: Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.

QUESTION: Can a lock be acquired on a class?
ANSWER: Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object.

QUESTION: What state does a thread enter when it terminates its processing?
ANSWER: When a thread terminates its processing, it enters the dead state.
QUESTION: How does Java handle integer overflows and underflows?
ANSWER: It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.

QUESTION: What is the difference between the >> and >>> operators?
ANSWER: The >> operator carries the sign bit when shifting right. The >>> zero-fills bits that have been shifted out.
QUESTION: Is sizeof a keyword?
ANSWER: The sizeof operator is not a keyword.

QUESTION: Does garbage collection guarantee that a program will not run out of memory?
ANSWER: Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection

QUESTION: Can an object's finalize() method be invoked while it is reachable?
ANSWER: An object's finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object's finalize() method may be invoked by other objects.

QUESTION: What value does readLine() return when it has reached the end of a file?
ANSWER: The readLine() method returns null when it has reached the end of a file.
QUESTION: Can a for statement loop indefinitely?
ANSWER: Yes, a for statement can loop indefinitely. For example, consider the following: for(;;) ;

QUESTION: To what value is a variable of the String type automatically initialized?
ANSWER: The default value of an String type is null.

QUESTION: What is a task's priority and how is it used in scheduling?
ANSWER: A task's priority is an integer value that identifies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks before lower priority tasks.