limited time offer

Core Java Interview Questions

Showing 1 - 10 of 179 results

Write a Java Program to convert "Welcome to Java" to "Java to Welcome".

Check below code:

package com.test.strings;

public class StringWords 
{
	public static void main(String args[])
	{
		String s = "Welcome to Java";
		
		String a[] = s.split(" ");
		
		String rev = "";
		
		for(int i = a.length - 1; i >= 0; i--)
		{
			rev = rev + a[i] + " ";
		}
		
		System.out.println(rev);
	}
}

Output: Java to Welcome

What will be the output of below Java code?

Sample Code: 

public  class  A  
{
    public void m1(int ... i)
    {
        System.out.println("Parent class");
    }
}

public class B extends A
{
    public void m1(int i)
    {
        System.out.println("child class");
	}
}

class C
{
    public static void main(String[] args)
    {
        A a = new A();
        a.m1(10);

        B b=new B();
        b.m1(10);

        A a1=new B();
        a1.m1(10);
    }
}

Output:

Parent class

child class

Parent class

Given an array of ints length 3, return the sum of all the elements.

Check below code:

package com.test.arrays;

public class ThreeElementsSum 
{
	public static void main(String args[])
	{
		int sum = ThreeElementsSum.sum3(new int[] {1,2,3});
		System.out.println(sum);
	}
	
	public static int sum3(int[] nums)
	{
		return nums[0] + nums[1] + nums[2];
	}
}

Output

6

Given two strings, a and b, return the result of putting them together in the order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi".

Check below code:

package com.test.strings;

public class abba 
{
	public static void main(String args[])
	{
		System.out.println(abba.makeAbba("Hi","Bye"));
		System.out.println(abba.makeAbba("Programs","Buzz"));
	}

	public static String makeAbba(String a, String b)
	{
		return(a+b+b+a);
	}
}

Output

HiByeByeHi
ProgramsBuzzBuzzPrograms

Modify and return the given map as follows: if the key "a" has a value, set the key "b" to have that value, and set the key "a" to have the value "". Basically "b" is a bully, taking the value and replacing it with the empty string.

Check below code:

package com.test.collections;

import java.util.HashMap;
import java.util.Map;

public class MapBully 
{
	public static void main(String args[])
	{
		MapBully mb = new MapBully();
		HashMap<String, String> hm = new HashMap<String, String>();
		hm.put("a","candy");
		hm.put("b","dirt");
		System.out.println(mb.mapBully(hm));
	}

	public Map<String, String> mapBully(HashMap<String, String> map) 
	{
        if(map.containsKey("a"))
        {
        	map.put("b", map.get("a"));
        	map.put("a", "");
        }
        return map;
    }
}

Output

{a=, b=candy}

Given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element. Both arrays will be length 1 or more.

Check Below Code:

package com.test.arrays;

public class CommonArrayEnd 
{
	public static void main(String args[])
	{
		CommonArrayEnd ca = new CommonArrayEnd();
		System.out.println(ca.commonEnd(new int[] {1, 2, 6}, new int[] {1, 2, 6}));
		System.out.println(ca.commonEnd(new int[] {1, 2, 3}, new int[] {7, 3, 2}));
		System.out.println(ca.commonEnd(new int[] {1, 2, 3}, new int[] {1}));
	}
	
	public boolean commonEnd(int[] a, int[] b) 
	{
		return (a[0] == b[0] || a[a.length-1] == b[b.length-1]);
	}
}

Output

true
false
true

Given an array of ints, return true if the array is length 1 or more, and the first element and the last element are equal.

Check below code:

package com.test.arrays;

public class FirstLastEqual 
{
	public static void main(String args[])
	{
		FirstLastEqual fl = new FirstLastEqual();
		System.out.println(fl.sameFirstLast(new int[] {1, 2, 1}));
		System.out.println(fl.sameFirstLast(new int[] {1, 2, 3, 1}));
		System.out.println(fl.sameFirstLast(new int[] {1, 2, 3}));
	}
	
	public boolean sameFirstLast(int[] nums) 
	{
		if(nums.length >= 1 && nums[0] == nums[nums.length-1])
			return true;
		else
			return false;	  
	}
}

Output

true
true
false

Given an array of ints, return true if 6 appears as either the first or last element in the array. The array will be length 1 or more.

Check Below Code:

package com.test.arrays;

public class SixFirstLast 
{
	public static void main(String args[])
	{
		SixFirstLast sf = new SixFirstLast();
		System.out.println(sf.firstLast6(new int[] {1, 2, 6}));
		System.out.println(sf.firstLast6(new int[] {2, 4, 6, 8}));
		System.out.println(sf.firstLast6(new int[] {6, 4, 6, 6}));
	}

	public boolean firstLast6(int[] nums) 
	{
		if(nums[0]==6 || nums[nums.length-1] == 6)
			return true;
		else
			return false;
	}
}	

Output

true
false
true

Subscribe to Core Java Interview Questions