Tuesday, 2 June 2020

Core Java Basic interview questions


Different between String and StringBuffer?
Different between StringBuffer and StringBuilder?
Why String Objects are immutable?
What is the String constant pool?
What is the methods of Object class?
What is different between Comparator and Comparable interfaces?
What is different between "==" operator and equals()?
What is Synchronization?
What is the marker interface?
What is Abstraction,Encapulation and Polymorphism?
What is the static polymorphism and Dynamic polymorphism?
Different between method overloding and overriding?
What is the interface and abstraction?
Different between abstraction and interface?
Which senario is suitable abstraction and interface?
What are the access modifiers?
Diffrent between protected and default packages?
What is diffrent between error and Exception?
What are Checked and Unchecked Exception?
Which situation occure NullPointerException?
What is Thread Life cycle?
Different between wait() and sleep() mthods?
Different between yeild() and join()?
How many ways to create the thread and which is best approach?
What is the Collection? what is the advantage collections overe the arrays?
What is the diffrent between Set and List?
What is the diffrent between HashSet and HashMap?
What is the diffrent between ArrayList and Vector?
What is the diffrent between Hashtable and HashMap?
What is the diffrent between TreeSet and TreeMap?
What is the generic class?
What are the generics and why we are used Generics in Java?
What is the SingleTon class?Example
What is the factory method?
What is the Serialization and Deserialization?
What is the tansient keyword in java?
What is the TypeErrasor?
JDk 1.5 features and 1.6 reatures?
What is use of keywords in Java and how many keywords are there?
What is Fail Fast and Fail Safe in Java?
Difference between Hashtable and ConccurentHashMap?
What is Hashing technique?
What is Callable Interface?
What is different between statement and preparedStatement?

Print the Duplicate Letters in a String in Java

package com.sk.demos;
import java.util.*;
public class FindDuplicates {
    public static void main(String[] args) {
        String input = "SURESH";
        
        // Call the function to print duplicate characters
        printDuplicates(input);
    }

    // Function to print duplicate characters using List
    public static void printDuplicates(String input) {
    // List to keep track of characters we've seen
        List<Character> seen = new ArrayList<>();  
     // Set to store duplicates
        Set<Character> duplicates = new HashSet<>();  
        
        // Iterate through the string and identify duplicates
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            
            // Check if we've seen the character already
            if (seen.contains(ch)) {
            // Add to duplicates if it's already in seen
                duplicates.add(ch);  
            } else {
            // Otherwise, add it to the seen list
                seen.add(ch); 
            }
        }
        
        // Print duplicates
        if (duplicates.isEmpty()) {
            System.out.println("No duplicate characters found.");
        } else {
            System.out.println("Duplicate characters:");
            for (char ch : duplicates) {
                System.out.println(ch);
            }
        }
    }
}
output:
============
S