Question 11: Sum of Digits in Java
Q: How do you calculate the sum of digits of a number?
Code Example:
java
public class SumOfDigits {
public static void main(String[] args) {
int number = 12345, sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
System.out.println(“Sum of digits: ” + sum);
}
}
Output: 15
Use Case: Common in basic number manipulation questions.
Question 12: Remove Duplicates from Array in Java
Q: How do you remove duplicate elements from an array?
Code Example:
java
import java.util.*;
public class RemoveDuplicates {
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3, 2, 5, 1, 6, 3, 7};
Set<Integer> unique = new HashSet<>(Arrays.asList(numbers));
System.out.println(“Without duplicates: ” + unique);
}
}
Output: [1, 2, 3, 5, 6, 7]
Tip: Use Set to auto-remove duplicates.
Question 13: Reverse Words in a String
Q: How do you reverse each word in a string?
Code Example:
java
public class ReverseWords {
public static void main(String[] args) {
String sentence = “Java Coding Interview”;
String[] words = sentence.split(” “);
StringBuilder result = new StringBuilder();
for (String word : words) {
result.append(new StringBuilder(word).reverse()).append(” “);
}
System.out.println(“Reversed words: ” + result.toString().trim());
}
}
Output: avaJ gnidoC weivretnI
Interview Usage: Shows string manipulation skills.
Question 14: Find Even and Odd Numbers in Java
Q: How do you separate even and odd numbers from an array?
Code Example:
java
import java.util.*;
public class EvenOddFinder {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
List<Integer> even = new ArrayList<>();
List<Integer> odd = new ArrayList<>();
for (int num : numbers) {
if (num % 2 == 0) even.add(num);
else odd.add(num);
}
System.out.println(“Even: ” + even);
System.out.println(“Odd: ” + odd);
}
}
🧾 Output:
Even: [2, 4, 6, 8]
Odd: [1, 3, 5, 7, 9]
Question 15: Find String Length Without Using .length()
Q: How to find the length of a string without using the .length() method?
Code Example:
java
public class StringLengthWithoutMethod {
public static void main(String[] args) {
String str = “automation”;
int length = 0;
try {
while (true) {
str.charAt(length);
length++;
}
} catch (StringIndexOutOfBoundsException e) {}
System.out.println(“Length: ” + length);
}
}
Question 16: How do you convert a String to an Integer and vice versa in Java?
Answer:
In Java, you can convert between String and Integer using built-in methods like Integer.parseInt(), Integer.toString(), and String.valueOf().
String to Integer:
java
String str = “123”;
int num1 = Integer.parseInt(str); // Method 1
int num2 = Integer.valueOf(str); // Method 2
Integer to String:
java
int number = 123;
String str1 = Integer.toString(number); // Method 1
String str2 = String.valueOf(number); // Method 2
String str3 = “” + number; // Method 3 (concatenation)
Complete Example:
java
public class StringIntConversion {
public static void main(String[] args) {
String numStr = “12345”;
int num = Integer.parseInt(numStr);
System.out.println(“String to Integer: ” + num);
int number = 12345;
String str = Integer.toString(number);
System.out.println(“Integer to String: ” + str);
}
}
Output:
vbnet
String to Integer: 12345
Integer to String: 12345
Question 17: How do you print elements at even and odd indexes in a Java array?
Answer:
Use a loop to check each index of the array. If index % 2 == 0, it’s an even index; else, it’s odd.
Example Code:
java
public class EvenOddIndexElements {
public static void main(String[] args) {
String[] elements = {“Java”, “Selenium”, “TestNG”, “Maven”, “Jenkins”, “Docker”};
System.out.print(“Even index elements: “);
for (int i = 0; i < elements.length; i += 2) {
System.out.print(elements[i] + ” “);
}
System.out.print(“\nOdd index elements: “);
for (int i = 1; i < elements.length; i += 2) {
System.out.print(elements[i] + ” “);
}
}
}
Output:
perl
Even index elements: Java TestNG Jenkins
Odd index elements: Selenium Maven Docker
Question 18: How do you reverse an array in Java?
Answer:
Use a two-pointer technique: swap elements from both ends moving toward the center.
Example Code:
java
import java.util.Arrays;
public class ArrayReversal {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
System.out.println(“Original: ” + Arrays.toString(array));
int start = 0;
int end = array.length – 1;
while (start < end) {
int temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end–;
}
System.out.println(“Reversed: ” + Arrays.toString(array));
}
}
Output:
makefile
Original: [1, 2, 3, 4, 5]
Reversed: [5, 4, 3, 2, 1]
Question 19: How do you check if an array is sorted in Java?
Answer:
Loop through the array and compare each element with the next. If any element is greater than the next one, it’s not sorted.
Example Code:
java
public class SortedArrayCheck {
public static void main(String[] args) {
int[] array = {1, 2, 4, 7, 9}; // Try {1, 5, 3, 7, 9} for unsorted
boolean isSorted = true;
for (int i = 0; i < array.length – 1; i++) {
if (array[i] > array[i + 1]) {
isSorted = false;
break;
}
}
System.out.println(“Array is sorted: ” + isSorted);
}
}
Output:
pgsql
Array is sorted: true
Question 20: How do you convert uppercase to lowercase (and vice versa) in Java?
Answer:
Use built-in methods toUpperCase() and toLowerCase() or do it manually using character comparison.
Built-in Methods:
java
String str = “Hello”;
String upper = str.toUpperCase(); // HELLO
String lower = str.toLowerCase(); // hello
Manual Conversion Example:
java
public class CaseConversion {
public static void main(String[] args) {
String input = “Java Programming”;
StringBuilder result = new StringBuilder();
for (char c : input.toCharArray()) {
if (Character.isUpperCase(c)) {
result.append(Character.toLowerCase(c));
} else if (Character.isLowerCase(c)) {
result.append(Character.toUpperCase(c));
} else {
result.append(c);
}
}
System.out.println(“Original: ” + input);
System.out.println(“Case converted: ” + result.toString());
}
}
Output:
vbnet
Original: Java Programming
Case converted: jAVA pROGRAMMING
👉The Next 20 Questions-2: JAVA STRING