Today's Jobs :


CORE JAVA FREQUENTLY ASKED QUESTIONS AND ANSWERS-10


CORE JAVA FREQUENTLY ASKED QUESTIONS AND ANSWERS-10

 Question: What happens to a static var that is defined within a method of a class ?
Answer:   Can't do it. You'll get a compilation error

Question: How many static init can you have ?
Answer:   As many as you want, but the static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope.

Question: What is the difference amongst JVM Spec, JVM Implementation, JVM Runtime ?
Answer:   The JVM spec is the blueprint for the JVM generated and owned by Sun. The JVM implementation is the actual implementation of the spec by a vendor and the JVM runtime is the actual running instance of a JVM implementation

Question: Describe what happens when an object is created in Java?
Answer:   Several things happen in a particular order to ensure the object is constructed properly:
1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its superclasses. Implemenation-specific data includes pointers to class and method data.
2. The instance variables of the objects are initialized to their default values.
3. The constructor for the most derived class is invoked. The first thing a constructor does is call the consctructor for its superclasses. This process continues until the constrcutor for java.lang.Object is called, as java.lang.Object is the base class for all objects in java.
4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.

Question: What does the "final" keyword mean in front of a variable? A method? A class?
Answer:   FINAL for a variable : value is constant
FINAL for a method : cannot be overridden
FINAL for a class : cannot be derived

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. <br> 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.