0% found this document useful (0 votes)
5 views

Java_Coding_Questions

The document contains a series of Java coding questions along with their solutions, covering various string manipulation tasks such as reversing a string, checking for palindromes, counting vowels and consonants, and identifying the longest substring without repeating characters. Each code snippet is accompanied by a main method that demonstrates its functionality with example outputs. The topics also include checking if a string contains only digits, removing punctuation, swapping case, finding the first non-repeating character, converting a string to a character array, and counting words in a string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java_Coding_Questions

The document contains a series of Java coding questions along with their solutions, covering various string manipulation tasks such as reversing a string, checking for palindromes, counting vowels and consonants, and identifying the longest substring without repeating characters. Each code snippet is accompanied by a main method that demonstrates its functionality with example outputs. The topics also include checking if a string contains only digits, removing punctuation, swapping case, finding the first non-repeating character, converting a string to a character array, and counting words in a string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Coding Questions with Answers

1. Reverse a String
import java.util.Scanner;
public class ReverseString {
public static String reverse(String str) {
return new StringBuilder(str).reverse().toString();
}
public static void main(String[] args) {
System.out.println(reverse("hello")); // Output: olleh
}
}

2. Check Palindrome
public class Palindrome {
public static boolean isPalindrome(String str) {
return str.equals(new StringBuilder(str).reverse().toString());
}
public static void main(String[] args) {
System.out.println(isPalindrome("madam")); // Output: true
}
}

3. Count Vowels & Consonants


public class VowelConsonantCount {
public static void countVC(String str) {
int vowels = 0, consonants = 0;
for (char c : str.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) != -1) vowels++;
else if (Character.isLetter(c)) consonants++;
}
System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);
}
public static void main(String[] args) {
countVC("hello");
}
}

4. Find Longest Substring Without Repeating Characters


import java.util.HashSet;
public class LongestSubstring {
public static int findLongest(String str) {
int maxLength = 0, left = 0;
HashSet<Character> set = new HashSet<>();
for (int right = 0; right < str.length(); right++) {
while (set.contains(str.charAt(right))) {
set.remove(str.charAt(left++));
}
set.add(str.charAt(right));
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
public static void main(String[] args) {
System.out.println(findLongest("abcabcbb")); // Output: 3
}
}

5. Check if String Contains Only Digits


public class OnlyDigits {
public static boolean isNumeric(String str) {
return str.matches("[0-9]+");
}
public static void main(String[] args) {
System.out.println(isNumeric("12345")); // Output: true
}
}

6. Remove Punctuation from a String


public class RemovePunctuation {
public static String removePunctuations(String str) {
return str.replaceAll("\p{Punct}", "");
}
public static void main(String[] args) {
System.out.println(removePunctuations("Hello, World!")); // Output: Hello World
}
}

7. Swap Case of Characters in a String


public class SwapCase {
public static String swapCase(String str) {
StringBuilder result = new StringBuilder();
for (char c : str.toCharArray()) {
result.append(Character.isUpperCase(c) ? Character.toLowerCase(c) :
Character.toUpperCase(c));
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(swapCase("HeLLo WoRLD")); // Output: hEllO wOrld
}
}

8. Find First Non-Repeating Character


import java.util.LinkedHashMap;
public class FirstNonRepeating {
public static char findFirst(String str) {
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
for (char c : str.toCharArray()) map.put(c, map.getOrDefault(c, 0) + 1);
for (char c : map.keySet()) if (map.get(c) == 1) return c;
return '_';
}
public static void main(String[] args) {
System.out.println(findFirst("swiss")); // Output: w
}
}

9. Convert String to Character Array


public class StringToCharArray {
public static void main(String[] args) {
String str = "Hello";
char[] arr = str.toCharArray();
for (char c : arr) System.out.print(c + " ");
}
}

10. Count Words in a String


public class WordCount {
public static int countWords(String str) {
return str.split("\s+").length;
}
public static void main(String[] args) {
System.out.println(countWords("Hello World Java")); // Output: 3
}
}

You might also like