Java Interview Questions and Answers

Q #1: Write a Java program to find the smallest number in an array?
   Input : int [] num ={9, 3, 4, 2 ,7}
   Output: 2
A: class SmallestNumberInAnArray {
    public static void main(String[] args) {
        int [] num = {9,3,4,2,7};
        int len = num.length;
        int tem = num[0];
        for(int i=0; i < len; i++){
            if(num[i] < tem){
                tem=num[i];
            }
        }
        System.out.println("Smallest number in an array - "+tem);
    }
}
Q #2: Sort the Integer array without using any Java inbuild functions?
   Input : int [] num ={9, 3, 4, 2 ,7}
   Output: 2,3,4,7,9
A: class SortInteger {
    public static void main(String[] args) {
        int[] num = {9,3,4,2,7};
        int len = num.length;
        int temp = 0;
        for (int i = 0; i < len-1; i++) {
            for (int j = i + 1; j < len; j++) {
                if (num[i] > num[j]) {
                    temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                    }
                }
            }
        for (int n: num)
            System.out.print(n + " ");
        }
    }
Q #3: Write a java program to reverse the string?
   Input : My name is abc
   Output: yM eman si cba
A #1: class ReverseString {
    public static void main(String[] args) {
        String value ="My name is abc";
        String [] split = value.split(" ");
        for(int i=0;i<split.length;i++){
            StringBuilder sb = new StringBuilder(split[i]);
            sb.reverse().append(" ");
            System.out.print(sb);
        }
    }
}
A #2: class ReverseString {
    public static void main(String[] args) {
        String value ="My name is abc";
        String [] split = value.split(" ");
        for(int i=0;i<split.length;i++){
            char [] ch = split[i].toCharArray();
            int len = ch.length;
            for(int j=len-1;j>=0;j--){
                System.out.print(ch[j]);
            }
            System.out.print(" ");
        }
    }
}
Q #4: Write a java program to count 1 and 0 in a given string?
   Input : String str = "101010";
   Output: ones - 3, zeros - 3
A : class CountNumberOfOnesZeros {
    private static int ones;
    private static int zeros;
    public static void main(String[] args) {
        String value="101010";
        char [] ch = value.toCharArray();        
        for(int i=0;i<ch.length;i++){
            int num = Integer.parseInt(String.valueOf(ch[i]));
            if(num == 0) {
                zeros++;
            }else{
                ones++;
            }
        }
        System.out.println("zeros "+zeros);
        System.out.println("ones "+ones);
    }
}
Q #5: Write a java program for an array of integers, find two index such that they add up to a specific target number?
   Input  #1: nums = [2,7,11,15], target = 9
   Output #1: [0,1]
   Input  #2: nums = [3,2,4], target = 6
   Output #2: [1,2]
A : class ArrayOfTwoIndex{
    public static int[] indexOf(int [] nums, int target){
        int len = nums.length;
        for(int i=0;i<len;i++){
            for(int j=i+1;j<len;j++){
                if(nums[i]+nums[j]==target){
                return new int[]{i,j};
                }
            }
        }
    return new int[]{-1,-1};
}
    public static void main(String[] args) {
        int [] nums = {2,7,11,15};
        int target =22;
        for(int i : indexOf(nums,target))
            System.out.print(i+" ");    
        }
    }
Q #6: Write a java program to remove duplicate numbers from the sorted array?
   Input : int [] nums ={1,2,2,2,3,5};
   Output: [1,2,5]
A : import java.util.*;
    import java.util.stream.Collectors;
    class RemoveDuplicateNumbers {
        public static void main(String[] args) {
            int [] nums ={1,2,2,2,3,5};
            Set sets = Arrays.stream(nums).boxed().collect(Collectors.toSet());
            for(String i : sets)
                System.out.print(i);
            }
        }
Q #7: Write a java program to remove duplicate numbers from the sorted array using inbuild funstions?
   Input : int [] nums ={1,2,2,2,3,5};
   Output: [1,2,5]
A : import java.util.*;
    import java.util.stream.Collectors;
    class RemoveDuplicateNumbers {
        public static void main(String[] args) {
            int [] nums ={1,2,2,2,3,5};
            Set sets = Arrays.stream(nums).boxed().collect(Collectors.toSet());
            for(String i : sets)
                System.out.print(i);
            }
        }
Q #8: Write a java program to remove duplicate numbers from the sorted array without using inbuild funstions?
   Input : int [] nums ={1,2,2,2,3,5};
   Output: [1,2,5]
A : class RemoveDuplicateNumbers {
    public static void main(String [] args){
        int[] nums = {1, 2, 2, 3, 4, 5};
        int[] temp = new int[nums.length];
        int j = 1, k = 0;
        int target = 0, count = 0;
        for (int i = 0; i<nums.length; i++) {
            if (j == nums.length) {
                temp[k++] = nums[i];
            } else {
                target = nums[j++];
            }        
            if (nums[i]<target) {
                temp[k++] = nums[i];
            } else if (nums[i] == target) {
                count++;
            }       
        }
        for (int i = 0; i<=temp.length - count; i++) {
            System.out.print(temp[i]);
        }
    }
}
            
Q #9: Write a java program to reverse the string?
   Input : String value = "apple";
   Output: elppa
A : class ReverseTheString{     
    public static void main(String[] args) {
        String value ="apple";
        for(int i=value.length()-1;i>=0;i--){
            System.out.print(value.charAt(i));
        }
    }
}
Q #10: Write a java program to move all the zeros to end of the array?
   Input : int [] num = {0,1,0,3,12};
   Output: 1,3,12,0,0
