Important Interview Question Of the Java Class.

In this Java Interview Questions blog, I am going to list some of the most Important Interview Question of the Java Class which is going to help you out in the interview. You can expect many questions from the interviewer in the interview with the java class members related topic.

Q1. What do you understand by the member of the class in java language?
Ans:
Every single thing which is in the scope of the class is called class members, not only in Java but this can also be applied in other programming languages.

Q2. What are the members of the class in Java?
Ans:
A class can contain a different type of members

    • Variable
    • Blocks
    • Methods
    • Constructor
    • Inner Class
    • Enum

Q3. What are the variables in Java and how many types of a variable?
Ans:
Variables will be of some specific data type. For that variable, we can assign some values in it which can be changed later on according to the requirement.
Int a = 10; here Int is a data type, a is variable, which value can be changed. There are two types of variables: 1) Instance variable 2) Static variable

Q4. What is an instance and static variable?
Ans: Instance variable:
The variable which is defined directly inside a class, without having any static keyword is called as an instance variable. For example:

Class Hello
 {
 int a = 10;
 // here, a and b are instant variables 
int b = 20; 
}

Static variable: The variable which is defined directly inside a class with a static keyword is called as a static variable. For example:

Class Hello 
{
 static int a = 10;
 // here, a and b are static variables
 static int b = 20; 
}

Q5. Difference between Instance and static variable?
Ans:

Instance variable

Static variable

Define directly inside the class, without having static keyword Define directly inside the class, with the static keyword
The memory of the instance variable will be allocated at the time of object creation of its corresponding class The memory of the static variable will be allocated at the time of loading of its corresponding class
The memory of the instance variable will be allocated N number of times, as many times the object of its the corresponding class will be created. The memory of the static variable will be allocated only once, at the time loading of its corresponding class.
Instance variable will be processed, at time of object creation of its class Static variable get a memory, or class can be load by the following three different ways

a) when we are accessing any static member of a class from outside the class, directly by the name of it class

b) when we are accessing any static member of a class with the help of a reference variable which contains null.

c) whenever we are creating an object.

Hello h1 = new Hello();

It is called a non-static variable It is called a class label variable
It can only be accessed from inside the instance context directly. It can be accessed from anywhere, inside the class directly

Q6. What happens when an object is created in class?
Ans: Whenever you create an object of a class, before creating an object of a class, JVM will check whether a class is loaded on the main memory or not. If a class is not being loaded into main memory then firstly, JVM will load a class, at the time of loading of a class, the following things will be done internally by JVM:

  • Allocates memory for a static variable of a class.
  • Process the static block of a class.

If a class is already being loaded into the main memory then, JVM will not load it again. Now, at the time of the creation of an object firstly,

  • it will allocate memory to an instance variable.
  • instance blocks will be processed.
  • corresponding object reference will be assigned to the reference variable.

Note: A class can be loaded once into the main memory but the object of a class can be created to n no of times.

Q7. What do you understand by block and its type?
Ans: A pair of curly braces defined directly inside a class is called as blocks
There is mainly two types of block:
a) Instance block
b) static block

Q8. What is Instance block?
Ans: A block which is directly defined inside a class without any static keyword is called as instance block.

class Hello
       { 
        {       
         // instance block
        }
      }

Q9. What is a static block?
Ans:
A block which is directly defined inside a class with static keyword is called a static block.
For example:

class Hello
    { 
      static  {       
               // static block
               }
             }

Q10. Difference between instance and static block?
Ans:

Instance block

Static block

Pair of curly braces, define directly inside a class without any static keyword is called as instance block Pair of curly braces, define directly inside class with static keyword is called a static block
It will be processed, when we are creating an object of a class It will be processed at the time of loading of the

class

Inside instance block, we can access both the members of the class i.e, static and instance members. Inside a static block, we can access only static members of a class
Variable declared inside the instance block will be equivalent to the instance variable Variable declared inside the static block will be equivalent to the static variable.

Q11. What is the method and what is its types?
Ans: Method in Java is a collection of the code, which is used to do some specific task. A method is having some specific name, which can be declared directly inside the class. It must have some return type and according to its return type, it must have a corresponding compatible return statement. It may or may not have parameters. A method can’t be defined inside any local context or method cannot be declared in any other method.
There are two types of method:

1) Instance method
2) Static method

Q12. What do you understand my Instance method and Static method?
Ans: Instance method: The method which is directly defined inside a class without any static keyword.

For example:

Class Hello
{ 
   void m1()
{
// instance method
}
}

Static method: The method which is directly defined inside a class with the static keyword.
For example:

Class Hello
{ 
  static void m1()
{
// static method
}
}

Q13. Difference between the Static and Instance method in Java?
Ans:

Instance method Static method
The method defines without static keyword and defines directly inside a class The method defines with static keyword and defines directly inside the class
An instance method can be called by the reference variable, which contains corresponding class object reference.

Hello h1= new Hello(); h1.method();

A static method can be called in the following ways :

a) Hello.method(); a method called directly by the name of a class

b) Hello h1 = null;
h1.method();
a method called with a reference variable which contains null.

c) Hello h1 = new Hello();
h1.method();
a method is called with the reference variable, which contains a class object reference

Inside an instance method, we can access instance and static both the members of a class Inside a static method, we can only access static member of a class directly.

Q14 What will be the output of the below piece of code
Ans:

Class xadmin
  {
   static int x
    static
    {
        System.out.println(10);
 
        i =50;
    }
}
 
  public class XadminStaticInitializationBlock
{
   static
  {
        System.out.println(20);
    }
 
    public static void main(String[] args)
    {
        System.out.println(30);
 
        System.out.println(A.i);
    }
}

Output:
20
30
10
50

Q15. What will be the OutPut of the below code after compiling the code?

Class xadmin
  {
     int i;
     static 
                {
         System.out.println(i);
       }
  }

Ans: compile time error. Because you can’t refer a non-static field inside a static initialization block.

Q16. Is the below code written correctly?

class xadmin
{    
  static
    { 
  static
        {
            System.out.println(100);
        }
    }
}

Ans: No, as we cannot nest the static block

Conclusion:
This article will help you with the Java class top basic interview question and answer for the freshers and experienced candidates. Read the article carefully, I believe this article is going to help a lot to the fresher as well as experience candidate to tackle the question fired by the interviewer. Stay tuned!  for the more interview question. Happy Learning! All the best! 

 

Recommended interview questions: