Skip to content

Forum Navigation

Please or Register to create posts and topics.

Coding Questions ask

1. How Do You Reverse a String in Java?

Steps to Reverse a String:

 

Declare a string.

Calculate the length of the string.

Loop through the characters of the string.

Add each character to a new string in reverse order.

java

 

String str = “hello”;

String reverse = “”;

int length = str.length();

for (int i = 0; i < length; i++) {

    reverse = str.charAt(i) + reverse;

}

System.out.println(reverse);

2. How Do You Determine if a String is a Palindrome?

Steps to Check for a Palindrome:

 

Reverse the original string.

Compare the reversed string with the original.

Output “Palindrome” if they match, otherwise “Not Palindrome”.

java

 

if (str.equals(reverse)) {

    System.out.println(“Palindrome”);

} else {

    System.out.println(“Not Palindrome”);

}

3. Find the Number of Occurrences of a Character in a String

Steps to Count Character Occurrences:

 

Initialize a counter to 0.

Loop through the string to search for the character.

Increment the counter whenever the character is found.

java

int count = 0;

char search = ‘a’;

for (int i = 0; i < length; i++) {

    if (str.charAt(i) == search) {

        count++;

    }

}

System.out.println(count);

4. How to Check if Two Strings Are Anagrams

Steps to Determine Anagrams:

 

Check if the lengths of the two strings are equal. If not, they cannot be anagrams.

Convert both strings to character arrays.

Sort both character arrays.

Compare the sorted arrays to check if they are equal.

java

 

boolean anagramStatus = false;

if (str.length() != reverse.length()) {

    System.out.println(str + ” and ” + reverse + ” are not anagram strings”);

} else {

    char[] anagram1 = str.toCharArray();

    char[] anagram2 = reverse.toCharArray();

    Arrays.sort(anagram1);

    Arrays.sort(anagram2);

    anagramStatus = Arrays.equals(anagram1, anagram2);

}

if (anagramStatus) {

    System.out.println(“Anagram strings”);

} else {

    System.out.println(“Not anagram strings”);

}

5. How Do You Calculate the Number of Vowels and Consonants in a String?

Steps to Count Vowels and Consonants:

 

Initialize counters for vowels and consonants to 0.

Loop through the string.

Check each character:

If it’s a vowel (a, e, i, o, u), increment the vowel counter.

Otherwise, increment the consonant counter.

java

 

int vowels = 0;

int consonants = 0;

for (int k = 0; k < str.length(); k++) {

    char c = str.charAt(k);

    if (c == ‘a’ || c == ‘e’ || c == ‘i’ || c == ‘o’ || c == ‘u’)

        vowels++;

    else

        consonants++;

}

System.out.println(“Vowel count is ” + vowels);

System.out.println(“Consonant count is: ” + consonants);

6. How Do You Get the Matching Elements in an Integer Array?

Steps to Find Duplicate Elements:

 

Declare an array.

Nest two loops to compare each number with the others.

Print the matching elements when found.

java

int[] a = { 1, 2, 3, 4, 5, 1, 2, 6, 7 };

for (int m = 0; m < a.length; m++) {

    for (int n = m + 1; n < a.length; n++) {

        if (a[m] == a[n])

            System.out.print(a[m] + ” “);

    }

}

7. How Would You Implement the Bubble Sort Algorithm?

Steps to Implement Bubble Sort:

 

Declare an array.

Nest two loops to compare adjacent elements in the array.

Swap elements if they are in the wrong order (ascending).

java

Copy code

int[] a = { 1, 2, 7, 6, 4, 9, 12 };

for (int k = 0; k < a.length; k++) {

    for (int l = 0; l < a.length – k – 1; l++) {

        if (a[l] > a[l + 1]) {

            int t = a[l];

            a[l] = a[l + 1];

            a[l + 1] = t;

        }

    }

}

8. How Would You Implement the Insertion Sort Algorithm?

Steps to Implement Insertion Sort:

 

Assume the first element is sorted.

Store the next element in a separate variable (key).

Compare the key with elements on its left and insert it in the correct position.

Repeat the process for all elements.

java

 

int[] a = { 1, 2, 7, 6, 4, 9, 12 };

for (int m = 1; m < a.length; m++) {

    int n = m;

    while (n > 0 && a[n – 1] > a[n]) {

        int k = a[n];

        a[n] = a[n – 1];

        a[n – 1] = k;

        n–;

    }

}

9. How Do You Reverse an Array?

Steps to Reverse an Array:

 

Loop until the middle of the array.

Swap elements at the current index with the corresponding element from the end.

java

 

int[] a = { 1, 2, 7, 6, 4, 9, 12 };

for (int t = 0; t < a.length / 2; t++) { 

    int tmp = a[t]; 

    a[t] = a[a.length – t – 1]; 

    a[a.length – t – 1] = tmp; 

}

 

10. How Do You Find the Intersection of Two Arrays?

Steps to Find Intersection:

 

Declare two arrays.

Use a HashSet to store elements of the first array.

Loop through the second array and check if the element is in the HashSet.

Print the elements found in both arrays.

java

int[] arr1 = {1, 2, 3, 4, 5};

int[] arr2 = {3, 4, 5, 6, 7};

Set<Integer> set = new HashSet<>();

for (int num : arr1) {

    set.add(num);

}

System.out.print(“Intersection of the two arrays: “);

for (int num : arr2) {

    if (set.contains(num)) {

        System.out.print(num + ” “);

    }

}