0% found this document useful (0 votes)
44 views2 pages

Java Coding Questions and Solutions

Uploaded by

ragavimars48
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)
44 views2 pages

Java Coding Questions and Solutions

Uploaded by

ragavimars48
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

Java Coding Questions with Answers

1. Reverse a String
String str = "SecPod";
String reversed = new StringBuilder(str).reverse().toString();
[Link](reversed); // Output: doPceS

2. Merge Two Sorted Arrays


int[] a = {1, 3, 5};
int[] b = {2, 4, 6};
int[] result = new int[[Link] + [Link]];
int i = 0, j = 0, k = 0;

while (i < [Link] && j < [Link]) {


if (a[i] < b[j]) result[k++] = a[i++];
else result[k++] = b[j++];
}
while (i < [Link]) result[k++] = a[i++];
while (j < [Link]) result[k++] = b[j++];
[Link]([Link](result)); // Output: [1, 2, 3, 4, 5, 6]

3. Check Palindrome
String s = "level";
boolean isPalindrome = true;
int i = 0, j = [Link]() - 1;
while (i < j) {
if ([Link](i) != [Link](j)) {
isPalindrome = false;
break;
}
i++; j--;
}
[Link](isPalindrome); // Output: true

4. Remove Duplicate Characters


String str = "aabbcc";
StringBuilder result = new StringBuilder();
boolean[] seen = new boolean[26];

for (int i = 0; i < [Link](); i++) {


char ch = [Link](i);
if (!seen[ch - 'a']) {
seen[ch - 'a'] = true;
[Link](ch);
}
}
[Link](result); // Output: abc

5. Character Frequency Count


String s = "hello";
int[] freq = new int[26];
for (int i = 0; i < [Link](); i++) {
freq[[Link](i) - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (freq[i] > 0) {
char ch = (char) (i + 'a');
[Link](ch + ": " + freq[i]);
}
}

6. Find Missing Number in Array


int[] arr = {1, 2, 4, 5};
int n = 5; // Since 1 to 5, length should be 5
int total = n * (n + 1) / 2;
int sum = 0;
for (int num : arr) {
sum += num;
}
[Link]("Missing number: " + (total - sum)); // Output: 3

Common questions

Powered by AI

To reverse a string in Java, you can use the StringBuilder's reverse method. For the string "SecPod", you create a StringBuilder object with the string, call the reverse method, and convert it back to a string. The output for "SecPod" using this method is "doPceS" .

To find the missing number in the array [1, 2, 4, 5], use the formula for the sum of the first n natural numbers, n(n + 1)/2, where n is the largest number expected in the sequence. Subtract the sum of the elements in the array from the computed sum. For this array where the largest number should be 5, the missing number is calculated as (15 - (1 + 2 + 4 + 5)) = 3 .

To merge two sorted arrays in Java, you create a result array with the combined length of the two arrays. Use two pointers to iterate through both arrays simultaneously. At each step, compare elements at the pointers, append the smaller element to the result array, and move the pointer forward. Repeat until all elements are merged. For arrays [1, 3, 5] and [2, 4, 6], this process results in the merged array [1, 2, 3, 4, 5, 6].

To determine if a string is a palindrome in Java, compare characters from the start and end of the string moving towards the center. If all mirrored characters are the same, the string is a palindrome. For the string "level", initialize two pointers at the start and end. If characters at both pointers match, move pointers towards the center and continue checking. The process confirms that "level" is a palindrome because the corresponding characters match at each comparison .

To count the frequency of each character in the string "hello", use an integer array of size 26 to store counts for each alphabet letter. Loop through the string, increment the array index corresponding to each character. For "hello", the frequency output is: 'h': 1, 'e': 1, 'l': 2, 'o': 1. Other characters have zero frequency and are not printed .

Duplicate characters can be removed from a string in Java by using a boolean array to track seen characters. For the string "aabbcc", iterate through each character and check if it has been seen. If not, add it to the result and mark it as seen. The outcome is "abc", with duplicates removed .

You might also like