Wednesday, October 15, 2008

Core java

How does serialization work?
Basically Serialization is used to maintain the state of an object. When a program reads or write an object from the input stream or to output stream an object must be serialized first, so that values of that object doesn't change. Serialization is needed in Remote Method Invocation.

How would you keep track of a session?
Session tracking is a mechanism that servlets use to maintain state about a series of requests from the same user (that is, requests originating from the same browser) across some period of time.

Explain the keywords - transient, finally?
A transient variable is a variable that may not be serialized.
Final variable cannot be changed. Final method cannot be overridden. Final class cannot be derived.

Can a method be static and synchronized?
Yes a method can be specified as static as well as synchronized.
Example
class a
{
public static synchronized int function() {}
}
This is call class level synchronization.

How can you do multiple inheritances in Java?
Its not possible directly. That means this feature is not provided by Java, but it can be achieved with the help of Interface. By implementing more than one interface.

What is data encapsulation?
Encapsulation mean data hiding. data correspond to variables and methods of the class. Data hiding mean: when u make different objects of the same class. The data stored in one object is unaware of the data for other object of the same class. In, this way it gives secure data.

What are the primitive types in Java?
According to their memory allocation order:byte,short,int,long,float,double,Boolean,char.

What are the different kinds of exceptions? How do you catch a Runtime exception?
There are 2 types of exceptions. 1. Checked exception 2. Unchecked exception. Checked exception is catched at the compile time while unchecked exception is checked at run time.
The checked exception are syntactical errors found in the code
ex:
class a
{
Public static void main(String args[])
{
System.out.println("bbb")
}
}
error:missing the semicolons
The unchecked exception can be represent inefficiency of the computer system to execute a particular statement
ex:
calss err
{
public static main()
{
System.out.println("bbb");
}
}

JVM is platform independent/dependent? why?
JVM is platform independent, because it coverts the Java code into Bytecode. Bytecode is same for every machine.

How hash table is synchronized? why hash map is not s...?
hash table is a legacy class. every legacy class provides shnchronization.we can make hash map synchronized but it call extra method.hashmap is not synchronized but it part of collections and it enter null values.

Explain the importance of "static"keyword?
static is used to to call any method and inner class without any instance of class. it executes before main methods.

Is it possible to compile and run a java program with out writing main() method?
yes,it is possible by using a siatic block
Ex:
class a
{
static
{
System.out.println("bbb");
System.exit(0);
}
}
Why ArrayList is faster than Vector?
ArrayList is faster than Vector, because ArrayList is not synchronized. Synchronization will reduce the performance of Vector. Vector is a legecy class and Arraylist is collection class.

What is the meaning of "final" keyword?
Final keyword is used for define constant variables and method and no body can change that variable and method within program.
ex:
class a
{
final void method1()
{
System.out.println("ggg");
}
}
class b
{ void method2()
{
a.method1();
}
}
What is the difference between Enumeration and Iterator?
Enumeration can be used for Collection objects as well as Iterator can be used for legacy classes.Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as using Iterator we can manipulate the objects also like adding and removing the objects.

What is the difference between tomact and weblogic?
WEBLOGIC:
1. is a application server.
2. Supports many protocols.
3. Used for enterprise applications and EJBs.
4. Supports N-Tier architectures.
5. Provides more scalability.
TOMCAT:
1. is a web server
2. supports only HTTP / HTTPS protocols
3. Used for Dynamic / static pages only (E.g servlets, JSPs, HTML etc.)
4. Supports 3-Tier architecture
5. Provides nominal scalability

How to Synchronize the HashMap?
Collections.getSynchronizedMap(HashMap)Use Collections.synchronizedMap(HashMap instance)

What is Thread?
Thread is sepatare path of execution with in a program.It is a light-weight because they utilize minimum resourse of the system.This means they take less memory and less processer time.

what is daemon threads?
Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread.

How can you stop a thread in java?
When the user wants to stop the thred,we should store'true' into the variables.The status of the variable is checked in the run() method and if it is true,the thread executes'return'statement and then stops.

