Practice 70 Java String Programming Questions
Q1. Write a Java program to check if a string is empty.
- Input: "Hello"
- Expected Output: False
Q2. Write a Java program to reverse a string.
- Input: "Hello"
- Expected Output: "olleH"
Q3. Write a Java program to check if a string contains a specific substring.
- Input: "Hello World", Substring: "World"
- Expected Output: True
Q4. Write a Java program to convert a string to lowercase.
- Input: "HELLO"
- Expected Output: "hello"
Q5. Write a Java program to convert a string to uppercase.
- Input: "hello"
- Expected Output: "HELLO"
Q6. Write a Java program to find the length of a string.
- Input: "Hello"
- Expected Output: 5
Q7. Write a Java program to concatenate two strings.
- Input: "Hello", " World"
- Expected Output: "Hello World"
Q8. Write a Java program to compare two strings for equality.
- Input: "Hello", "hello"
- Expected Output: False
Q9. Write a Java program to check if a string starts with a specific prefix.
- Input: "Hello", Prefix: "He"
- Expected Output: True
Q10. Write a Java program to check if a string ends with a specific suffix.
- Input: "Hello", Suffix: "lo"
- Expected Output: True
Q11. Write a Java program to replace a character in a string.
- Input: "Hello", Old Char: 'o', New Char: 'a'
- Expected Output: "Hella"
Q12. Write a Java program to remove a specific character from a string.
- Input: "Hello", Char to remove: 'l'
- Expected Output: "Helo"
Q13. Write a Java program to find the index of a character in a string.
- Input: "Hello", Character: 'e'
- Expected Output: 1
Q14. Write a Java program to find the last index of a character in a string.
- Input: "Hello", Character: 'l'
- Expected Output: 3
Q15. Write a Java program to check if a string is a palindrome.
- Input: "madam"
- Expected Output: True
Q16. Write a Java program to find a substring from a string.
- Input: "Hello World", Start index: 0, End index: 5
- Expected Output: "Hello"
Q17. Write a Java program to extract a part of a string (substring).
- Input: "Hello World", Start index: 6
- Expected Output: "World"
Q18. Write a Java program to split a string into an array of substrings.
- Input: "Hello World", Delimiter: " "
- Expected Output: ["Hello", "World"]
Q19. Write a Java program to check if a string is a number.
- Input: "1234"
- Expected Output: True
Q20. Write a Java program to count the occurrences of a character in a string.
- Input: "Hello", Character: 'l'
- Expected Output: 2
Q21. Write a Java program to find the first occurrence of a substring in a string.
- Input: "Hello World", Substring: "World"
- Expected Output: 6
Q22. Write a Java program to remove leading and trailing whitespaces from a string.
- Input: " Hello World "
- Expected Output: "Hello World"
Q23. Write a Java program to check if a string contains only digits.
- Input: "12345"
- Expected Output: True
Q24. Write a Java program to convert a string to a char array.
- Input: "Hello"
- Expected Output: ['H', 'e', 'l', 'l', 'o']
Q25. Write a Java program to convert a char array to a string.
- Input: ['H', 'e', 'l', 'l', 'o']
- Expected Output: "Hello"
Q26. Write a Java program to join two strings with a separator.
- Input: "Hello", "World", Separator: "-"
- Expected Output: "Hello-World"
Q27. Write a Java program to check if a string is a valid email address.
- Input: "test@example.com"
- Expected Output: True
Q28. Write a Java program to remove all vowels from a string.
- Input: "Hello"
- Expected Output: "Hll"
Q29. Write a Java program to find the longest word in a string.
- Input: "I love programming in Java"
- Expected Output: "programming"
Q30. Write a Java program to find the frequency of a word in a string.
- Input: "Hello World Hello", Word: "Hello"
- Expected Output: 2
Q31. Write a Java program to check if a string is an anagram of another string.
- Input: "listen", "silent"
- Expected Output: True
Q32. Write a Java program to replace a word in a string.
- Input: "Hello World", Old Word: "World", New Word: "Java"
- Expected Output: "Hello Java"
Q33. Write a Java program to convert the first letter of each word in a string to uppercase.
- Input: "hello world"
- Expected Output: "Hello World"
Q34. Write a Java program to check if a string is empty after trimming whitespace.
- Input: " "
- Expected Output: True
Q35. Write a Java program to count the number of words in a string.
- Input: "I love Java programming"
- Expected Output: 4
Q36. Write a Java program to compare two strings lexicographically.
- Input: "apple", "banana"
- Expected Output: -1
Q37. Write a Java program to find the longest palindrome substring in a string.
- Input: "babad"
- Expected Output: "bab"
Q38. Write a Java program to check if a string contains only alphabetic characters.
- Input: "HelloWorld"
- Expected Output: True
Q39. Write a Java program to replace all spaces in a string with a specific character.
- Input: "Hello World", Character to replace: '-'
- Expected Output: "Hello-World"
Q40. Write a Java program to check if a string contains only lowercase letters.
- Input: "hello"
- Expected Output: True
Q41. Write a Java program to check if a string contains only uppercase letters.
- Input: "HELLO"
- Expected Output: True
Q42. Write a Java program to find the position of the first non-repeated character in a string.
- Input: "swiss"
- Expected Output: 0
Q43. Write a Java program to find the longest prefix of a string.
- Input: "abcd", Prefix: "ab"
- Expected Output: "ab"
Q44. Write a Java program to check if a string is a valid URL.
- Input: "https://www.example.com"
- Expected Output: True
Q45. Write a Java program to find the number of vowels in a string.
- Input: "Hello World"
- Expected Output: 3
Q46. Write a Java program to find the first non-repeated character in a string.
- Input: "swiss"
- Expected Output: "w"
Q47. Write a Java program to extract a substring from the beginning of a string.
- Input: "Hello World", End index: 5
- Expected Output: "Hello"
Q48. Write a Java program to check if a string is a valid phone number.
- Input: "123-456-7890"
- Expected Output: True
Q49. Write a Java program to repeat a string multiple times.
- Input: "Hello", Times: 3
- Expected Output: "HelloHelloHello"
Q50. Write a Java program to find the difference between two strings.
- Input: "Hello", "Hella"
- Expected Output: "o"
Q51. Write a Java program to split a string into an array of words.
- Input: "I love programming"
- Expected Output: ["I", "love", "programming"]
Q52. Write a Java program to join an array of strings into one string.
- Input: ["I", "love", "Java"]
- Expected Output: "I love Java"
Q53. Write a Java program to convert a string to a number (integer).
- Input: "123"
- Expected Output: 123
Q54. Write a Java program to convert a string to a float.
- Input: "12.34"
- Expected Output: 12.34
Q55. Write a Java program to convert an integer to a string.
- Input: 123
- Expected Output: "123"
Q56. Write a Java program to check if a string matches a given regular expression.
- Input: "abc123", Regex: "[a-z]+[0-9]+"
- Expected Output: True
Q57. Write a Java program to find the number of characters in a string excluding spaces.
- Input: "Hello World"
- Expected Output: 10
Q58. Write a Java program to split a string by commas.
- Input: "apple,banana,cherry"
- Expected Output: ["apple", "banana", "cherry"]
Q59. Write a Java program to remove a substring from a string.
- Input: "Hello World", Substring: "World"
- Expected Output: "Hello "
Q60. Write a Java program to capitalize the first letter of each word in a sentence.
- Input: "hello world"
- Expected Output: "Hello World"
Q61. Write a Java program to check if a string contains only whitespace characters.
- Input: " "
- Expected Output: True
Q62. Write a Java program to find the common prefix between two strings.
- Input: "flower", "flow"
- Expected Output: "flow"
Q63. Write a Java program to find the last word in a string.
- Input: "I love Java programming"
- Expected Output: "programming"
Q64. Write a Java program to remove the first word in a string.
- Input: "I love Java programming"
- Expected Output: "love Java programming"
Q65. Write a Java program to find the longest word in a sentence.
- Input: "I love programming in Java"
- Expected Output: "programming"
Q66. Write a Java program to reverse each word in a string.
- Input: "I love Java"
- Expected Output: "I evol avaJ"
Q67. Write a Java program to remove all digits from a string.
- Input: "Hello123"
- Expected Output: "Hello"
Q68. Write a Java program to replace the first occurrence of a word in a string.
- Input: "I love programming", Word to replace: "love", New word: "enjoy"
- Expected Output: "I enjoy programming"
Q69. Write a Java program to check if a string is a valid integer.
- Input: "1234"
- Expected Output: True
Q70. Write a Java program to find the position of the last non-repeated character in a string.
- Input: "swiss"
- Expected Output: 0
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!