Java - Strings - Interview Questions

Java strings is a very important topic if you are preparing for a Java interview. Numerous FAQs in Java interviews are either directly or indirectly based on your knowledge of Java strings, including topics such as string builders, string buffers, string constant pools, immutability of strings, performance and efficiencies of string manipulations etc.

A thorough understanding of Java strings is also critical in coding and algorithmic questions in your Java interview. Almost every algorithmic or coding question, either directly and indirectly deals with your knowledge of strings and string manipulations.

Below Java String interview questions, answers, tips and samples will get you grounded on the fundamentals of Strings, important methods to remember, and important code snippets that will be useful in coding and algorithmic interview questions.

Important keywords are provided at the end of the questions. Review them, make them a part of Java vocabulary, and talk about them confidently during your interview process.

What do you understand by immutability of Java String objects?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ ABS, Allston Trading, Atlassian, Barclays, Bitwise, Cloudera, Expicient, Fidelity Investments, Full Creative, Gamesys, Goldman Sachs, IHS Markit, Jungo Connectivity, Man Group, Oracle, Revature, SAS, Sears, State Street, Subex, TCS, TransPerfect, VenturePact, Zalando,

Strings in Java programming language are immutable, i.e. once a string object is created its value cannot be changed. When you change the value of a string reference variable, internally the java virtual machine creates a new string in memory and returns that value. The old string still exists in memory but is not being referenced.

//create new string 'abc'
String s = 'abc'; //'abc' is put into memory

//Modify string to 'abcdef'
s = s.concat('def')

What do you mean by String constant pool?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Capgemini, Conversant, Deloitte , Evergent , Genesys , Guidewire, HCL, IBM , IVY Comptech , Nuance,

Strings literals in Java programming language are immutable. Once a string is created in memory it cannot be changed. If a string literal has to be changed, the Java virtual machine creates the new string literal in memory and returns it.

To make it more memory efficient, the Java virtual machine has a special area of memory called the String constant pool. When a new string is required, the Java virtual machine first checks if the string literal exists in the string constant pool. If it exists then the string reference variable will refer to this string literal, a new literal in not created in memory. If the string literal does not exists in memory then a new string literal is created in the string constant pool.

What is the difference between String and StringBuilder?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Appcino Technologies, Applied Systems, Citrix, Credit Suisse, eBay, Fidelity Investments, Glassdoor, GlobalLogic, IBM, Infogain, IXL Learning, Ludia, Myntra, Nexient, PayPal, Riversand Technologies, SoftCo, Symantec, Tech Mahindra, ThoughtWorks, Titanium Software, Verbat Technologies, Western Digital,

String objects are immutable. Once a string object is created then its value cannot change. Every time you want to get a modified string value, the Java virtual machine will create a new string object. So if you modify a string 100 times, 100 string objects are created in memory.

Unlike string objects, StringBuilder objects are mutable. You can change the value of a string builder object without creating a new object. So you can modify a StringBuilder object many times, but only a single instance of the StringBuilder object is created.

What is the difference between StringBuilder and StringBuffer?

 FAQKey Concept

StringBuilder is not thread safe, i.e it's methods are not synchronized; whereas StringBuffer is thread safe, i.e it's methods are synchronized.

Since the methods of StringBuilder are not synchronized, it is faster than StringBuffer.

If thread safety is not a requirement, you should use StringBuilder instead of StringBuffer.

How do you convert a String to an Integer in Java programming language?

 FAQKey Concept

The Integer wrapper class Java.lang.Integer provides two static methods, Integer.parseInt() and Integer.valueOf() that convert Strings to Integers. Integer.parseInt() returns a primitive int value whereas Integer.valueOf() returns an Integer object.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

How do you convert an Integer to a String in Java programming language?

 FAQKey Concept

You can convert an Integer to a String using two ways. Using the static methods Integer.toString() and String.valueOf()...

*** See complete answer and code snippet in the Java Interview Guide.

What are some of the key methods in String class?

 FAQKey Concept

Following are some of the commonly used methods in the String class

toCharArray() - Returns an array of the string's characters

chatAt() - Returns the character at a specific index position of the string

equals() - Returns true if this string matches the text of another string object.

*** See complete answer and code snippet in the Java Interview Guide.

What are some of the key methods in StringBuilder and StringBuffer class?

 FAQKey Concept

Following are some of the key methods in StringBuilder and StringBuffer classes.

append() - Appends another string to this string.

insert() - Inserts another string into this string. You have to specify the index at which the string has to be included.

delete() - Deletes part of the string.

*** See complete answer and code snippet in the Java Interview Guide.

How do you convert a string to a character array?

 

You can convert a string to a character of arrays by using the method toCharArray() on the String...

*** See complete answer and code snippet in the Java Interview Guide.

How do you traverse through the characters of a string?

 

You can traverse a string two ways.

You can traverse through the characters of a string by using the index position of the string using string.charAt() method. The advantage of this method is that you are traversing the string in memory and are not creating a copy or buffer in memory.

*** See complete answer and code snippet in the Java Interview Guide.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

How do you traverse through the characters of a string in reverse order?

 FAQKey Concept

You can traverse the characters of a string in reverse order in two ways.

You can traverse through a string by using the index position of the string using string.charAt() method. The advantage of this method is that you are traversing the string in memory and are not creating a copy or buffer in memory...

*** See complete answer and code snippet in the Java Interview Guide.

How do you remove specific characters of a string?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Amazon Lab126, Arista Networks, Bloomberg LP,

You are given a string. You have to remove specific characters from the string and print the string.

Lets say the string is 'This is my string' and you have to write a function that takes this string and prints out 'Thi i my tring'.

Your approach will be as follows.

1. Loop through the characters of the string.
2. Check if the character is 's'.
3. ...

*** See complete answer and code snippet in the Java Interview Guide.

How do you reverse the words in a string?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Addepar, Amazon, Amazon Lab126, Aristocrat Technologies, Bloomberg L.P., Cisco Systems, Electronic Arts, GoDaddy, Goldman Sachs, Google, MathWorks, Microsoft, Motorola Mobility, NAVEX Global, NetApp, NICE inContact, Redfin, Salesforce, Tek Travels, Teradata, Tintri, Uber,

You are given a string. You have to reverse the words in the string. Let's say the string is 'interviewgrid.com is awesome'. You have to write a function that takes this string as input and prints 'awesome is interviewgrid.com'. Assume that the words are separated by a single space.

Your approach to this function could be as follows.

1. Create a new StringBuilder object that will store the output string.
2. Tokenize the string into tokens, i.e. words, with single space as delimiter.
3. ...

*** See complete answer and code snippet in the Java Interview Guide.

Write a function to check if two strings are anagrams of each other?

 FAQKey Concept
* Similar Java Interview Question Recently Asked @ Amazon, BlackRock, CA Technologies, EPIC Systems, Google, Microsoft, Opera Solutions, Oracle, Sixt, Veeva Systems, Workday,

Definition - Two string are anagrams of each other if they contain the same count of each character. For example - "interview grid" is an anagram of "view intergrid"

Assumption - Let's make the assumption that upper case and spaces are relevant for the comparison. i.e. "Interview  Grid" is different from "interview grid". Check with the interviewer before making this assumption.

We can solve this problem two ways.

1. Sort the two strings and check if they are equal.

2. ...

*** See complete answer and code snippet in the Java Interview Guide.

 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example