What is serialization?Give me example?
The processing of storing object contents into a file.
Ex:
calss employee implements serializable
{//instace variable
private int id;
private string name;
private float sal;
private date doj;
}

What are the Acess specifiers in java?
Public-To acess to any where
Pivate-To accessible only with in a class.
Protected-To acess with in the same directory.
Default-To accessible outside the class,but with in the same directory.

What is the difference between String and StringBuffer? Which is better to use in project?
String buffer is a mutable string object. String is a non - mutable object. When we say mutable means which can be changed anytime. Its better to string buffer if we want to modify a string from multiple threads(Some places synchronization is required).

what is difference between instance and object.?what are the all difference between interface and abstract class?
instance means just creating a reference(copy) .
object :means when memory location is associated with the object( is a runtime entity of the class) by using the new operator
interface is a set of abstract methods, all of which have to be overriden by the class whichever implements the interfaceabstract class is a collection of data and methods which are abstact (not all of them)

What is collection framework?what interfaces and classes support collection framework?
A Collection Framework is a generic types which you use to create collection classes that support various ways to store an dmange objects of any kind in memory.The Collection Framwork supports the folowing Interfaces:1) Collection2) Set3) List4) Sorted setThe Collection Framwork supports the following Classes:1) HashSet2) TreeSet3) LinkedList4) ArrayList5) HashMap6) Vector7) LinkedHashSet8) HashTable

what is difference between array & arraylist?
ArrayList is a part of the Collection Framework.We can store any type of objects, and we can deal with only objects.It is growable.Array is collection of similar data items.We can have array of primitives or objects.It is of fixed size,We can have multi dimensional arrays.

what is difference between string and stringtokenizer?
Obviously StringTokenizer as it is suggested by its name tokenizes a String supplied to it as an argument to its constructor and the character based on which tokens of that string are to be made.The default tokenizing character is space " ".
A StringTokenizer is utility class used to break up string.
e.g.
StringTokenizer st = new StringTokenizer("Hello World"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); }
Output:
Hello

What is the difference between interrupt() and join() in threads?Give an example program?
Interupt is the method on the thread to distrub or interupt on the other thread which are under execution.
Where as the join method is used to say t.join() is to suspend the current thread operation for a time being and go to the other thread and return back after completing the other task.
t.join();
t.join(200);//here the current thread is suspended for a 200msec,with in this time u have to return back after completing the task.

Tell me the differences between enumeration and iteration?Which can use where in realtime?
Enumeration deals with objects while iteration deals with values only. Enumeration is used when we use vector, hashtable, etc while iteration are used in while loop, for loop etc

what is the difference between synchronized block and normal block ?
Synchronized Block:-->
Sysnchronization is Used in a case of Multithreading where Acess of multiple threads to a resource concuurently can alter the integrity of data.Synchronization is used to controll acess to a shared resource.
Synchronized block allows execution of arbitary code on the lock of arbitary object-->
eg:--synchronized()
{
/*code*/
}
with this to no two threads can execute this code block on the Object denoted by objref .Any thread which acess to exceute this code first have to aquire the lock og object thebn it can proceed..at the same time any other thread wishing to execute the code block have to wait to acquire the lock of the object as it has been acquired by another thread.

What is the purpose of Hashcode in java?
Hashcode is a unique identification number allotted to the objects by the JVM.


How many threads are need to run JVM?
The JVM is a single main thread. It is a user thread.(remember there are two types of threads- user and deamon threads). When this user thread ends, all other deamon threads will terminate- this is an inference...

What is the difference between ArrayList and Vector otherthan arrayList is not synchronised but Vector is?
Arraylist has no default size while vector has a default size of 10.-Arraylist don't define any increment size while vector does.-arraylist can be seen directly without any iterator while vector requires an iterator to display all it's content. (not very sure).
It is similar to ArrayList, but with two differences: Vector is synchronized, and it contains many legacy methods that are not part of the collections framework.

