Question
Complete the Java code to replace all occurrences of a specific word (case-insensitive) in a string. public class RegexReplacer { public String replaceWord(String text, String wordToReplace, String replacement) { // Example: text="Hello World", wordToReplace="world", replacement="Java" // Expected: "Hello Java" String regex = "(?i)\\b" + Pattern.quote(wordToReplace) + "\\b"; _________ // Line to complete } }
Solution
Correct Answer: E (The replaceAll method of Matcher is needed to replace all occurrences using a regex pattern. text.replaceAll is a convenience method that internally uses Pattern.compile and Matcher.replaceAll.)
More IT Operating System Questions
- A custom stack implementation has a pop() method that is supposed to remove and return the top element. However, when the stack is empty, calling pop() cau...
- Which of the following problems is typically solved using Dynamic Programming, where items cannot be broken into smaller pieces?
- Which of the following is a non-linear data structure?
- Which of one the below options are the two different types of bus topology ?
- Recursive algorithms, often central to Divide and Conquer, are prone to specific debugging challenges. Which of the following is a primary concern when deb...
- Which of the following best describes the relationship between Distributed Parallel Computing and Cloud Computing?
- A relation is in Third Normal Form (3NF) if which of the following conditions is satisfied?
- What is the primary function of a router in a network?
- Which of the following statement is INCORRECT related to mysql_list_tables() function ?
- Which debugging technique involves adding temporary output statements (e.g., print() or console.log()) to display variable values or execution flow?