Tuesday, December 5, 2006

Questions for getting knowlwdge in Java conti......

16.)Can a reference variable of an interfece holds the address of an object of a class that implements that interface
Example
class X implements Y
{
//Method Implementations of Interface
public static void main(String args[])
{
Y y=new X();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Yes. reference variable of an interfece holds the address of an object of a class that implements that interface


17.)Explain the advantages of OOPs with the examples.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The main advantage of using OOPs is that it supports Inheritance.Inheritance is the process of acquiring the properties and methods from one entity to another entity
Other advantages are abstraction(data hiding) and polymorphism.

ENCAPSULATION: It is the data binds together code and the data it manipulates and keeps them safe from outside interference and misuse. When data and code are linked together in this fashion, an object is created. In other words an object is the device that supports encapsulation.
POLYMORPHISM: The name itself suggests that “POLY” stands for many and “MORPHISM” stands for forms. Polymorphism is the quality that allows one name to be used for two or more related but technically different purposes. In general giving multiple interfaces to a function or an operator is called “POLYMORPHISM”.INHERITANCE: Inheritance is the process by which one object can acquire the properties of another. The best example for this is C,C++, and JAVA languages itself. Java is being derived from C++ which in return is being derived from C from where it inherits all the capabilities of C,C++ and also add the features of its own.


18.) difference between data abstraction vs encapsulation
----------------------------------------------------------------------------------------------------------------------------------------------------------------

Abstraction is achieved through encapsulation. Abstraction solves the problem in the design side while encapsulation is the implementation.
In Short Abstraction is "Collection of data" and Encapsulation is "Exposure (or grouping) of data in appropriate access specifier".


19.)difference between class and object?


Class is like a template where as object is the implementation of the template. By using the same we can have any no .of implementations

Class is the blueprint to construct a building. object is the physical constructed building . we can construct any no .of buildings with the same blue print.

20.)What are there to update the instance variables?
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Setter methods (or) simply called as mutators can update the instance variables


21.)Can instance variables can be overridden?
----------------------------------------------------------------------------------------------------------------------------------------------------------------

Instance variables can not be overridden. Only methods which a subclass extends its super class and methods from the interface can be overridden.

22.)How many polymorphism are there in OOPS and how many supports Java?
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Coercion,overloading,parametric, and inclusion among these java supports only three


23.) How come jvm know what to do with marker interface I mean we are not implementing any method. Will it know with the name of the marker interface what to do?
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
jvm does not know bout marker interfaces. yr code, or sun knowns bout marker interfaces via reflection.

24.) explain the difference between response.sendRedirect and forward methods
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

when client gives req to a servletà response.sendredirect() method partially delegates the control to another servlet or jsp by giving request to that,and recieves the response from that servlet.then our requested servlet includes the response of its own and another servlet,and that willbe passed to the client.that means client doesnot knowthat control goes to another servlet,that willbe happened internally.

incaseof forward() method control permanently goes to another servlet.the output ofthe first servlet willbe discarded.The only response of redirected servlet willbe displayed to the browser.


25.) How can we make a hashmap synchronised by using the collections framework
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
There is a static method in Collection Using which u can obtain a Synchronized version of HashMap..

Map m = Collections.synchronizedMap(new HashMap(...));

Questions for getting knowlwdge in Java

LANGUAGE MAKES SIMPLE
1.)What is the difference between the static variable and local variable?
________________________________________________________________________________
a) static variable just it is global variable.We access this variable through this classname. variable;
where in case of local variable u can access that particular method alone u can't access that variable where u declare other than that method.
b) static variable has a single copy of the object where as an ordinary variable store a indivdual element

2)What is the difference between abstract class and interface?
--------------------------------------------------------------------------------

a) . abstract classes have a concrete Method while interface have no method implementation.
b). abstract class comes in to inherit chain while interface don’t
c). Abstract class may have concrete methods which need not require to be overridden but In Interface all methods need to be overridden.


3.) Whats the difference between length() and .length?...
--------------------------------------------------------------------------------

length() is the method and length is the keyword. length() is used to find the length of the string in String object. length is used to know the size of array.


4.)Take 2 classes.i.e., Class A,Class B.In Class B declare constructor as Private.But I have to access Class A using the constructor in Class B.How is it possible?One interviewer told that Through getInstance() method it's possible to access Class A.eEven Class B declare as Private.How is it possible?
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Yes its true when u have method in ClassB as getInstance() which return the new ClassB(). Here it is about creating singleton object of ClassB which returns only one instance at give point.
5.)What is the use of super keyword. give me an example.
--------------------------------------------------------------------------------
With the keyword 'super', you can access the overridden methods and data.
public class A {
int i = 0;
void fun(){
i = 9;
}
}
class B extends A {
int j = 0;
void fun() {
j = 8;
super.fun ();
j = j - i;
}
}

6.)When to extend Thread Class and when to implement Runnable Interface
----------------------------------------------------------------------------------------------------------------------------------------------------------------

I will always prefer the Runnable Interface. Reason being, if we extend to Thread class, then we cannot extend any other class from our application. So if there is a class that can provide useful functionality to us, then we cannot extend it and use its "protected" fields.Moral of the Story - Gud Designer will "ALWAYS" use Runnable interface.

7.)Why local variable doesn't initialized by default while class variable may be?
----------------------------------------------------------------------------------------------------------------------------------------------------------------

A constructor initializes an object immediately upon creation. If we do not explicitly define a constructor for a class, then java creates a default constructor for the class. The default constructor automatically initializes all instance variables to zero. So local variables doesn't initialized by default while class variable does.

8.)Does Java have pass by value or pass by reference or both?Please Explain it with example
----------------------------------------------------------------------------------------------------------------------------------------------------------------
a.)"pass by value" and "pass by ref" are both defined in java. In pass by value technique.....a copy of aurguments are passed to the method......( ex: sum(10,5) and in pass by ref technique.....a ref to the values are passed to the method.....(exp: sum(a,b))
b.) java has both i.e. pass by value and pass by ref. When primitive types are passed, they are passed by value but when objects are passed, they are passed by ref. And reference itself is passed by value.

9.)What is the difference between function/method synchronization and object synchronization?Explain with an example
----------------------------------------------------------------------------------------------------------------------------------------------------------------
synchronize method

To synchronize an entire method, you just include the synchronized keyword as one of the method qualifiers, as in:

class syncDemo{

synchronized void test{

//your synchronized code

}

synchronize object

To create a synchronized object, you use the synchronized keyword with an expression that evaluates to an object reference, as in

class syncDemo{

void test{

synchronized (this){

//your synchronized code

}

function or method synchronization synchronizes entire method i.e.synchronized void MyMethod(){...}this will place a lock on the object until current thread finishes execution. This ensures that MyMethod is always accessed by a single thread at a time.But consider this,void MyMethod(){.... someOtherMethod();..}Here you might need to just make sure that call to someOtherMethod() is synchronized, so instead of making entire method synchronized, you just make a synchronized block around someOtherMethod()void MyMethod(){....synchronize{ someOtherMethod();}..}This improves perfomance by locking object for shorter duration.Of-course you need to analyze your code correctly for doing this. There is not straight forward answer as to when to use Synchronize method vs Synchronize block.

10.)When to use a CharacterStream or a ByteStream
----------------------------------------------------------------------------------------------------------------------------------------------------------------

In general, we should use the character stream classes when working with characters or strings, and use the byte stream classes when working with bytes or other binary objects.


11.)What is the difference between a class and Interface?
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Interfaces (pure behavior) and classes (state and behavior). Interfaces are used in Java to specify the behavior of derived classes. Where in classes we usually declare and initialize variables and these will be called as state and concrete methods are said to be behavior in classes.

12.)What is the significance of Marker interface? Why are we using, even though it has no method?
----------------------------------------------------------------------------------------------------------------------------------------------------------------

The marker methods specifies the functionality of a class depending upon the need. For example if a method needs to be serialised then it should implement serialiazable which doesnt have any methods to define, but an indication for the JVM that this class instatiates objects which can be inserted into an byte stream

13.)What is meant by Serialization and Externalization?
--------------------------------------------------------------------------------

Marker Interface is used by java runtime engine (JVM) to identify the class for special processing.

Serialization is a process of converting state of an object to stream of bytes that can be sent over network or can be stored in a file or in database to be reconstructed into Object Later when required. Remember EJB spec, all EJB (Session or Entity) implements Serializable interface indirectly, in case of Statefull session bean it can be Passivated or Activated (Persisted by writing it to file system or Database this is done by container).


Externalization is same as Serialization except that WriteObject() and ReadObject() method are called by JVM during Serialization and Serialization of object. One thing you can do with Externalization is that you can store extra information into object like STATIC variables and transient variables or you can add more information if you have any business need. One good example is compressing and uncompressing of data to send it through network or converting one format to other like a BMP image to JPEG or GIF format.


14.)What is the difference between Serializalble and Externalizable interface?
----------------------------------------------------------------------------------------------------------------------------------------------------------------
When you use Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject() two methods to control more complex object serailization process. When you use Externalizable interface, you have a complete control over your class's serialization process.

15.)String a=new String("A");
String b=new String("A");
String c="A";
String d="A";

Then which of the following returns true?

1. if(a==b)
2. if(b==c)
3. if(c==d)
4. if(d==a)
----------------------------------------------------------------------------------------------------------------------------------------------------------------


the if(c==d) gives the true only. the reason behind this is whenever you create the object by not using "new" operator like string a="A" then it will search whether is there any object with same content.if there is any same object then it simply assigns to previous existing object.but when you create the object with new then it will create new obj every time.thats all


15.) What is the difference between an interface and abstract class?
----------------------------------------------------------------------------------------------------------------------------------------------------------------

In the interface all methods must be abstract; in the abstract class some methods can be concrete. In the interface no accessibility modifiers are allowed, which is ok in abstract classes.
Abstract class means
1)We can have concreate methods and abstract methods
2) We can give implementation for concreate methods.
Interfaces means
1)we could not have concreate methods. All the methods are abstract methods implicitly.
2)And those methods allows only public access specifier only. It wont
allow any final, static keywords with methods