What is the difference between Static and final?
Static variable can change their values, final variables can not be changed they are constants . Static variable it attached to their class not with the object only one copy of static variable is exists and they can be called with reference of their class only final classed can not be extended and their are not static classes.

what is the difference betweeen sendRedirect() and requestDispatcher()?
RequestDispatcher is used to redirect a request on the server side.It is used in the follwing way: getrequestdispatcher().forward() : forwards to a resource ie the control for the request and the response are with the forwarded resource.getrequestdispatcher().include() : include the specified resource. The control still lies with the resource thet calls the include method.sendRedirect is called for a redirection by the browsers. A status code of 302 is neede for the send redirect to work

What is Difference b/w this() and super()?Give me example?
this() can be used to refer to same class
Example:
pulic void setage(int age)
{
this.age=age;
}
super() can be used to refer to super cclass variables
Ex:
super.method();
super(values);

Why java is suitable for internet?

Java is suitable for Internet because of two main reasons

  1. It is system independent and hence its programs can run on any type of computer system available on internet.
  2. It eliminates a lot of security problems for data on internate.

How can you find the hash code of an object?

The hash code() method of ‘object’class in java.lang package is useful to find the hash code of an object.

Can you declare a class as ‘private’?

No,if we declare a class as private,then it is not available to java compiler and hence a compiler time error occurs.But,inner classes can be declared as private.

What is constructor overloading?

Writing two or more constructors with the same name but with difference in the parameters is called constructor overloading.Such constructors are useful to perform different tasks.

What are instance methods?

The methods which act on the instace variable of the class.To call the instace methods we should use the form:objectname.methodname().

What are static methods?

The methods which do not act upon the instance variables of a class.Static methods are declared as ‘static’.

What is the difference between instance variables and static variables?

  1. An instance variable is a variable whose separate copy is available to each object. A class variable is a variable whose single copy in memory is shared by all objects.
  2. Instance variables are created in the objects on heap memory.Class variables are stored on method area.

What is the advantage of inheritance?

In inheritance, a programmer reuses the super class code without rewriting it,in creating of sub classes. So, developing the classes becomes very easy. Hence, the programmers productivity is increased.

Why multiple inheritance is not available in Java?

Multiple inheritance is not available in java for the following reasons:

  • It leads to confusion for a java program.
  • The programmer can achieve multiple inheritance by using interfaces.
  • The programmer can achieve multiple inheritance by repeatedly using single inheritance.

How many types of inheritance are there?

There are two types of inheritance single and multiple. All other types are more combinations of these two. However, java supports only single inheritance.

What is coercion?

Coercion is the automatic conversion between data types done by the compiler.

What is the conversion?

Conversion is an explicit change in the data types specified by the cast operator.

What is method signature?

Method signature represents the method name along with method parameters.

Can you override private methods?

No.Private methods are not available in the sub classes, so they cannot be overridden.

Can you private methods and final methods as same?

Yes. The java compiler assigns the value for the private methods at the time of compilation.Also,private methods can not be modifier at run time.This is the same case with final methods also.

What is the difference between primitive data types and advanced data types?

Primitive data types represents single values.Advanced data types represent a group of values. Also, methods are available to handle the primitive data types.

Can you write an interface without any methods?

Yes.

What do you call the interface without any members?

An interface without any members is called making interface.It makes the class objects for a special purpose.

For Example: Cloneable (java.lang) and Serializable (java.io) are two making interfaces.

Can you implement one interface from another?

No. We cannot implementing an interface means writing body for the methods.This can not be done again in an interface.

Can you write a class within an interface?

Yes, it is possible to write a class within an interface.

How can you call the garbage collector?

We can call garbage collector of JVM to delete any unused variables and unreferenced objects from memory using gc() method.

For Example:

System.gc();

Runtime.getRuntime().gc();

What is CLASSPATH?

The CLASSPATH is an environment variable that tells the java compiler where to look for class files to import.CLASSPATH is generally set to a directory or a JAR file.

What is JAR file?

