Java Interview Questions - Part I

1) What is meant by Object Oriented Programming?

OOP is a method of programming in which programs are organized as cooperative collections of objects. Each object is an instance of a class and each class belong to a hierarchy.

2) What is a Class?

Class is a template for a set of objects that share a common structure and a common behavior.

3) What is an Object?

Object is an instance of a class. It has state, behaviour and identity. It is also called as an instance of a class.

4) What is an Instance?

An instance has state, behaviour and identity. The structure and behaviour of similar classes are defined in their common class. An instance is also called as an object.

5) What are the core OOP's concepts?

Abstraction, Encapsulation, Inheritance and Polymorphism are the core OOP's concepts

6) What is meant by abstraction?

Abstraction is the process of hiding certain details and showing only essential information to the user.

7) What is meant by Encapsulation?

Encapsulation in Java refers to integrating data (variables) and code (methods) into a single unit.

8) What are Encapsulation, Inheritance and Polymorphism?

Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse. Inheritance is the process by which one object acquires the properties of another object. Polymorphism is the feature that allows one interface to be used for general class actions.

9) What are methods and how are they defined?

Methods are functions that operate on instances of classes in which they are defined.Objects can communicate with each other using methods and can call methods in other classes. Method definition has four parts.

10) What are different types of access modifiers (Access specifiers)?

Access specifiers are keywords that determine the type of access to the member of a class. These keywords are for allowing privileges to parts of a program such as functions and variables. These are:

Public: Any thing declared as public can be accessed from anywhere.

Private: Any thing declared as private can't be seen outside of its class.

Protected: Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages.

Default modifier: Can be accessed only to classes in the same package.

11) What is an Object and how do you allocate memory to it?

Object is an instance of a class and it is a software unit that combines a structured set of data with a set of operations for inspecting and manipulating that data. When an object is created using new operator, memory is allocated to it.

12) Explain the usage of Java packages.

This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows protecting data from being used by the non-authorized classes.

13) What is method overloading and method overriding?

Method overloading: When a method in a class having the same method name with different arguments is said to be method overloading. Method overriding: When a method in a class having the same method name with same arguments is said to be method overriding.

14) What gives java it's "write once and run anywhere" nature?

All Java programs are compiled into class files that contain bytecodes. These byte codes can be run in any platform and hence java is said to be platform independent.

15) What is a constructor? What is a destructor?

Constructor is a function that is executed when an object is created for the class.The constructor is used to initialize any task. Destructor is an operation that frees the state of an object and/or destroys the object itself. In Java, there is no concept of destructors. Its taken care by the JVM.

16) What is the difference between constructor and method?

Constructor will be automatically invoked when an object is created whereas method has to be called explicitly

17) What is a Static member class?

A static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.

18) In Java, How to make an object completely encapsulated?

All the instance variables should be declared as private and public getter and setter methods should be provided for accessing the instance variables.

19) What is static variable and static method?

Static variable is a class variable which value remains constant for the entire class. Static method is the one which can be called with the class itself and can hold only the static variables.

20) What is finalize() method?

The finalize() method is used just before an object is destroyed and can be called just prior to garbage collection.

21) What is the difference between StringBuffer and String?

String is an Immutable class, i.e. you can not modify its content once created. While StringBuffer is a mutable class, means you can change its content later. Whenever we alter content of String object, it creates a new string and refer to that,it does not modify the existing one. This is the reason that the performance with StringBuffer is better than with String.

22) What is the difference between Array and Vector?

Array is a set of related data type and static whereas vector is a growable array of objects and dynamic.

23) What is a package?

A package is a collection of classes and interfaces that provides a high-level layer of access protection and name space management.

24) What is the difference between this() and super()?

The this() keyword can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor.

25) Explain working of Java Virtual Machine (JVM)?

JVM is an abstract computing machine like any other real computing machine which first converts .java file into .class file by using Compiler (.class is nothing but byte code file.) and Interpreter reads byte codes.

Java Interview Question - Part II

Java Interview Question - Part III