Java Interview Question

Q: List any five features of Java?
A: Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded.

Q: What do you mean by Object?
A: Object is a runtime entity and it’s state is stored in fields and behavior is shown via methods. Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.

Q: What kind of variables a class can consist of?
A: A class consist of Local variable, instance variables and class variables.

Q: What is a Local Variable
A: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and it will be destroyed when the method has completed.

Q: What is a Instance Variable
A: Instance variables are variables within a class but outside any method. These
variables are instantiated when the class is loaded.

Q: What is a Class Variable
A: These are variables declared with in a class, outside any method, with the static keyword.

Q: What is Singleton class?
A: Singleton class control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes.

Q: Difference between throw and throws?
A: It includes:
  • Throw is used to trigger an exception where as throws is used in declaration of exception.
  • Without throws, Checked exception cannot be handled where as checked exception can be propagated with throws. 
Q: What is Serialization and deserialization?
A: Serialization is the process of writing the state of an object to a byte stream.
Deserialization is the process of restoring these objects.

Q: What are System, out and println and how they are inter-related.
A: Sun-Microsystem make a PrintStream class and define print, println method for accessing these define another library class which are System class. out is instance of PrintStream class and print, println are method of PrintStream class.

class PrintStream {
          public void print(){}
          public void println(){}

class System {

         public static PrintStream out;
Q: What’s the difference between an Abstract Class and Interface in Java?
A: The primary difference between an abstract class and interface is that an interface can only possess declaration of public static methods with no concrete implementation while an abstract class can have members with any access specifiers (public, private etc) with or without concrete implementation.

Another key difference in the use of abstract classes and interfaces is that a class which implements an interface must implement all the methods of the interface while a class which inherits from an abstract class doesn’t require implementation of all the methods of its super class.

A class can implement multiple interfaces but it can extend only one abstract class.
Q: What are the two ways of implementing multi-threading in Java?
A: Multi threaded applications can be developed in Java by using any of the following two methodologies:
1. By using Java.Lang.Runnable Interface. Classes implement this interface to enable multi threading. There is a Run() method in this interface which is implemented.
2. By writing a class that extend Java.Lang.Thread class.
Q: How garbage collection is done in Java?
A: In java, when an object is not referenced any more, garbage collection takes place and the object is destroyed automatically. For automatic garbage collection java calls either System.gc() method or Runtime.gc() method.