0% found this document useful (0 votes)
27 views4 pages

Java String Array Questions Solutions

The document contains a series of Java practice questions and solutions focusing on strings and arrays. Each section provides code snippets that demonstrate various operations such as reversing strings, checking for palindromes, counting vowels and consonants, and manipulating arrays. The examples cover fundamental programming concepts and algorithms useful for beginners to practice and enhance their Java skills.

Uploaded by

urur9460
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)
27 views4 pages

Java String Array Questions Solutions

The document contains a series of Java practice questions and solutions focusing on strings and arrays. Each section provides code snippets that demonstrate various operations such as reversing strings, checking for palindromes, counting vowels and consonants, and manipulating arrays. The examples cover fundamental programming concepts and algorithms useful for beginners to practice and enhance their Java skills.

Uploaded by

urur9460
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/ 4

Java Practice Questions & Solutions - Strings and Arrays

String 1: Reverse a String


String input = "hello";
String reversed = new StringBuilder(input).reverse().toString();
System.out.println("Reversed: " + reversed);

String 2: Check Palindrome


String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
System.out.println(str.equals(rev));

String 3: Count vowels and consonants


String input = "Automation".toLowerCase();
int vowels = 0, consonants = 0;
for(char c : input.toCharArray()) {
if("aeiou".indexOf(c) >= 0) vowels++;
else if(Character.isLetter(c)) consonants++;
}
System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);

String 4: Remove spaces


String input = "Java is fun";
String result = input.replace(" ", "");
System.out.println(result);

String 5: Capitalize each word


String[] words = "appium automation".split(" ");
StringBuilder sb = new StringBuilder();
for(String w : words)
sb.append(Character.toUpperCase(w.charAt(0))).append(w.substring(1)).append(" ");
System.out.println(sb.toString().trim());

String 6: Find duplicate characters


String input = "chayann";
Map<Character, Integer> map = new HashMap<>();
for(char c : input.toCharArray()) map.put(c, map.getOrDefault(c, 0) + 1);
for(var e : map.entrySet()) if(e.getValue() > 1) System.out.println(e.getKey());

String 7: First non-repeated character


String input = "swiss";
Map<Character, Integer> map = new LinkedHashMap<>();
for(char c : input.toCharArray()) map.put(c, map.getOrDefault(c, 0) + 1);
for(var e : map.entrySet()) { if(e.getValue() == 1) { System.out.println(e.getKey());
break; } }
String 8: Frequency of characters
String input = "apple";
Map<Character, Integer> freq = new HashMap<>();
for(char c : input.toCharArray()) freq.put(c, freq.getOrDefault(c, 0) + 1);
System.out.println(freq);

String 9: Check Anagram


String a = "listen", b = "silent";
char[] ac = a.toCharArray(), bc = b.toCharArray();
Arrays.sort(ac); Arrays.sort(bc);
System.out.println(Arrays.equals(ac, bc));

String 10: Swap strings without third variable


String a = "hello", b = "world";
a = a + b;
b = a.substring(0, a.length() - b.length());
a = a.substring(b.length());
System.out.println("a: " + a + ", b: " + b);

String 11: Print all substrings


String input = "abc";
for(int i = 0; i < input.length(); i++)
for(int j = i + 1; j <= input.length(); j++)
System.out.println(input.substring(i, j));

String 12: Reverse only words


String input = "Hello world";
String[] words = input.split(" ");
for(String w : words) System.out.print(new StringBuilder(w).reverse() + " ");

String 13: Sum of digits in string


String input = "a1b2c3";
int sum = 0;
for(char c : input.toCharArray()) if(Character.isDigit(c)) sum += c - '0';
System.out.println("Sum: " + sum);

String 14: Check if string has only digits


String input = "12345";
System.out.println(input.matches("\\d+"));

String 15: Find longest word


String input = "find the longest word";
String[] words = input.split(" ");
String longest = "";
for(String w : words) if(w.length() > longest.length()) longest = w;
System.out.println("Longest: " + longest);
Array 1: Max and Min
int[] arr = {3, 7, 2, 9};
int max = arr[0], min = arr[0];
for(int n : arr) { if(n > max) max = n; if(n < min) min = n; }
System.out.println("Max: " + max + ", Min: " + min);

Array 2: Sum of elements


int[] arr = {1, 2, 3};
int sum = 0;
for(int n : arr) sum += n;
System.out.println("Sum: " + sum);

Array 3: Reverse array


int[] arr = {1, 2, 3};
for(int i = arr.length - 1; i >= 0; i--) System.out.print(arr[i] + " ");

Array 4: Even and Odd elements


int[] arr = {1, 2, 3, 4};
for(int n : arr) System.out.println(n + " is " + (n%2==0?"Even":"Odd"));

Array 5: Count positive/negative/zero


int[] arr = {1, -2, 0, 3};
int pos=0, neg=0, zero=0;
for(int n : arr) { if(n>0) pos++; else if(n<0) neg++; else zero++; }
System.out.println("Pos: " + pos + ", Neg: " + neg + ", Zero: " + zero);

Array 6: Find duplicates


int[] arr = {1, 2, 3, 2};
Set<Integer> seen = new HashSet<>();
for(int n : arr) { if(!seen.add(n)) System.out.println("Duplicate: " + n); }

Array 7: Remove duplicates


int[] arr = {1, 2, 2, 3};
Set<Integer> set = new LinkedHashSet<>();
for(int n : arr) set.add(n);
System.out.println(set);

Array 8: Sort without sort()


int[] arr = {5, 2, 8};
for(int i=0;i<arr.length;i++)
for(int j=i+1;j<arr.length;j++)
if(arr[i]>arr[j]) { int t=arr[i]; arr[i]=arr[j]; arr[j]=t; }
System.out.println(Arrays.toString(arr));

Array 9: Check palindrome


int[] arr = {1, 2, 1};
boolean isPal = true;
for(int i=0;i<arr.length/2;i++)
if(arr[i] != arr[arr.length-1-i]) isPal = false;
System.out.println(isPal);

Array 10: Second largest/smallest


int[] arr = {1, 3, 2};
Arrays.sort(arr);
System.out.println("2nd Smallest: " + arr[1] + ", 2nd Largest: " + arr[arr.length-2]);

You might also like