Core Java Interview Questions

Showing 1 - 10 of 118 results

Are inheritance and overriding concept applicable for the constructor in Java?

In java, inheritance and the overriding concept are not applicable for constructors. In other words, if we inherit a class then the constructor of the parent class is not available in child class.

Constructor Overriding is never possible in Java. This is because, Constructor looks like a method but name should be as class name and no return value. Overriding means what we have declared in Super class, that exactly we have to declare in Sub class it is called Overriding.

How many ways you can create an Object in Java?

There are the five ways to create the object or get the object in java:

1. By using the new operator

Test t = new Test();

2.  By using the newInstance() Method

Test t = (Test)class.forName("Test").newInstance();

3. By using the factory Method

Runtime r = Runtime.getRuntime();
DateFormat d = Dateformat.getInstance();

4. By using the clone() Method

Test t = new Test();
Test t1= (Test)t.clone();

5. Bu using the deserialization

FileInputStream fis = new FileInputStream("Vinay");
ObjectInputStream obj = new ObjectInputStream(fis);
Dog d = (Dog)obj.readObject();

Not:- There is another way to create the object in Java but this is not standard, it is applicable only for the String

String s = "Vinay";

What will be the output of below Java code?

Example Code: 

public class Test
    int i =10;

    public static void main(String[] args)

Output: Error, Cannot make a static reference to the non-static field i.

What is a Map in Java?

In Java, Map is an interface of Util package which maps unique keys to values. The Map interface is not a subset of the main Collection interface and thus it behaves little different from the other collection types. Below are a few of the characteristics of Map interface: 

  • Map doesn’t contain duplicate keys.
  • Each key can map at max one value.

What is Polymorphism?

Polymorphism is briefly described as "one interface, many implementations". Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow an entity such as a variable, a function, or an object to have more than one form. There are two types of polymorphism:

  • Compile time polymorphism
  • Run time polymorphism

Compile time polymorphism is method overloading whereas Runtime time polymorphism is done using inheritance and interface.

What is abstraction in Java?

Abstraction refers to the quality of dealing with ideas rather than events. It basically deals with hiding the details and showing the essential things to the user. Thus you can say that abstraction in Java is the process of hiding the implementation details from the user and revealing only the functionality to them. Abstraction can be achieved in two ways:

  1. Abstract Classes (0-100% of abstraction can be achieved)
  2. Interfaces (100% of abstraction can be achieved)

In a given string find the repetitive characters. For example for Input: aaaabbbccaaabbc output should be a4b3c2a3b2c1

Below approach is using StringBuffer there can be many other ways:

package testjava.strings;

public class LetterRepeat 
	public static void main(String args[])
		String stringInput = "aaaabbbccaaabbc";
		char[] charInput = stringInput.toLowerCase().toCharArray();
		StringBuffer sb = new StringBuffer();
		for(int i = 0, count; i < charInput.length; i += count)
			count = 1;
			while(i + count < charInput.length && charInput[i] == charInput[i + count])
				count ++;
		String result = sb.toString();
		System.out.println("Input: "+stringInput);
		System.out.println("Output: "+result);


Input: aaaabbbccaaabbc

Output: a4b3c2a3b2c1

In Java, Find the closest number to 0 in a given integer Array.

Check below code, output will be: Nearest to 0: -1

package testjava.arrays;

import java.util.Arrays;

public class ClosestZeo 
	public static void main(String args[])
		int[] data = {-1, -5, 3, 2, 7, 4, -3, -2, -10, 2};
		int current = 0;
		int near = data[0];
		for(int i = 0; i < data.length; i++)
			current = data[i] * data[i];
			if(current <= (near * near))
				near = data[i];
		System.out.println("Nearest to 0: "+near);


Subscribe to Core Java Interview Questions