Important Core Java Interview Questions and Answers

Core Java Interview Questions and Answers

This is a Collection of very important core java interview questions with answers.

These questions will help students and job seekers to gain confidence to appear and get success in java interviews.

Lot of importance is given to a candidate’s ability to answer core java questions in a java job interview.

For private Online Core Java Training with java interview preparation you may contact me.

Core Java Interview Questions and Answers

1. What is a class?

A class is a user defined datatype. It is a frame of all the objects that can be created using it. A class can contain data properties and functional properties.


2. What is an object?

An object is an instance of a class. Each object will contain its own copies of the instance variables and the functions defined in a class. To create an object we use the new keyword.

  1. Soldier s1=new Soldier();

new Soldier() will create an object and make s1 is a reference variable of Soldier type that refers to the Soldier object created.

3. What is difference between instance variable and reference variable?

The data properties in a class are declared with variables. These variables are called instance variables. Example in the Soldier class we have declared two data properties the x coordinate and the y coordinate representing the location of  a soldier. These data properties are called instance variables.

Each object of the Soldier class will have its own copies of the instance variable. e.g the first soldier object will have its own x and y and the second soldier object will have its own copy of x and y.

Reference variable is the variable that can refer to objects. Eg Soldier s1 is a Soldier type reference variable that can refer to Soldier type of objects.

4. Explain garbage collection in java.

When an object in the memory doesn’t have any reference variable pointing to it, java considers it a useless object, unnecessarily occupying memory. Java periodically searches for such type of hanging objects and removes them from memory. This procedure is called automatic garbage collection. We can tell the jvm to start the garbage collection by calling the function System.gc()

5. What is function overloading?

Function overloading is one type of polymorphism in which the function name is same but the parameter list is different. The number or nature of the parameters may be different.

6. What is a Constructor and its use?

A Constructor is just like a function with the same name as the class name and no return type, not even void. Constructors are generally used to initialize instance variables during object creation.

7. What is this keyword in java?

The keyword “this” refers to the current class object.

8. What is super keyword in java?

The “super” keyword in java refers to the parent class object.

9. What is static keyword in java?

The keyword static can be put in front of a variable , function or a block in java. To access a static variable or a static function we don’t need to create an object. We cann access a static variable or a function directly through the class name e.g classname.function() or classname.variable.
A static variable is shared among all objects of the class.

10. What is the significance of the main function being static?

The function main is the starting point of program execution. There is no need for the jvm to create an object of the class to access the main function. So the main function is made static.

11. What is the use of inheritance in programming?

The main benefit of inheritance is code reusability. The properties of the parent class are available in the child classes. The child class inherit from the parent class using the extends keyword.

12. Does java support multiple inheritance?

Java doesn’t support multiple inheritance through classes but supports through interfaces. Class A extends B,C is not possible but class A implements X,Y is possible provided X and Y are interfaces.

13. What is difference between Path and CLASSPATH?

Path is an environment variable that guides the operating system from where it will search for executable files.

CLASSPATH is an environment variable that helps to educate the JVM from where it will start searching for .class files. generally we store the root of the package heirarchy in the CLASSPATH environment variable.

We also store the location of external jar files in the environment variable, so that the jvm can locate the class files within it.

14. What is function overriding?

Same function name and same parameter list, and can happen during inheritance only.

15. Explain run time polymorphysm.

Java resolves dynamically at run time, which function to call, based on which sub class is referred by the super class reference variable just before the function is called.
Figure f=new Rectangle(); f.area();
In the above code the area of the rectangle class get called because just before the function is called, the reference variable is pointing to a Rectangle object.

16. What is an abstract class?

Any class can be made abstract by just putting the abstract keyword in front of the class. But if a class contains one or more abstract functions, we are bound to make the class abstract.

We cannot create an object of an abstract class.

Abstract classes are generally used to create the frame of the project, or the top level class.

Subclass of an abstract class has to provide implementation of all the abstract functions present in the super class, else the subclass will also become abstract.

We can also implement run time polymorphism through abstract classes.

17. What is Object class?

Object class is an inbuilt class present in the java.lang package. It is the super class of all classes in java.

Whenever we create a class, by default it is the subclass of the object class. We can override non final functions of the Object class in any class eg toString(), equals() etc.

18. What is the use of toString function?

The toString function gets automatically inherited in all java classes from the Object class. We can override the toString function. When ever we print an object of a class, by default the toString function of the class gets called. So if we want to give a customizable output when printing an object, we can override the toString function.

19. Explain about different access specifiers.

The different access specifiers are public, private, protected and the default.

private properties are accessible from only within the same class.

public properties are accessible from anywhere.

no modifier properties are accessible only from within the same package.

protect properties are accessiable from the same package, but out side the package it is accessiable provided it is done from a sub class.

20. What are the differences between an abstract class and an interface?

An abstract class can contain both function definitions and function declarations, but an interface can contain only function declarations.

An abstract class is extended by using the extends keyword while an interface is implemented using the implements keyword.

Multiple inheritance is not supported through classes or abstract classes but is supported through interfaces.

21. What is final keyword in java?

The final keyword in java can be used infront of a variable, function or a class.

If a final keyword is placed infront of a variable, it acts like a constant. It cannot be modified.

Eg final int YES=100;

By convention final variables are in upper case. They are declared and initilized in the same line.

Function defined as final cannot be overridden in the subclasses.

Classes defined as final cannot be subclassed or inherited.

22. What are wrapper classes, and their use?

Java provides special classes corresponding to each of the primitive data types. These are called wrapper classes. They are e.g. Integer, Character, Double etc.

Most of the collection classes store objects and not primitive data types. Wrapper classes also provide many utility methods. And since we create instances of these classes we can store them in any of the collection classes and pass them as a collection. Also we can pass them as method arguments where a method expects an object.

23. What is an exception in java?

Exceptions are run time errors. e.g int i=1/0; This code doesn’t show any error during compile time, but when we run the code we get an ArithmeticException.

24. What is exception handling?

When we run a program that has error, java creates an appropriate exception type of object and throws it. Then the default exception handler of java handles the exception. But the default exception handler stops the program execution abruptly and gives a non understandable output.
So we are not satisfied by the default exception handler’s exception handling. So we want to handle the exception on our own so that the program will not stop abruptly and also give a non understandable output. We can do exception handling on our own using try and catch block.

25. What is the use of printStackTrace() method?

It helps to trace the exception step by step. It helps to find how the exception occured step by step.

26. Explain custom exception class.

We need a custom exception class to represent a custom problem. There are many inbuilt exception classes in java like ArithmeticException that represents arithmetic problems, ArrayIndexOutOfBounds exception to represent array related problems. But we need custom exception classes to represent custom problems, or our own problems. e.g ProductNotFoundException is a custom exception class representing a problem which occurs if a product is not found during the search. To create a custom exception class we have to extend the inbuilt Exception class.

Please check again we are updating this with more Core Java interview questions