Interview Questions and Answers Q1. Write a Program to remove multiple spaces
For example
We are what we repeatedly do; excellence, then, is not an act but a habit
should result in
We are what we repeatedly do; excellence, then, is not an act but a habit Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
String str = "We are what we repeatedly do; excellence, then, is not an act but a habit";
String[] splittedString = str.split(" ");
for(String word: splittedString){
word = word.trim();
if(!word.equals("")){
System.out.print(word.trim());
System.out.print(" ");
}
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  Related Questions Difference between == and .equals() ? Why is String immutable in Java ? Explain the scenerios to choose between String , StringBuilder and StringBuffer ?
or
What is the difference between String , StringBuilder and StringBuffer ? What are the difference between composition and inheritance in Java? Explain OOPs
or
Explain OOPs Principles
or
Explain OOPs Concepts
or
Explain OOPs features
or
Tell me something about OOPs What is a Lambda Expression ? What's its use ? What are different ways to create String Object? Explain. Why Char array is preferred over String for storing password? Does garbage collection guarantee that a program will not run out of memory? What is the difference between final, finally and finalize() ?