Thursday, June 24, 2010

CORE JAVA CONTD ... 2

QUESTION: Difference between a Class and an Object ?
ANSWER: A class is a definition or prototype whereas an object is an instance or living representation of the prototype

QUESTION: What is the difference between method overriding and overloading?
ANSWER: Overriding is a method with the same name and arguments as in a parent, whereas overloading is the same method name but different arguments

QUESTION: What is a "stateless" protocol ?
ANSWER: Without getting into lengthy debates, it is generally accepted that protocols like HTTP are stateless i.e. there is no retention of state between a transaction which is a single request response combination

QUESTION: What is constructor chaining and how is it achieved in Java ?
ANSWER: A child object constructor always first needs to construct its parent (which in turn calls its parent constructor.). In Java it is done via an implicit call to the no-args constructor as the first statement.

QUESTION: What is passed by ref and what by value ?
ANSWER: All Java method arguments are passed by value. However, Java does manipulate objects by reference, and all object variables themselves are references

QUESTION: You can create a String object as String str = "abc"; Why cant a button object be created as Button bt = "abc";? Explain
ANSWER: The main reason you cannot create a button by Button bt1= "abc"; is because "abc" is a literal string (something slightly different than a String object, by-the-way) and bt1 is a Button object. The only object in Java that can be assigned a literal String is java.lang.String. Important to note that you are NOT calling a java.lang.String constuctor when you type String s = "abc";

QUESTION: What does the "abstract" keyword mean in front of a method? A class?
ANSWER: Abstract keyword declares either a method or a class. If a method has a abstract keyword in front of it,it is called abstract method.Abstract method hs no body.It has only arguments and return type.Abstract methods act as placeholder methods that are implemented in the subclasses.Abstract classes can't be instantiated.If a class is declared as abstract,no objects of that class can be created.If a class contains any abstract method it must be declared as abstract

QUESTION: How many methods do u implement if implement the Serializable Interface?
ANSWER: The Serializable interface is just a "marker" interface, with no methods of its own to implement. Other 'marker' interfaces are java.rmi.Remote java.util.EventListener
QUESTION: What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. import java.net.* versus import java.net.Socket)?
ANSWER: It makes no difference in the generated class files since only the classes that are actually used are referenced by the generated class file. There is another practical benefit to importing single classes, and this arises when two (or more) packages have classes with the same name. Take java.util.Timer and javax.swing.Timer, for example. If I import java.util.* and javax.swing.* and then try to use "Timer", I get an error while compiling (the class name is ambiguous between both packages). Let's say what you really wanted was the javax.swing.Timer class, and the only classes you plan on using in java.util are Collection and HashMap. In this case, some people will prefer to import java.util.Collection and import java.util.HashMap instead of importing java.util.*. This will now allow them to use Timer, Collection, HashMap, and other javax.swing classes without using fully qualified class names in.

QUESTION: What is the difference between logical data independence and physical data independence?
ANSWER: Logical Data Independence - meaning immunity of external schemas to changeds in conceptual schema. Physical Data Independence - meaning immunity of conceptual schema to changes in the internal schema.
QUESTION: What is user defined exception ?
ANSWER: Apart from the exceptions already defined in Java package libraries, user can define his own exception classes by extending Exception class.

QUESTION: Difference Between Abstraction and Encapsulation
ANSWER: Abstraction is removing some distinctions between objects, so as to show their commonalities. Encapsulation is hiding the details of the implementation of an object so that there are no external dependencies on the particular implementation.