Java Programming Practice Questions
Java Programming Practice Questions
To reverse an array in Java, you can use the two-pointer technique. Start one pointer at the beginning and the other at the end, then swap the elements at these pointers and move them towards each other until they meet. For example, reversing `[1, 2, 3, 4, 5]` results in `[5, 4, 3, 2, 1]`.
To find the second highest occurring character, build a frequency table for each character. Convert these frequencies into a sorted list and find the second last element (giving second highest). For the string 'aabbccdddee', 'd' appears as the second most frequently occurring character.
To check if an array is sorted in ascending order, iterate through the array and compare each pair of consecutive elements. If you find any instance where a prior element is greater than the following one, the array is not sorted. If no such pairs are found, the array is sorted. For example, `[1, 2, 3, 4, 5]` is sorted.
To count uppercase and lowercase letters in a string, iterate through each character and use the `Character.isUpperCase()` and `Character.isLowerCase()` to determine the case of each letter. For instance, in "Hello World", there are 2 uppercase and 8 lowercase letters.
To print each character of a string along with its ASCII value, you need to iterate over the string and use the charAt() method to get each character, then cast it to an int to obtain the ASCII value. For example, for input "abc", the output will be 'a : 97 b : 98 c : 99'.
To check if two strings are anagrams, you can sort the characters of both strings and compare them. If they are both the same length with identical sorted characters, they are anagrams. For example, 'listen' and 'silent' can be sorted to 'eilnst' in both cases, confirming they are anagrams.
Yes, it is possible to convert a string to uppercase without using the `toUpperCase()` method by iterating over each character, checking if it is lowercase, and converting it manually by subtracting 32 from the ASCII value of any lowercase letter to get its uppercase equivalent. For example, 'a' can be converted to 'A' by this method.
To determine if two strings are rotations of each other, concatenate one of the strings with itself and check if the other string is a substring of this new concatenated string. For example, 'abcd' and 'cdab' are rotations since 'cdab' is a substring of 'abcdabcd'.
An efficient way to remove duplicate characters without HashSet or Collections is to iterate through the string, using an additional data structure such as a boolean array to track characters that have been encountered. If a character appears for the first time, add it to the result. For example, 'programming' becomes 'progamin'.
To count the number of words in a sentence, one efficient approach in a Java program is to split the string by spaces using the `split(" ")` method, which will return an array of words, and then return the length of this array. For instance, input "This is a test." will result in an output of 4.