Core Java Interview Questions
What do you mean by Transient Variable in Java ?Transient variable are those which can not be serialized. It is a volatile variable, we can have an example like if a variable is declared as transient in a Serializable class and the class is written to an output stream, the value of the variable can not be written to the stream instead when the class is retrieved from the stream the value of the transient variable becomes null.
Explain the difference between an Interface and an Abstract Class?An abstract classes are partially defined and can have instance methods that implement a default behavior, on the other hand an Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract.
An interface has all public members with no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract or undefined methods.Interfaces provide a form of multiple inheritance. A class can extend only one other class.
Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class. Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are faster.
There is a common feature of interface and abstract class that both can not be instantiated.
Define Abstract Class and InterfaceExample of Abstract class:
Example of Interface:
Explain Synchronization with code example?Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time. In non synchronized multithreaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object's value. Synchronization prevents such type of data corruption.
Example 1: Synchronizing any function:
Example 2: Synchronizing a particular block of code inside a function:
Why is synchronization important?Without synchronization, it is highly possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value, This often leads to critical errors. With respect to multithreading, synchronization is the capability to control the access of multiple threads to the shared resources.
What are the different ways of using thread?The thread can be implemented in two ways, first by implementing runnable interface and secondly by inheriting from the Thread class by extending it.
The former is more advantageous, because when you are going for multiple inheritance only interface can help, normal class does not support multiple inheritance.
What is Garbage Collection ?Garbage collection is one of the most important feature of Java.Garbage collection does not guarantee that a program will not run out of memory. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. It is possible for programs to use up memory resources faster than they are garbage collected. User program cann't directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program. It is also possible for programs to create objects that are not subject to garbage collection. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use. I Java on calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.An object is subject to garbage collection when it becomes unreachable to the program in which it is used.
Difference between a break statement and a continue statement?A
Describe the advantage of the event-delegation model v/s event-inheritance model?The event-delegation model has some advantages over the event-inheritance model.
Difference between a while statement and a do statement?
What is Encapsulation ?Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
What is Polymorphism ?Polymorphism means something like one name in many forms. Polymorphism enables one entity to be used as as general category for different types of actions. The specific action is determined by the exact nature of the situation. The concept of polymorphism can be explained as "one interface, multiple methods".
Polymorphism exists in three distinct forms in Java:
What are Wrapper classes in Java ?Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.
Following table lists the primitive types and the corresponding wrapper classes:
What is a package?Package is a collection of related classes and interfaces. package declaration should be first statement in a java class.
Explain final, finally and finalize()?
Java Fundamentals 1 >>>