Saturday, April 11, 2009

Java Interview Questions

For the past few days I have been reading a lot about probable java interview questions. Finally, I thought to share it and here is what I have got. For now I will add some fundamental questions and answers and keep the post updating as I come across more. I have tried to put the answer as brief as possible so as it would be easy to remember. Answers here are taken from different Java books one being "The Complete Reference".

Fundamentals:

class: a template for object.
object: instance of a class.
constructor: initializes an object immediately upon creation.

Data Types:
Primitive: byte, short, int, long, char, float, double, and boolean
byte:- 8 bit
short:- 16 bit
int :- 32 bit
long:- 64 bit
float:- 32 bit
double:-64 bit

char:- 16 bit; 8 bit in C/C++; represents unicode.

final variable: value never changes
final method: can't be overridden
final class: can't be extended; all methods are implicitly final

access modifiers: public, private, protected
public: accessible by any other code.
private: is not accessible outside the class.
protected: seen outside the current package, but only to classes that subclass the class directly.
default: accessible in the same package.

static: any member declared static can be accessed before any object of its class are created, and without reference to any object. If a field or method is defined as static then there is only one copy for entire class rather than one copy for each instance. Static methods cannot access non-static field or call non-static method.

static variables: usually global variables; no copy is made
static methods: can only call other static methods; must only access static dta; cannot refer to this or super.

abstract class: cannot be instansitaed but can be extended.
abstract methods: with no method body, should be inside and abstract class.
Interface: abstract class with only abstract methods.

polymorphism: one interface, multiple methods
method overloading: parameter defines the method implementation.
method overriding: in inheritance; subclass overrides the superclass's method with same signature and parameter; name and type signature hast to be same or else they will be just overloaded.

super(): calls the constructor of the superclass

Advanced Java:

finalize() : free resources captured by any Java object.

Object Serialization: Writing an object and re-constructing the same object back in the exact form is known as object serialization. Object serialization is used in Remote Method Invocation (RMI)--communication between objects via sockets and for lightweight persistence--the archival of an object for use in a later invocation of the same program which is possible through JDBC. Serialization can be done with the helo of the interface Serializable.

Serialization: mechanism by which user can save the state of an object by converting it to a byte stream.

Connection Pool: Connection pool is about an application server storing the database connection information in order to reuse it again. If there is a connection pool every time if any request is made the system does not have to create a new connection. Connection pooling is popular in web based data driven application and Enterprise applications.

Map and HashMap :- Map is an interface whereas HashMap is a class that implements Map.
Vector and ArrayList :- Vector is synchronized and Arraylist is not. whenever multiple threads are supposed to access the same instance vectors should be used else Arraylist should be used. Arraylist gives better perfromance in non-synchronized case.

Swing and AWT:- Swing has light components whereas AWT has heavy-weight components.
Java supports passing by value.
wrapper classes: specialized classes corresponding to each of the primitive data types like Integer, Character Double.

checked exceptions: checked exceptions are those that the compiler force to catch like IOException.

J2EE

No comments: