Interview Questions & Answers

java

public class StringPalindromeProgram {

 private static boolean isEmpty(final String cs) {
     return cs == null || cs.length() == 0;
 }

 public static boolean checkPalindrome(String input) {

     // Check error conditions
     if (isEmpty(input)) {
         return false;
     }
     String reverse = "";
     int length = input.length();

     for (int i = length - 1; i >= 0; i--) {
         reverse = reverse + input.charAt(i);
     }

     if (input.equals(reverse)) {
         System.out.println(input + " is palindrome = " + true);
     } else {
         System.out.println(input + " is palindrome = " + false);
     }
         return false;
 }

 public static void main(String[] args) {
     checkPalindrome("madam");
     checkPalindrome("abcba");
     checkPalindrome("abc");
  }
}

output:
madam is palindrome = true
abcba is palindrome = true
abc is palindrome = false
 

public class CountOccuranceOfCharInString {

    public static void main(String[] args) {
        int count = countMatches("obify consulting", 'o');
        System.out.println("using for loop : " + count);

    }

    private static int countMatches(final String str, final char ch) {
        if (str == null || str.length() == 0) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (ch == str.charAt(i)) {
                count++;
            }
        }
        return count;
    }
}

output:
2

to be edited

 

public class CountNumberOfWordsInString {
 public static void main(String[] args) {
  count1();
 }

 private static void count1() {
  final String str = "Obify Consulting Sambalpur branch";
  String[] strArray = str.split(" ");
  System.out.println("Number of words in a string = " + strArray.length);
 }

output:
Number of words in a string = 4

public class DuplicateWordsInString {
    private static void duplicateWords(String inputString) {
        // Splitting inputString into words
        final String[] words = inputString.split(" ");

        // Creating one HashMap with words as key and their count as value
        final Map < String, Integer > wordCount = new HashMap < String, Integer > ();

        // Checking each word
        for (String word: words) {
            // whether it is present in wordCount
            if (wordCount.containsKey(word)) {
                // If it is present, incrementing it's count by 1
                wordCount.put(word, wordCount.get(word) + 1);
            } else {
                // If it is not present, put that word into wordCount with 1 as
                // it's value
                wordCount.put(word, 1);
            }
        }

        // Extracting all keys of wordCount
        final Set < String > wordsInString = wordCount.keySet();

        // Iterating through all words in wordCount

        for (String word: wordsInString) {
            // if word count is greater than 1

            if (wordCount.get(word) > 1) {
                // Printing that word and it's count
                System.out.println(word + " : " + wordCount.get(word));
            }
        }
    }

    public static void main(String[] args) {

        duplicateWords("java is java");

        duplicateWords("Java is java again java");

        duplicateWords("Super Man Bat Man Spider Man");
    }
}

output:

java : 2 
java : 2
man : 2

public class Test {
    public static void main(String[] args) {
        String s = new String("5");
        System.out.println(1 + 10 + s + 1 + 10);
    }
}

output:
115110

Explanation: The string concatenation operator works as follows: if both the operands are numbers, 
it performs the addition; otherwise it concats the arguments by calling the toString() method if needed.
 It evaluates from left to right. Hence, the expression in the program results in the string 115110.

public class StrEqual {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = new String("hello");
        String s3 = "hello";
        if (s1 == s2) {
            System.out.println("s1 and s2 equal");
        } else {
            System.out.println("s1 and s2 not equal");
        }
        if (s1 == s3) {
            System.out.println("s1 and s3 equal");
        } else {
            System.out.println("s1 and s3 not equal");
        }
    }
}

output:
s1 and s2 not equal
s1 and s3 equal

Explanation: JVM sets a constant pool in which it stores all the string constants used in the type.
 If two references are declared with a constant, then both refer to the same constant object.
 The == operator checks the similarity of objects themselves (and not the values in it).
 Here, the first comparison is between two distinct objects, so we get s1 and s2 not equal.
 On the other hand, since references to s1 and s3 refer to the same object, we get s1 and s3 equal.

class Base {
    public void test() {
    }
}

class Base1 extends Base {
    public void test() {
        System.out.println("Base1");
    }
}

class Base2 extends Base {
    public void test() {
        System.out.println("Base2");
    }
}

class Test {
    public static void main(String[] args) {
        Base obj = new Base1();
        ((Base2) obj).test(); // CAST
    }
}

output:
The program will result in an exception (ClassCastException).

Explanation: The dynamic type of variable obj is Base1 that you were trying to cast into Base2.
 This is not supported and so results in an exception.