Skip to content

Top Java Programming Interview Questions[Set-2]

interviewQuestion

In This article, I have tried to list down some commonly asked Java programming interview questions.

1)Write a program to find the complement of a number.

Input-6(110)

Output-1(001)


import java.util.Scanner;

class sol1 {
	  
    public static void main(String args[])
    {
    	System.out.println("enter number");
         try (Scanner inp = new Scanner(System.in)) {
			int number= inp.nextInt();
			int rem[]=new int[6];
			
			int i=0;
			while(number >0)
			{
				rem[i]=number%2;
				number=number/2;
				i++;
			}
			for(int j=i-1;j>=0;j--)
			{
				
				
				if(rem[j]==0)
				{
					System.out.print(1);
				}
				else
				{
					System.out.print(0);
				}
			}
		}
    }
}

2) WAP to fetch numerics from an alphanumeric string.

Use IsDigit() method of Character Class. This is an inbuilt method that determines whether the specified char value is a digit or not.


public class isdigit {


public static void separate(String string) {
    StringBuilder alphabetsBuilder = new StringBuilder();
    StringBuilder numbersBuilder = new StringBuilder();
    
    for (int i = 0; i < string.length(); i++) {
        char ch = string.charAt(i);
        if (Character.isDigit(ch)) {
            numbersBuilder.append(ch);
        } 
        else {
            alphabetsBuilder.append(ch);
        }
    }
  
    System.out.println("Numbers in String: " + numbersBuilder.toString());
     
}
    public static void main(String str[])
    {
       
    	separate("Test4567");

    }
}

3)How to calculate max consecutive one’s from a binary array?

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        
        int count=0;
        int max=0;
        for(int i=0;i<nums.length;i++)
        {
            if(nums[i]!=0)
            {
                count++;
                max=Math.max(max,count);
            }
            else
            {
                count=0;
            }
        }
        return max;
    }
}
class main
{
    static int num []={1,1,0,1,0,0,1,1,1};
    public static void main(String [] str)
    {
        Solution s = new Solution();
       int total= s.findMaxConsecutiveOnes(num);
        System.out.println(total);
    }
}

4)How to retrieve all the keys present in HashMap?

public class JavaHashMapExample 
{    
    public static void main(String[] args) 
    {
       
         
    HashMap<Integer, String> map = new HashMap<Integer, String>();
         
        //Adding key-value pairs to HashMap
         
        map.put(1, "Test");
         
        map.put(2, "Java");
         
        map.put(3, "Program");
         
        map.put(4, "For");
         
        map.put(5, "Hashmap");
         
        //Retrieving the Key Set
         
        Set<Integer> keySet = map.keySet();
         
        for (Integer key : keySet) 
        {
            System.out.println(key);
        }
    }   
}

5) Sort HashMap using keys.

import java.util.*;

class sortHashMap {

	static Map<String, Integer> map = new HashMap<>();

	public static void sortbykey() {
		ArrayList<String> sortedKeys = new ArrayList<String>(map.keySet());

		Collections.sort(sortedKeys);

		for (String x : sortedKeys)
			System.out.println("Key = " + x + ", Value = " + map.get(x));
	}

	public static void main(String args[]) {
		map.put("test", 80);
		map.put("hash", 90);
		map.put("map", 80);
		map.put("sorting", 75);
		map.put("injava", 40);

		sortbykey();
	}
}

6)Count number of characters of last word in a string.

public class leetcode {
	
	static int reverseEachWordOfString(String inputString)
    {
		int len=0;
        String x=inputString.trim();
        System.out.println(x);
      
         
        for (int i = 0; i < x.length(); i++) {
            if (x.charAt(i) == ' ')
                len = 0;
            else
                len++;
        }
 
        return len;
    }
 
     public static void main(String[] args) 
    {
        int count=reverseEachWordOfString("Count occurence of string ");
         
        System.out.print(count);
    }
}

7)WAP to reverse an Array list.

 public static void  reverseList()
    {
        List<Character> listb = new ArrayList<Character>();
    	
    	listb.add('a');
    	listb.add('b');
    	listb.add('z');
    	listb.add('c');
        for (int i = 0; i < listb.size(); i++) {
            listb.add(i, listb.remove(listb.size() - 1));
        }
        System.out.println(listb);
      }

8)Write a programme to split a string without using any inbuilt method.

public static void splitString()
    {
    	 Scanner input = new Scanner(System.in);
         System.out.print("Enter the String to be Spilted : ");
         String st = input.nextLine();
         Scanner str = new Scanner(st);
         while (str.hasNext())
         {
        	
             System.out.println(str.next());
         }
 }

For more coding questions visit here.