---Advertisement---

Java String Interview Question and Answers (Part-III)

By Manisha

Published On:

---Advertisement---

Q11: How to Merge Two Sorted Arrays in Java?

Answer:
Merging two sorted arrays into a single sorted array is a common coding interview question. This is similar to the merge step of merge sort.

Java Code:

java

public class MergeSortedArrays {

 public static int[] merge(int[] arr1, int[] arr2) {

  int n1 = arr1.length, n2 = arr2.length;

  int[] result = new int[n1 + n2];

  int i = 0, j = 0, k = 0;

  while (i < n1 && j < n2) {

   result[k++] = (arr1[i] <= arr2[j]) ? arr1[i++] : arr2[j++];

  }

  while (i < n1) result[k++] = arr1[i++];

  while (j < n2) result[k++] = arr2[j++];

  return result;

 }

 public static void main(String[] args) {

  int[] merged = merge(new int[]{1, 3, 5}, new int[]{2, 4, 6});

  System.out.println(“Merged Array: ” + java.util.Arrays.toString(merged));

 }

}

Output:
Merged Array: [1, 2, 3, 4, 5, 6]


🔹 Q12: How to Convert List to Set and Vice Versa in Java?

Answer:

  • List to Set: Removes duplicates.
  • Set to List: Allows indexing and preserves elements.

Java Code:

java

import java.util.*;

public class CollectionConverter {

 public static void main(String[] args) {

  List<String> names = Arrays.asList(“Alice”, “Bob”, “Alice”);

  Set<String> uniqueNames = new HashSet<>(names);

  List<String> finalList = new ArrayList<>(uniqueNames);

  System.out.println(“List to Set: ” + uniqueNames);

  System.out.println(“Set to List: ” + finalList);

 }

}

Output:
List to Set: [Alice, Bob]
Set to List: [Alice, Bob]


🔹 Q13: What is the Difference Between ArrayList, HashSet, and HashMap?

Answer:

  • ArrayList: Maintains insertion order, allows duplicates.
  • HashSet: No duplicates, unordered.
  • HashMap: Key-value pairs, keys are unique.

Example Code:

java

ArrayList<String> list = new ArrayList<>(List.of(“A”, “B”, “A”));

HashSet<String> set = new HashSet<>(list);

HashMap<String, Integer> map = new HashMap<>();

map.put(“Apple”, 1);

map.put(“Banana”, 2);


🔹 Q14: How to Remove All Whitespaces from a String in Java?

Answer:
Use replaceAll() or StringBuilder with Character.isWhitespace().

Code (Regex):

java

String input = ” Java Code “;

String cleaned = input.replaceAll(“\\s+”, “”);

System.out.println(cleaned); // JavaCode


🔹 Q15: How to Extract Only Digits from an Alphanumeric String in Java?

Answer:
Use either regex or manual iteration using Character.isDigit().

Java Code:

java

String mixed = “abc123def456”;

String digits = mixed.replaceAll(“[^0-9]”, “”);

System.out.println(digits); // 123456


🔹 Q16: How to Read Excel Data in Java using Apache POI?

Answer:
Apache POI allows reading .xlsx files using XSSFWorkbook.

Dependencies:

  • poi-5.2.3.jar
  • poi-ooxml-5.2.3.jar

Sample Code:

java

FileInputStream fis = new FileInputStream(“data.xlsx”);

Workbook wb = new XSSFWorkbook(fis);

Sheet sheet = wb.getSheetAt(0);

for (Row row : sheet) {

 for (Cell cell : row) {

  System.out.print(cell.toString() + “\t”);

 }

 System.out.println();

}


🔹 Q17: How to Capture Screenshot in Selenium using Java?

Answer:
Use TakesScreenshot interface to capture webpage screenshot.

Java Code:

java

WebDriver driver = new ChromeDriver();

driver.get(“https://example.com”);

File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(src, new File(“screenshot.png”));


🔹 Q18: What Are Implicit, Explicit, and Fluent Waits in Selenium?

Answer:

  • Implicit Wait: Applies globally to all elements.
  • Explicit Wait: Waits for a specific condition.
  • Fluent Wait: Custom polling & exception handling.

Java Code:

java

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));

wait.until(ExpectedConditions.elementToBeClickable(By.id(“btn”)));

FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)

 .withTimeout(Duration.ofSeconds(20))

 .pollingEvery(Duration.ofSeconds(2))

 .ignoring(NoSuchElementException.class);


🔹 Q19: How to Remove Special Characters from a String in Java?

Answer:
Use regex to allow only alphanumerics.

Java Code:

java

String input = “Hello@#World123!”;

String clean = input.replaceAll(“[^a-zA-Z0-9]”, “”);

System.out.println(clean); // HelloWorld123


🔹 Q20: How to Split a String by Comma and Iterate?

Answer:
Use split() method and for-each loop.

Java Code:

java

String data = “apple,banana,grape”;

String[] fruits = data.split(“,”);

for (String fruit : fruits) {

 System.out.println(fruit);

}

---Advertisement---

Leave a Comment