Java Basics - Interview Questions and Answers

What is a class?

A class is a blue print from which individual objects are created.
A sample of a class is given below:
public class Dog{
      String breed;
      int age;
      String color;
      void barking(){ }
      void hungry(){ }
      void sleeping(){ }
}

A class can contain any of the following variable types.
  • Local variables: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
  • Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
  • Class variables: Class variables are variables declared with in a class, outside any method, with the static keyword.
A class can have any number of methods to access the value of various kinds of methods. In the above example, barking(), hungry() and sleeping() are methods.

Explain Polymorphism?

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. For Detailed tutorials on polymorphism please visit Polymorphism In Java.

Explain Method signatures.

The signature of a method is the combination of the method’s name along with the number and types of the parameters (and their order).

Why we use static before public static void main?

A static method is one that's associated with a class, but can be called without creating an instance of that class. Most methods (i.e., non-static ones) require an object to be called, but static ones do not.

The real answer is, of course, that this is just how they chose to do it. A non-static main() would have worked just as well; the JVM could create an instance of main()'s class to call it if it needed to. But since the JVM is designed to expect a static method, that's what you must use.

Can we have more than one public class in .java file ?

No, we can have only one public class in a .java file. It's just for conventions. Think if you have declared a class as public that means that you want to access it from anywhere else also, now if you declare that class in some other .java file. How does compiler find that, keeping into this mind these conventions are derived.

What is enum in java ?

Enumerations (in general) are generally a set of related constants. They have been in other programming languages like C++ from beginning. After JDK 1.4, java designers decided to support it in java also, and it was officially released in JDK 1.5 release.

Enumeration in java is supported by keyword enum. enums are a special type of class that always extends java.lang.Enum.

A simple usage will look like this
public enum DIRECTION { EAST, WEST, NORTH, SOUTH //optionally can end with ";" } 


Here EAST, WEST, NORTH and SOUTH are final static inner classes of Direction of type Direction extends java.lang.Enum.

Difference between String Buffer and String Builder

Well, the most important difference between String and StringBuffer/StringBuilder in java is that String object is immutable whereas StringBuffer/StringBuilder objects are mutable.

By immutable, we mean that the value stored in the String object cannot be changed. Then the next question that comes to our mind is “If String is immutable then how am I able to change the contents of the object whenever I wish to?” . Well, to be precise it’s not the same String object that reflects the changes you do. Internally a new String object is created to do the changes.

How to make a method thread safe in java ?

Either we can use synchronized keyword or we can use methods of java.util.concurrent.atomic, For detailed tutorials on Thread safety visit How to make a method thread-safe in Java.

Explain serialization in java ?

Serialization is the process of making the object’s state is persistent. That means the state of the object is converted into stream of bytes and stored in a file. In the same way we can use the de-serilization concept to bring back the object’s state from bytes. This is one of the important concept in Java programming because this serialization is mostly used in the networking programming. The object’s which are needs to be transmitted through network has to be converted as bytes, for that purpose every class or interface must implement serialization interface. It is a marker interface without any methods.

What is transient in java ?

The keyword transient in Java used to indicate that the variable should not be serialized. By default all the variables in the object is converted to persistent state. In some cases, you may want to avoid persisting some variables because you don’t have the necessity to transfer across the network. So, you can declare those variables as transient. If the variable is declared as transient, then it will not be persisted. It is the main purpose of the transient keyword.

Java Basics - Interview Questions and Answers