1: What is object in java?

  • Object Is an Instance of class and it has its own state and behavior.
  • In real world we can say, Dog is object of Animal class which have different state like breed, color, name, hungry, etc and behavior  like wagging tail, fetching, barking etc.


2 : What is class?

  • A class is the blueprint or we can say template from which individual objects are created.


3 : What is constructor?

  • Constructor is a code block just like a method which is used to initialize the state of an object.
  • It will be invoked at the time of object creation to construct the value for object.


4 : What is default constructor?

  • If there is not any constructor in class then java compiler creates default constructor.
  • It is no argument constructor which initializes any uninitialized fields to their default values.


5 : Constructor returns any value?

  • Yes. It will return instance of current class (However we can not use return type with constructor).


6 : What is the difference between static and not static variable?

  • Static variables are preceded by static keyword. For non-static variable, there is not any preceding keyword.
  • Memory is allocated for static variables at the time of class loading. Memory is allocated to non- static variables whenever an object is created.
  • Memory is allocated only once to static variables on class loading. Memory is allocated multiple time whenever a new object is created to non-static variables.
  • Static variable example : Collage name of students, Company name of employees..


7 : What is the difference between static and not static(Instance) method?

  • Method declared with static keyword is static method. If Method declared without static keyword then it is instance method.
  • No need of object to call static methods. Object needed to call instance method.
  • Can not access non static stuff inside static methods directly. Opposite to it, We can access static and non static stuff directly inside instance method.


8 : What is inheritance in java?

  • In Java, Inheritance provides mechanism using which one object of child class can acquire  all the properties and behaviors of parent object.
  • It will crate IS-A relationship.
  • Main usage of inheritance in java is for code re-usability and  method overriding to achieve run-time polymorphism.


9 : Multiple inheritance is supported in java on class level? If No.. Why?

  • No.. Multiple inheritance is not supported in java in case of class to simplify the language and reduce the complexity.


10 : What is method overriding in java?

  • Method overriding is a feature which allows a child class or sub class to provide a specific implementation of a method which is already provided by one of its parent classes or super class.
  • It is used for runtime polymorphism.


11 : Why main method is static?

  • As we know, We can access static stuff without creating object of class. Because of static keyword with main method, Java virtual machine can directly call it without creating object of class.
  • This way it will provide kind of root to start execution of program.


12 : What is method overloading?

  • Method overloading is ability to create multiple methods with same in same class but with different signatures (different input parameters and types).
  • Method names will be same but parameters will be different for all overloaded methods.


13 : What is constructor overloading?

  • Same as method overloading, Single class can have multiple constructors with same name as class name but all have different signatures (different input parameters and types) is called constructor overloading.


14 : Can we override static methods?

  • We can declare static method with same signature in subclass but it will not behave as overridden method. So answer is No.
  • We can not override static methods as they are part of  class not object.
  • You can override static methods but output will be different than the expected.


15 : How to reverse string in java?

  • StringBuffer class has a method called reverse(). We can use it to reverse the string.

Example :


16 : Can we overload static methods?

  • Yes.. There is not any restriction to overload static methods.
  • We can overload static and non static methods in java.


17 : Can we use private member of parent class in sub class?

  • No.. It will not allow to use private members like private method, variable of parent class in child class.
  • Private members are accessible only inside same class.


18 : What is an interface in java?

  • An interface is a blue print of a class which can hold abstract methods (Methods without implementation) only.
  • It creates Rules To Follow structure for class where It Is Implemented. We can achieve 100% abstraction using interface in java.


19 : Can we access protected method of parent class in sub class?

  • Yes.. We can access protected members of parent class in all it’s sub classes and classes within the same package.


20 : What is an array in java?

  • An array is container object in java which can hold fixed number of values of same type.


21 : Explain System.out.println();

  • System : is a final class in  java.lang package.
  • out : is a static member of system class. It is an instance of This stream is already open and ready to accept output data.
  • println : is a method of .It is an overloaded method.


22 : Write program to print fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21,…


23 : Write program to get result of 52+42-32+22-12


24 : What is return type of testng @DataProvider annotation method?

  • It will return double object array “Object[][]”.


25 : int x=10 and y=20. Swap both variable values without using any temp variable.


