Java Interview Questions

Displaying 1 - 10 of 239

Write a Java program to print first 5 values which are divisible by 2; 3 and 5

Using While Loop in Java

class DivisibleBy2and3And5
{
    static void divisible(int N)
    {
        int num = 1;
        while (num < N)
        {
            if (num % 2 == 0 && num % 3 == 0 && num % 5 == 0)
                System.out.print(num + " ");
            num++;	
        }
    } 
	
    public static void main(String []args)
    {
        int N = 100;
        divisible(N);
    }
} 

Using For Loop

class DivisibleBy2And3And5
{
    static void divisible(int N)
    {
        for (int num = 1; num < N; num++)
        {
            if (num % 2 == 0 && num % 3 == 0 && num % 5 == 0)
                System.out.print(num + " ");
        }
    } 
	
    public static void main(String []args)
    {
        int N = 100;
        divisible(N);
    }
} 

Write a java program to print numbers between 1 to 100 which are divisible by 3; 5 and by both.

public class divisible3And5Both
{
    public static void main(String args[]) 
    {
        System.out.println("\nDivided by 3: ");		
        for (int i=1; i<100; i++)
        {
			if (i%3==0) 
			System.out.print(i +", ");			
		}			
				
		System.out.println("\n\nDivided by 5: ");
		for (int i=1; i<100; i++) 
		{
			if (i%5==0) System.out.print(i +", ");			
		}
				
		System.out.println("\n\nDivided by 3 & 5: ");			
		for (int i=1; i<100; i++) 
		{
			if (i%3==0 && i%5==0) System.out.print(i +", ");			
		}
		System.out.println("\n");
    }
}

What are checked and unchecked Exception in Java?

Checked Exception: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.

  • FileNotFoundException
  • IOException
  • ClassNotFoundException
  • SQLException 
  • DataAccessException
  • InvocationTargetException
  • MalformedURLException

Unchecked Exception: Unchecked are the exceptions that are not checked at compiled time. 

  • NullPointerException
  • ArrayIndexOutOfBound
  • IllegalArgumentException
  • IllegalStateException
  • IllegalStateException

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

Java Parent Child Objects: 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

Modify and return the given map as follows: if the key "a" has a value, set the key "b" to have that value

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.

Note: 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