Wednesday, May 25, 2011

Core Java Questions Part-1

Why Java is not 100% pure object oriented language?
Answer
: Because java uses primitives.

What is the difference between static and non static variables ?
Answer :
 A static variable is associated with the class as a whole rather than with specific instances of a class. There will be only one value for static variable for all instances of that class. Non-static variables take on unique values with each object instance

What is static initializer block? What is its use?
Answer :
 A static initializer block is a block of code that declares with the static keyword. It normally contains the block of code that must execute at the time of class loading. The static initializer block will execute only once at the time of loading the class only

What are the methods in Object?
Answer :
 clone, equals, wait, finalize, getClass, hashCode, notify, notifyAll, toString

What is daemon thread?
Answer :
 Theards which are running on the background are called deamon threads. daemon thread is a thread which doesn't give any chance to run other threads once it enters into the run state it doesn't give any chance to run other threads. Normally it will run forever, but when all other non-daemon threads are dead, daemon thread will be killed by JVM

What is Transient variables ?
Answer :
 Transient variables are variable that cannot be serialized.

What is synchronization
Answer :
 Synchronization is the ability to control the access of multiple threads to shared resources. With synchronization , at a time only one thread will be able to access a shared resource.

What are the differences between an abstract class and an interface?
Answer :
 An abstract class can have concrete method, which is not allowed in an interface. Abstract class can have private or protected methods and variables and only public methods and variables are allowed in interface. We can implement more than one interface , but we can extend only one abstract class. Interfaces provides loose coupling where as abstract class provides tight coupling.

What are different type of exceptions in Java?
Answer :
 There are two types of exceptions in java. Checked exceptions and Unchecked exceptions. Any exception that is is derived from Throwable and Exception is called checked exception except RuntimeException and its sub classes. The compiler will check whether the exception is caught or not at compile time. We need to catch the checked exception or declare in the throws clause. Any exception that is derived from Error and RuntimeException is called unchecked exception. We don't need to explicitly catch a unchecked exception.

What is an abstract class and Abstract Method?
Answer :
 An abstract class is an incomplete class. It is declared with the modifier abstract. We cannot create objects of the abstract class. It is used to specify a common behavioral protocol for all its child classes.
An abstract method is a method that don't have a body. It is declared with modifier abstract.

What is an interface?
Answer :
 An interface is a collection of method declarations and constants. In java interfaces are used to achieve multiple inheritance. It sets a behavioral protocol to all implementing classes.

What is finalize() ?
Answer :
 Finalize is a protected method in java. When the garbage collector is executes , it will first call finalize( ), and on the next garbage-collection it reclaim the objects memory. So finalize( ), gives you the chance to perform some cleanup operation at the time of garbage collection.

What is final ?
Answer :
 A final is a keyword in java. If final keyword is applied to a variable, then the variable will become a constant. If it applied to method, sub classes cannot override the method. If final keyword is applied to a class we cannot extend from that class.

What is static ?
Answer :
 static means one per class. static variables are created when the class loads. They are associated with the object. In order to access a static we don't need objects. We can directly access static methods and variable by calling classname.variablename.



How we remove white spaces from a string ?
Answer :- Use trim() method to remove a white spaces.
              String str = "  Satya";
              str.trim();