26 : What is local variable in java?

  • Local variable is declared inside method or constructor and it is limited for that method or constructor only.


Local Variable Example :


27 : What is a Instance Variable in java?

  • Instance Variable is declared parallel to method or constructor in class. It is visible for all methods and constructors of that class.


Instance Variable Example :


28 : What is a Class Variable in java?

  • Class variable is declared with static keyword in class parallel to methods and constructor.
  • Class variable is initialized only once at the start of execution and destroyed on end of program.
  • Class variable is also known as static variable.


Class Variable Example :


29 : What is the difference between instance variable and class variable?

Instance Variable :

  • It is unique to each instance of the class.
  • Declared without static modifier.
  • Memory allocation, loading and initialization is done at run time.

Static Variable :

  • It is shared by all instances of the class.
  • Declared with static modifier.
  • Memory allocation is done at compile time, loaded at load time and they are initialized at class initialization time.


30 : What is access modifier in java?

  • Access modifiers allows us to set access levels for variables, methods, classes and constructors in java.
  • We can control access levels using access modifiers in java.


31 : What is default value of local variable.

  • There is not any default value of local variable. You must have to initialize it.


32 : Java support constructor inheritance?

  • No, Constructor inheritance is not supported in java.


33 : Which is super class of all other classes in java?

  • java.lang.Object is  super class of all other classes in java.


34 : What is Encapsulation?

  • Encapsulation is process of packing code and data together In to a single Unit.


35 : Write a program to reverse a string without using reverse function.

Output : .


36 : What is the difference between the Constructor and Method?

Constructor :

  • Name of the constructor must be same as class name.
  • Constructor must not have any return type.
  • It is used to initialize the state of an object.
  • It is not possible to call constructor directly. Constructors called implicitly when the new keyword creates an object.

Method :

  • Method name can be any.
  • Method must have return type.
  • It is used to expose behavior of an object.
  • Methods can be called directly.
  • Read more on Constructor and Method.


37 : What are the different types of types of constructors in java?

Default Constructor : Constructor without parameter is called default constructor.

Parameterized constructor : Constructor with parameter is called Parameterized constructor.

Output :


38 : Write a program for Fibonacci series in Java ?


39 : Write a program to print below given pattern.



1 2

1 2 3

1 2 3 4

1 2 3 4 5


Program to print above pattern is as bellow.


40 : What is the difference between “this” and “super” keywords in Java?

  • “this” keyword is used to store current object reference while “super” keyword is used to store super class object in sub class..
  • “this” is used to access methods of the current class while “super” is used to access methods of the base class.
  • this() used to call constructors in the same class whereas super() is used to call super class constructor.


41 : In java, What is return type of main method?

  • Main method doesn’t have any return type. It is void.


42 : Can We Overload main method in java?

  • Yes, Java class can have any number of main methods so it is possible to overload main method.
  • But when you run program, It will not execute overloaded main method.
  • Always It will execute only public static void main(String[] args) method.


43 : Can we declare class as protected?

  • No, You can not declare class as protected.


44 : Write a program to remove given character from string.


45 : How to convert string from upper to lower and lower to upper case?

  • You can use toUpperCase and toLowerCase methods to convert string from lower to upper and upper to lower case.


46 : What is Polymorphism?

  • Polymorphism is ability using which we can create reference variables or methods which behaves differently in different programmatic context.
  • Best example of polymorphism is human.
  • We behaves differently with different people in different environment.
  • Our behavior will be different when we meet to boss and meet to friend.


47 : What is the advantages of Polymorphism?

  • Main advantage of polymorphism is code reusabilty.
  • You can dynamically supply different implementations through polymorphism.
  • So it will reduce your work volume in terms of handling and distinguishing various objects.


48 : What is a package?

  • A package is a namespace which allows developer to organizes a group of related classes and interfaces.
  • Conceptually it is just like folder which contains different types of files.
  • It is easy to keep things organised by keeping related classes and interfaces into packages.


49 : What is string in java?

  • In Java programming, String is object which is prepared by sequence of package has String class to create and manipulate strings.


50 : What is StringBuffer in java?

  • StringBuffer help us to create mutable(modifiable) string in java.
  • That means we can modify the string if we use StringBuffer.