A java Archive file(JAR) is a file that contains compressed version of several .class files,audio,files,image files or directories.JAR file is useful to bundle up several files related to a project and use them easily.

Why do we need wrapper class?

They convert primitive data types into objects and this is needed on Internet to communicates between two application.

What is a collection framework?

A collection framework is a class library to handle groups of objects. Collection framework is implemented in java.util package.

Does a collection object store copies of other objects or their references?

A collection object stores references of other objects.

Can you store a primitive data types into a collection?

No, Collections store only objects.

What is the difference between Iterator and Listiterator?

Both are useful to retrieve elements from a collection. Iterator can retrieve the elements only in forward direction. But Listiterator can retrieve the elements in forward and backward direction also.So Listiterator is preffered to iterator.

What is the difference between Iterator and Enumeration?

Both are useful to retrieve elements from a collection.Iterator methods whose names are easy to follow and Enumeration methods are diffcult to remember.Also Iterator has an option to remove elements from collection which is not available in Enumeration.

What is auto boxing?

Converting a primitive data type into an object from automatically is called auto boxing.Auto boxing is done in generic types.

Can you synchronized the Arraylist Object?

Yes, we can use synchronizedList() method to synchronize the Arraylist, as:

Collections.synchronizedList(new ArrayList());

What is the difference between Set and List:

Set:

  • A set represent a collection of elements.Order of the elements may change in the Set.
  • Set will not allow duplicate values to be stored and not allow null elements.

List:

  • A List represents ordered collection of elements.List preserves the order of elements in which they are entered.
  • List allow duplicate values and allow null elements.

What is IP address?

An IP address is a unique identification number allotted to every computer on a network or Internet.IP address contains some bytes which identify the network and the actual computer inside the network.

What is DNS?

Domain Naming Service is a service on internet that maps the IP addresses with corresponding website names.

What is Socket?

A Socket is a point of connection between a server and a client on a network.

What is port number?

Port number is a 2 byte number which is used to identify a socket uniquely.

Write a program to find the thread used by JVM to execute the statements?

Class sss

{

Public static void main(String args[])

{

System.out.println(“bbbbb”);

Thread t = Thread.currentThread();

}

}

Which method is executed by the thread by default?

Public void run() method.

What is the difference between sleep() and wait() methods?

Both the sleep() and wait() methods are used to suspend a thread execution for a specified time.When sleep() is executed inside a synchronized block, the object is still under lock. When wait() method is executed, it breaks the synchronized block,so that the object lock is removed and it is available.

Generally, sleep() is used for making a thread to wait for some time. But wait() is used in connection with notify() r notifyall() methods in thread communication.

What is Thread deadlock?

When a Thread locked an object and waiting for another object to be released by another Thread and other thread is also waiting for the first thread is released the first thread object, both the threads will continue waiting forever.

What is a default priority of a thread?

When a thread is created, by default its priority will be 5.

What is a daemon thread?

A daemon thread is a thread that executes continuously. Daemon threads are service providers for other threads or objects. It generally provides a background processing.

What is Thread life cycle?

  • A thread is created using new thread() statement and is executed by start() method.
  • The thread enters ‘runnable’ state and when sleep() or wait() methods are used or when the thread is blocked on I/O, it then goes inti ‘not runnable’ state.
  • From ‘not runnable’state, the thread comes back to the ‘runnable’ state and continues running the statements.
  • The thread dies when it comes out of run() method.

What is the advantage of stream concept?

Streams are mainly useful to move data from one place to another place.This concept can be used to receive data from an input device and send data to an output device.

What is the default buffer size used by any buffered class?

512 bytes

What is de-serialization?

De-serialization is a process of reading back the objects from a file.

What is Throwable?

Throwable is a class that represents all errors and exceptions which may occur in java.


Which is the super class for all exceptions?

Exception is the super class of all exceptions in java.


What is the difference between an exception and an error?

An exception is an error which can be handled. It means when an exception happens, the programmer can do something to avoid any harm.But an error is an erroe which cannot be handled, it happens and the programmer cannot do any thing.







No comments: