Core Java

Java Fundamentals 1

Java Fundamentals 2

Java Fundamentals 3

Java Swing

Java Applet

Java Memory & Performance

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 Interface

Example of Abstract class:
abstract class testExampleAbstractClass {
    protected String exampleString;
    public String getExampleString() {
      return exampleString;
    public abstract string testAbstractFunction();

Example of Interface:
public interface sampleTestInterface {
    public void functionTestOne();
    public long TEST_CONSTANT = 1000;

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:
public synchronized void exampleMethod () {
    // usual code.

Example 2: Synchronizing a particular block of code inside a function:
public exampleFunction (){
    synchronized (this) {
      // usual code here.

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 break statement results in the termination of the statement to which it applies like switch, for, do, or while. A continue statement is used to end the current loop iteration and return control to the loop statement.

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.
  • First, it enables event handling to be handled by objects other than the ones that generate the events (or their containers). This allows a clean separation between a component's design and its use.
  • The other advantage of the event-delegation model is that it performs much better in applications where many events are generated. This performance improvement is due to the fact that the event-delegation model does not have to repeatedly process unhandled events, as is the case of the event-inheritance model.

Difference between a while statement and a do statement?

  • A while statement checks at the beginning of a loop to see whether the next loop iteration should occur.If condition fails loop does not iterate at all.
  • A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.

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:
  • Method overloading
  • Method overriding through inheritance
  • Method overriding through the Java interface

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:
Primitive Wrapper
boolean java.lang.Boolean
byte java.lang.Byte
char java.lang.Character
double java.lang.Double
float java.lang.Float
int java.lang.Integer
long java.lang.Long
short java.lang.Short
void java.lang.Void

What is a package?

Package is a collection of related classes and interfaces. package declaration should be first statement in a java package is imported by default even without a package declaration.

Explain final, finally and finalize()?

  • final is a modifier which can be applied to a class or a method or a variable. final class can't be inherited, final method can't be overridden and final variable can't be changed.
  • finally is an exception handling code section which gets executed whether an exception is raised or not by the try catch block code segment.
  • finalize() is a method of Object class which will be executed by the JVM just before garbage collecting object to give a final chance for resource releasing activity.

Java Fundamentals 1 >>>

Home Clouds