Skip to main content

Top 15 Java String Coding Interview Questions (With Answers) Part -1

Top 15 Java String Coding Interview Questions

Top 15 Java String Coding Interview Questions (With Answers)


1. Reverse a String

public class ReverseString {
    public static void main(String[] args) {
        String input = "InterviewYatra";
        String reversed = "";
        for (int i = input.length() - 1; i >= 0; i--) {
            reversed += input.charAt(i);
        }
        System.out.println("Reversed: " + reversed);
    }
}

2. Check if a String is a Palindrome

public class PalindromeCheck {
    public static boolean isPalindrome(String str) {
        int i = 0, j = str.length() - 1;
        while (i < j) {
            if (str.charAt(i++) != str.charAt(j--)) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isPalindrome("madam")); // true
    }
}

3. Count Vowels and Consonants

public class VowelConsonantCount {
    public static void main(String[] args) {
        String str = "InterviewYatra";
        int vowels = 0, consonants = 0;

        for (char ch : str.toLowerCase().toCharArray()) {
            if (Character.isLetter(ch)) {
                if ("aeiou".indexOf(ch) != -1) vowels++;
                else consonants++;
            }
        }
        System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);
    }
}

4. Remove Duplicate Characters

public class RemoveDuplicates {
    public static void main(String[] args) {
        String input = "interview";
        String result = "";
        for (int i = 0; i < input.length(); i++) {
            if (!result.contains(String.valueOf(input.charAt(i)))) {
                result += input.charAt(i);
            }
        }
        System.out.println("Unique: " + result); // intervw
    }
}

5. Check if Two Strings Are Anagrams

import java.util.Arrays;

public class AnagramCheck {
    public static boolean isAnagram(String s1, String s2) {
        char[] a = s1.toCharArray();
        char[] b = s2.toCharArray();
        Arrays.sort(a);
        Arrays.sort(b);
        return Arrays.equals(a, b);
    }
}

6. Character Frequency Count

import java.util.HashMap;

public class CharacterFrequency {
    public static void main(String[] args) {
        String str = "developer";
        HashMap<Character, Integer> map = new HashMap<>();
        for (char c : str.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        System.out.println(map);
    }
}

7. First Non-Repeating Character

public class FirstUniqueChar {
    public static void main(String[] args) {
        String str = "aabbcde";
        for (char c : str.toCharArray()) {
            if (str.indexOf(c) == str.lastIndexOf(c)) {
                System.out.println("First Non-Repeating: " + c);
                break;
            }
        }
    }
}

8. Replace Spaces with Hyphens

public class ReplaceSpaces {
    public static void main(String[] args) {
        String sentence = "Java Developer";
        System.out.println(sentence.replace(" ", "-"));
    }
}

9. Convert to Title Case

public class TitleCase {
    public static void main(String[] args) {
        String[] words = "java developer job".split(" ");
        StringBuilder sb = new StringBuilder();
        for (String word : words) {
            sb.append(Character.toUpperCase(word.charAt(0)))
              .append(word.substring(1)).append(" ");
        }
        System.out.println(sb.toString().trim());
    }
}

10. String Contains Only Digits

public class OnlyDigits {
    public static void main(String[] args) {
        String input = "12345";
        boolean result = input.matches("\\\\d+");
        System.out.println("Only digits: " + result);
    }
}

11. Print All Substrings

public class Substrings {
    public static void main(String[] args) {
        String str = "abc";
        for (int i = 0; i < str.length(); i++) {
            for (int j = i + 1; j <= str.length(); j++) {
                System.out.println(str.substring(i, j));
            }
        }
    }
}

12. String Rotation Check

public class RotationCheck {
    public static void main(String[] args) {
        String s1 = "abcd";
        String s2 = "cdab";
        System.out.println((s1.length() == s2.length()) && ((s1 + s1).contains(s2)));
    }
}

13. Find Longest Word

public class LongestWord {
    public static void main(String[] args) {
        String sentence = "Java backend developer interview questions";
        String[] words = sentence.split(" ");
        String longest = "";
        for (String word : words) {
            if (word.length() > longest.length()) longest = word;
        }
        System.out.println("Longest Word: " + longest);
    }
}

14. Compare Strings Without equals()

public class CompareStrings {
    public static void main(String[] args) {
        String s1 = "Interview";
        String s2 = "Interview";
        boolean same = (s1.compareTo(s2) == 0);
        System.out.println("Same: " + same);
    }
}

15. Reverse Words in Sentence

public class ReverseWords {
    public static void main(String[] args) {
        String sentence = "Java is great";
        String[] words = sentence.split(" ");
        String reversed = "";
        for (int i = words.length - 1; i >= 0; i--) {
            reversed += words[i] + " ";
        }
        System.out.println("Reversed Words: " + reversed.trim());
    }
}

Comments

Popular posts from this blog

Top 15 React Interview Questions for 1–2 Years Experience

🟦 Top 15 React Interview Questions for 1–2 Years Experience Preparing for a React interview with 1–2 years of experience? Here's a carefully curated list of 15 important React questions with clear, real-world answers. These are frequently asked in interviews at companies like TCS, Infosys, Cognizant, Capgemini, and product-based firms. Q1. What is the Virtual DOM in React, and how does it improve performance? Answer: The Virtual DOM is a lightweight, in-memory copy of the real DOM. When state/props change, React creates a new Virtual DOM tree, compares it with the old one (diffing), and only updates the parts of the real DOM that changed. This makes rendering much faster and improves performance in large applications. Q2. What is JSX in React? Answer: JSX stands for JavaScript XML. It allows us to write HTML elements in JavaScript and place them in the DOM without using createElement() . JSX improves code readability and is transpiled to React.createElement() calls. ...

Top 15 Spring Boot Interview Questions and Answers – Real Examples (2025)

Top 15 Spring Boot Interview Questions – 2025 Spring Boot is one of the most demanded frameworks for Java backend development. Whether you're interviewing for TCS, Infosys, or a product-based company, these Spring Boot questions will help you prepare like a pro. Here are 15 questions with detailed explanations for developers with 1–2 years of experience. Q1. What is Spring Boot? Answer: Spring Boot is a Java-based open-source framework built on top of the Spring Framework. It helps developers create stand-alone, production-ready Spring applications with minimal configuration. Its key features include: Auto-configuration Embedded servers (Tomcat, Jetty) Starter dependencies Production-ready tools (Actuator, Metrics, etc.) Example: You can create a REST API within minutes by using @RestController and spring-boot-starter-web — no need for external web server deployment. Q2. What is the role of @SpringBootApplication annotation? Answer: This annotation i...

Wipro Java Developer Interview Questions with Answers (Mid-Level Role)

  Wipro Java Developer Interview Questions with Answers (Mid-Level Role) (Glassdoor Based – May 2024) Interview Location: Bengaluru Interview Mode: Online Candidate Role: Mid-Level Java Developer Source: Based on real experience shared on Glassdoor Review Summary: Easy and conversational. Interviewer was friendly. Focus was mainly on Java basics, internals, and real-world understanding. Q1: What is static in public static void main(String[] args) ? A: The static keyword lets the JVM call the method without creating an object. It indicates that the method belongs to the class, not instances. Q2: Why does a Java program start from the main method? A: main() is the predefined entry point of a Java application. The JVM starts execution from there. Q3: What are Checked and Unchecked Exceptions? With examples. A: Checked Exceptions : Detected at compile time. E.g., IOException , SQLException . Unchecked Exceptions : Detected at runtime. E.g., NullPointerExce...