A : import java.util.*;
    import java.util.stream.Collectors;
    class MoveAllZerosToEndOfArray {
        public static void main(String[] args) {
            int [] num = {0,1,0,3,12};
            int [] result = new int[num.length];            
            int j=0;
            for(int i=0;i<num.length;i++){
                if(num[i]>0){
                    result[j++]=num[i];
                }
            }
            List integerList = Arrays.stream(result).boxed().collect(Collectors.toList());
            System.out.println(integerList);
        }
    }
Q #11: Write a java program whether string is palindrome?
   Input : String value = "madam";
   Output: String is palindrome
A : class StringPalindrom {
    public static void main(String[] args) {
        String value = "madam";
        int length=value.length();
        boolean isPalindrome=true;
        for(int i=0;i<length;i++){
            if(value.charAt(i)!=value.charAt(length-1-i)){
            isPalindrome=false;
        }
    }
    String result = isPalindrome? value+" string is palindrome" : value+" string is not palidrome";
    System.out.println(result);
    }
}
Q #12: Write a java program whether number is palindrome?
   Input : int value = "151";
   Output: Integer is palindrome
A : class IntegerPalindrom {
    public static void main(String[] args) {
        int num = 151;
        int actualValue=0;
        int expctedValue=num;
        while (num>0){
            actualValue=(actualValue*10)+num%10;
            num=num/10;
        }
        String result = actualValue==expctedValue? "Its paliandrome" : "Its not a paliandrome";
        System.out.print(result);
    }
}
Q #13: Write a java program where,
   Input : String str = "AABBCABCGG";
   Output: A:2 B:2 C:1 A:1 B:1 C:1 G:2
A : class CountDuplicateCharInString {
    public static void main(String[] args) {
        String str = "AABBCABCGG";
        char [] ch = str.toCharArray();
        int j=1;
        int count=1;
        for(int i=0;i<ch.length;i++){
            if(ch[j]==ch[i]){
                count++;
                System.out.println(ch[i]+":"+count);
                count--;
                j+=2;
                i+=1;
            }else{
                System.out.println(ch[i]+":"+count);
                System.out.println(ch[j]+":"+count);
                j+=2;
                i+=1;
            }
        }
    }
}
Q #14: Write a java program to remove the duplicate from String?
   Input : String str = "apple";
   Output: aple
A : class RemoveDuplicateString {
    public static void main(String[] args) {
        String value = "apple";
        char[] ch = value.toCharArray();
        char[] temp = new char [ch.length];
        int j=1,k=0,count=0;
        for(int i=0;i<ch.length;i++){
            if(i==ch.length-1){
                temp[k++]=ch[i];
            }else if(ch[i]==ch[j++]){
                count++;
            }else{
                temp[k++]=ch[i];
            }
        }
        for(int i=0;i<ch.length-count;i++){
            System.out.print(temp[i]);
        }
    }
}
Q #14: Write a java program to print the second largest number in an array list(not a sorted array)?
   Input : int [] num = {3,2,5,1,6};
   Output: 2
A : class SecondLargestArray {
    public static void main(String[] args) {
        int temp = 0;
        for (int i = 0; i < num.length; i++) {
            for (int j = i + 1; j < num.length; j++) {
                if (num[i] > num[j]) {
                    temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                }
            }
        }
        System.out.println(num[1]);
    }
}
Q #15: Write a java program to reverse the integer?
   Input : int num = 456;
   Output: 654
A : class ReverseInteger {
    public static void main(String[] args) {
        int num = 456;
        int temp=0;
        while(num>0){
            temp=(temp*10)+num%10;
            num=num/10;
        }
        System.out.println(temp);
    }
}
Q #16: Write a java program to find number of words in a string?
   Input : String str = "This this is is done by Saket Saket is Saket";
   Output: this-2, is-3, done-1, by-1, Saket-3
A : class NumberOfWordsInString {
    public static void main(String[] args) {
        String str = "This this is is done by Saket Saket is Saket";
        String[] split = str.split(" ");
        LinkedHashMap hashMap = new LinkedHashMap<>();
        for (int i = 0; i < split.length; i++) {
            if (hashMap.containsKey(split[i])) {
                int count = hashMap.get(split[i]);
                hashMap.put(split[i], ++count);
            } else {
                hashMap.put(split[i], 1);
            }
        }
        System.out.println(hashMap);
    }
}
Q #17: Write a java program to find the missing sequence number?
   Input : 1,2,3,4,6,7,8
   Output: 5
A : class MissingSequenceNumber {
    public static void main(String[] args) {
        int [] num ={1,2,3,4,6,7,8};
        int j=1;
        for(int i=0;i<num.length-1;i++){
            if(num[j++]-num[i]!=1){
                System.out.println(j);
            }
        }
    }
}
Q #18: Write a java program to find You are given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string?
Example :1
   Input : s= "codeleet", indices=[4,5,6,7,0,2,1,3]
   Output: leetcode
Example :2
   Input : s= "abc", indices=[0,1,2]
   Output: abc
A : class ShufflingString {
    public static shuffleString(String s, int [] indices){
        Map<Integer, Character> map = new LinkedHashMap<>();
        for(int i=0;i<indices.length;i++){
            map.put(i,s.chartAt(i));
        }
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<indices.length;i++){
            sb.append(map.get(i));
        }

        return sb.toString();
    }
    public static void main(String[] args) {
        String s = "codeleet";
        int [] num ={4,5,6,7,0,2,1,3};
        shuffleString(s,num);
    }
}
Q: What is the highest mountain in the world?
A: Mount Everest is the highest mountain in the world.