Basic Core Java Interview Question

  1. Write a program which print hello world? Explain in details.
  2. What is class in java? How we create object in java?
  3. What is variable in java?Explain types of variable?
  4. What is methods in java?
  5. What is a constructor in java? How many types of constructor?
  6. what is the use of constructor? When it will get called?
  7. Can we make the constructor private? 
  8. What are the static block and instance block in java? What are the uses of them?
  9. What is this and super in java? What is the use of these?
  10. Constructor chaining in java with example.
  11. Static variable, method, and block in java.
  12. instance variable in java.
  13. varargs in java.
  14. Enum in java.
  15. final variable, final method and final class.
  16. Difference between JDK, JRE, JVM and JIT in java.

 

Write a program which print hello world? Explain in details.

Explanation –

  1. we have defined a class HelloWorld.
  2. public – access modifier or  keyword
  3. static – keyword
  4. void – return type(returning nothing)
  5. main – method name called by JVM.
  6. String – predefined java class.
  7. args [] – String array , reference variable
  8. System – Predefined final class available in lang package.
  9. out – static reference variable in System class, type of PrintStream class.
  10. println (String args[]) – is a method defined in PrintStream class.

What is class in java? How we create object in java?

Class is a programming code template which is used to create object. A class can contains –

  1. variables
  2. constructors
  3. methods
  4. blocks

 

What is variable in java?

variable  is a memory location which is used to store some value. Variable must have some specific type. It can be primitive type or class type.

  1. instance variable
  2. local variable
  3. static variable

Instance variable – 

  1. Instance variable defined inside class and outside of method.
  2. Scope of instance variable inside a class(only if you define instance variable as private for more details about instance variable read this post).
  3. memory allocation for instance variable will  happen at object creation.

We can access instance variable with private access modifier  inside class only.

Local variable – 

  1. Local variable defined inside method,constructor or block.
  2. Scope of local variable only inside method,constructor or block.
  3. we can define local variable as final only.

Example of local variable  –

Static variable – 

  1. We define static variable with static keyword inside class.
  2. Memory location for static variable will happen at class loading.
  3. Scope of static variable is for whole application.

 

What is methods in java?

  1. Method is set of statements which used to perform different task.
  2. we can call method with object/this/super/class name.

 

What is constructor in java? How many types of constructor?

  1. Constructor is a special type of program instructions that performs a specific task.
  2. Constructor name must be same as class name.
  3. Constructor doesn’t have return type.
  4. We can’t declare constructor as static,final or abstract.

There are two types of constructor –

1. Default constructor
2. Parameterized Constructor

Default constructor Example – 

Output of above program is – default constructor

 

Parameterized Constructor  Example – 

Output is – parametrized constructor value of name is ram

What is use of constructor? When it will get called?

  1. Constructor is used to create object with new keyword.
  2. Constructor is also used to initialize the variables with some values.
  3. Constructor will invoked at time of object creation.

Output is – constructor invoked

Let’s see example of initialization of  variables with some values using constructor-

 

 Note – In real time project we prefer setter method than constructor for variable initialization because it is easy with setter method, if we want to assign some new value in attribute/field later. But again it’s totally depended up to your design and requirement. 

Q. Can we make the constructor private?

Yes we can make constructor private. But we can create object inside that class only.

 

 

Top