Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday 27 August 2017

Interview Questions on String in Java

String Interview Questions in Java

String Interview Questions

This post is all about of interview questions on String in java. Here we will cover all the java String handling interview questions and answers.

So let's start with string interview questions.


(1) What is String?

String is a sequence of characters i.e java string represents sequence of characters e.g "hi, i am anurag".

(2) Is String a keyword in java?

No, String is not a keyword in java. String is a final class and it is belongs to the java.lang package.


(3) How many ways we can create string objects?

There are two ways we can create string objects, The first is through literals(using double quotes) and the seconds is through new operator.

For example :

String name = "programming";//through literal

String name = new String("welcome");// through new operator


(4) What is string constant pool?

String constant pool is a space in heap memory when we create string object using literals(i.e using double quotes (" ")) then it goes into the constant pool. String constant pool does not take duplicate string object. It returns the same reference.

(5) is string is a primitive type or derived type?

String is not a primitive data type, it is derived type.

(6) is string mutable or immutable in java?

It is immutable in java i.e after creating string object we cannot update or modify this string object.


(7) is StringBuffer mutable or immutable?

StringBuffer is a mutable in java i.e it can be modified or change or update after created.

(8) is StringBuilder mutable or immutable?

StringBuilder object is a mutable in java.


(9) How many objects will be created in the following code?

String s = "welcome";

String s1 = "welcome"

only one object will be created in the constant pool because constant pool does not take duplicate object.

(10) What is toString() in java?

Java toString() is a method and it returns the string representation of any object and toString() method belongs to the Object class in java.

(11) Why string is declared immutable or final in java?

There are many reasons for this.
  • Security
  • Thread-safe
  • String pool

(12) What is the difference between String, StringBuffer and StringBuilder?

StringJava string object is a immutable i.e unmodifiable or unchangeable. It can be created by two ways first is using literal and second is new keyword.

StringBuffer: Java StringBuffer is a mutable i.e modifiable or changeable. It can be created by only one way through new keyword. StringBuffer is a synchronized i.e thread-safe.

StringBuilder: Java StringBuilder is a mutable just like StringBuffer but it is non-synchronized.


(13) How many object will be created in the following code?

String s = new String("welcome");

2 object will be created. One is through new keyword in heap memory and second is through literal in string constant pool.

(14) Why java uses literal concept in string?

Because it makes memory efficient(constant pool does not take duplicate objects which created by using string literal).

(15) Difference between " equals() " and " == "?

  • In java equals() is a method which comes from Object class whereas == is an operator.
  • In java equals() method is used to comparing the content of two string objects. If it is matched, return true otherwise false whereas == operator is used to comparing the reference of two strings.

(16) What is StringTokenizer in java?

StringTokenizer class belongs to the java.util package and it is used to breaks the string into tokens with the help of delimiters(symbols or spaces).

Read More: 

Java String Programming Interview Questions.
Java Collection Interview Questions.
Java JSP Interview Questions.
Difference Between String and StringBuffer in Java.
Difference Between Array and Collection in Java.

This String interview questions java will help you in any java interviews.

Share:

3 comments:

  1. will share tricky question on the java string interview questions
    How to Repeat string in java?
    In Java, we can use the repeat() method of StringUtils from Apache Commons Lang package.

    Example

    String str = “beyond”

    String repeated = StringUtils.repeat (str,3)

    Output: beyondbeyondbeyond

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate