Top 50 Java String Interview Questions and Answers

String class needs no introduction. String class is one of the most used and very important class in java. Every java application is using String class. In this article I will be sharing 50 most frequently asked java string interview questions and answers. We will divide this post into three categories:

Beginner level (0-1 year experience) Freshers

Intermediate level(1-7 years experienced)

Advanced level (7+ years experienced) java string interview questions and answers.

Note : Please go through all the questions. Interviewer may choose to ask any question.


Beginner Level (0-1 yr): Java String Interview Questions and Answers

Q1.  What is String?

String is a class in java which is present in java.lang package. According to Oracle docs,
The String class represents character strings. Strings are constant, their values can not be changed after they are created.

Q2  Is String immutable in java?

Top 65 Java Programming Interview Questions and Answers

This post is dedicated to all the java programming interview questions and answers that are getting asked in the internship interview or full time entry level graduate interview (freshers). 

Q1. How to reverse a String in java? [Solution]

Write a java program to reverse the String. There are 6 ways to reverse a String in java. You should know at least how to reverse a String without using reverse() method in java.

Q2. How to find the first non repeated character in the String? [Solution]

This is the starting question for the product companies, so make sure you go through it. Write a java program to find the first non repeated character in the String.

Q3. How to find intersection of two arrays in java? [Solution]

Write a java program to find common elements between the two given arrays.
 Suppose given two arrays array1= {1,4,7, 9, 2} arrray2 = {1,7,3,4,5} the answer should be {1,4}
[Solution]

Q4 Find out if String has all Unique Characters? [Solution]

Write a java program to find out if the given String has all Unique Characters. There are 5 ways to determine String has all Unique Characters.

Q5 How to Count number of words in the String? [Solution]

This is an important phone interview coding question. Write a java program to count number of words in the String.

Count occurrences of character in String java program code with example

The common technical interview question in java is to count the occurrence of  a specific word in the string but what if interviewer ask to count the total number of times each alphabet appears in the string . That the question, whose logic you need to think at that point of time and sadly there is no  built in  java method to give you the direct answer by passing the input string .

Read Also :    Count number of  words in the String with Example

So in order to be prepared for such type of questions I have shared the program code below .

Input string  :       "Alive is awesome"

Output           :      A=1,l=1,i=2,v=1,e=3, =2,s=2,a=1,w=1,o=1,m=1
                                                                               

Note :  The result is case sensitive , it means uppercase and lowercase alphabet are treated as different.
for example , Please see above  the alphabet which is texted in Red

Read Also :    Find first Non-Repeated Character in the String


1. Pseudo code/Logic for Count occurrences of Character in String in Java

I will use HashMap to count occurrences of Character in String in java. It will have Character as key and its count occurrences as value. First we will convert the given string into char array.Then , we will traverse char array and increment its value by 1 for each Character present in the input string.

5 Difference between Iterator and Enumeration with example : Java Collections Question


The most common interview question in Collections is What is the difference between iterator and enumeration.The main difference between Iterator and Enumeration is removal of the element while traversing the collection. Iterator can remove the element during traversal of collection as it has remove() method. Enumeration does not have remove() method. In this post I will share the difference and similarities between Iterator and Enumeration along with example.

Iterator

Iterator is the interface and found in the java.util package.
It has three methods

*hasNext()
*next()
*remove()

Read Also:   Java interview questions for experienced

Enumeration

Enumeration is also an interface and found in the java.util package .
It is used for passing through a collection, usually of unknown size.

It has following methods

*hasMoreElements()
*nextElement()

Note : Enumeration does not have remove() method.

Difference between Iterator and Enumeration

Top 50 Java Collections Interview Questions and Answers

We have already shared the most frequently asked java interview questions for experience candidates. Also shared the tricky coding interview questions in our previous posts. Today , we will learn about the top 50 java collections interview questions and answers. We will divide this post into three categories :

Beginner level (0-1 year experience (Freshers)) ,

Intermediate level (1-3 years experienced Java Developers)

Advanced level(3+ Experienced) java collections interview questions and answers

Note : Please prepare all the below questions . Interviewer may choose to ask any question.

Beginner Level (0-1 yr): Java Collections Interview Questions  and Answers


Q1  What is Collection ? What is a Collections Framework ? What are the benefits of Java Collections Framework ?

Collection : A collection (also called as container) is an object  that groups multiple elements into a single unit.

Collections Framework : Collections framework provides unified architecture for manipulating and representing collections.

Benefits of Collections Framework :

1. Improves program quality and speed
2. Increases the chances of reusability of software
3. Decreases programming effort.

Q2 What is the root interface in collection hierarchy ? 

Root interface in collection hierarchy is Collection interface . Few interviewer may argue that