1) What is String in Java? Is String a data type?
A) A string is a class in java.lang package, but in java all classes are considered as data types. Hence, we can take String as a data type too.
2) What are the interfaces String class Implements?
A) String class implements “3” interfaces in Java. They are:
3) Is String class final in Java?
A) Yes. String class is declared as final in Java.
4) What is the difference between equals() and “ == ” operator?
A) The equals() method compares the characters inside a String object. The “==” operator compares two object references to see whether they refer to the same instance.
5) What is the String Constant pool (String literal pool)?
A) String Constant pool is a separate block of memory where the string objects are held by JVM.
6) Explain the difference between the following statements?
String s = “Hello”;
String s = new String(“Hello”);
A) 1) In first statement assignment operator is used to assign the string. In this case, JVM first checks the String pool whether the same value is already available in the String container or not, if available then it creates another reference to it, if not available then it creates the new one.
2) In the second case each and every time it creates a new object of string.
7) How to concat two different String?
A) We can concat two different Strings with the help of “+” assignment operator or concat() method of String class.
8) What do you mean by immutability?
A) Immutability means once an object is initialized we can’t change.
9) How to make a class immutable in java?
A) 1) Make a class final or private.
2) Remove constructor.
3) Allow only the getter method in your class.
10) What is the difference between String and StringBuffer classes?
|1) String class object is immutable hence there contain cannot be modified.||1) StringBuffer object is mutable so they can be modified.|
|2) The method that directly manipulates data of the object in String class.||2) Such methods are available in String class.|
|3) Append(),reverse() method not available in String class.||3) Append(),reverse() method available in StringBuffer class.|
11) What is String Buffer?
A) String Buffer is like a String but can be modified in the same memory location. Every method of the String Buffer class would be changing the data within the same object and returns the address of the same object.
12) What is the String Builder?
A) String Builder is a mutable sequence of characters. String Builder class is designed for use as a drop-in replacement for StringBuffer in places where the String buffer was being used by a single thread.
13) There are any other classes in java that is immutable?
A) All the wrapper classes in Java are immutable like Integer, Float, Double etc.
14) What is the String Tokenizer class?
A) String Tokenizer class is used to break the string in the form of tokens.
15) There is an alternative to string Tokenizer?
A) String class is an alternative of string tokenizer, with the help of split() method of string.
16) What is the difference between StringBuffer and StringBuilder?
A) StringBuffer is synchronized and StringBuilder is not.
17) How to convert Integer to String?
A) With the help of toString() method of String class.
18) How to convert Integer to String?
A) There are many ways but two ways are shown below:
1) String s = 7 + “ ”;
2) String s = String.valueOf(7);
In the first case, JVM consumes a lot of space for memory. In the second case, JVM does not consume memory.
19) The string is Thread safe or not?
A) Yes, String is thread safe because string object is immutable that’s way string is thread safe.
20) What is difference between compareTo() and equals() method?
|1) compareTo() method is provided by java.lang.String package that checks the difference string to a character by character if both the strings are same then return 0 otherwise it returns (-value) for each character.||1) Equals() method is java.object package Object class method that checks the two different object and returns true or false.|
21) What is the String pool in Java?
A) String pool is a special storage area in java heap, mostly located on Bergen space, to store string literals like “abc”. When Java program creates a new string using a string literal, JVM checks for that string in the pool and, if string literal is already present in a pool then the same object is returned instead of creating a whole new object. String pool check is performed only when you create a string as literal, if you create String using new() operator, a new String object will be created even if the string with the same content is available in the pool.
22) Is String thread-safe in Java?
A) Since a string is immutable, it is thread safe and it can be shared between multiple threads without external synchronization.
23) Why java uses the concept of a string literal?
A) To make Java more memory efficient, because no new objects are created if it exists already in string constant pool.
24) What is the difference between creating an object of string class using the new operator and using doubles quotes?
A) Using double quotes object of the string class representing a specific sequence of characters could be created only once. Whereas using new operator we can create any number of String class objects representing the same sequence of characters.
25) Explain about concatenation operator (+) in Java Strings?
A) The functionality of the concatenation operator is to create a new String class object with the data present at the left-hand side and right-hand side.
26) What is the default capacity of String?
A) Strings don’t have default capacity. The passed String length is its capacity.
27) What is the default capacity of String Buffer?
A) String Buffer has default capacity as 16 buffers. It increases its capacity automatically when sizes reach maximum using its capacity formula ( ( current capacity * 2 ) + 2).
28) In how many ways we can concat String in Java?
A) Using String object, we can concat new string to the current in two ways.
1) using + operator.
2) using concat() method.
29) In how many ways we can concat String Builder in Java?
A) Using String Buffer we can perform the concat operation only in one way.
1) using append() method.
30) What is toString() method?
A) If you want to represent any object as a string, toString() method is used.
31) What is the advantage of the toString() method?
A) By overriding the toString() method of the object class, we can returns the values of the object.
32) What is the use of the charAt() method?
A) CharAt() method returns the character at the specified location.
String name = “TutorialsWeb”
Char ch = name.charAt(7);
33) Java String Palindrome Program?
public static void main(String args)
String original,reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter the Text");
original = in.nextLine();
int length = original.length();
for(int i = length - 1;i >= 0; i--)
reverse = reverse + original.charAt(i);
System.out.println(reverse + " is a Palindrome");
System.out.println(reverse + " is Not a Palindrome");
|Enter the Text
madam is a Palindrome
Also Read: Java collections interview questions
Also Read: jcpassociates kiosk