String Flashcards

1
Q

Is String a keyword in java?

A

No. String is not a keyword in java. String is a final class in java.lang package which is used to represent the set of characters in java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is String a primitive type or derived type?

A

String is a derived type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In how many ways you can create string objects in java?

A

String s1 = new String(“abc”); //Creating string object using new operator

String s2 = “abc”; //Creating string object using string literal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is string constant pool?

A

String objects are most used data objects in Java. Hence, java has a special arrangement to store the string objects. String Constant Pool is one such arrangement. String Constant Pool is the memory space in heap memory specially allocated to store the string objects created using string literals. In String Constant Pool, there will be no two string objects having the same content.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is special about string objects as compared to objects of other derived types?

A

One special thing about string objects is that you can create string objects without using new operator i.e using string literals. This is not possible with other derived types (except wrapper classes). One more special thing about strings is that you can concatenate two string objects using ‘+’. This is the relaxation java gives to string objects as they will be used most of the time while coding. And also java provides string constant pool to store the string objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What do you mean by mutable and immutable objects?

A

Immutable objects are like constants. You can’t modify them once they are created. They are final in nature. Where as mutable objects are concerned, you can perform modifications to them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which is the final class in these three classes – String, StringBuffer and StringBuilder?

A

All three are final. (Interviewer will ask this type of questions to confuse you)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why StringBuffer and StringBuilder classes are introduced in java when there already exist String class to represent the set of characters?

A

The objects of String class are immutable in nature. i.e you can’t modify them once they are created. If you try to modify them, a new object will be created with modified content. This may cause memory and performance issues if you are performing lots of string modifications in your code. To overcome these issues, StingBuffer and StringBuilder classes are introduced in java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How many objects will be created in the following code and where they will be stored in the memory?
String s1 = “abc”;

String s2 = “abc”;

A

Only one object will be created and this object will be stored in the string constant pool.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you create mutable string objects?

A

Using StringBuffer and StringBuilder classes. These classes provide mutable string objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which one will you prefer among “==” and equals() method to compare two string objects?

A

I prefer equals() method because it compares two string objects based on their content. That provides more logical comparison of two string objects. If you use “==” operator, it checks only references of two objects are equal or not. It may not be suitable in all situations. So, rather stick to equals() method to compare two string objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which class will you recommend among String, StringBuffer and StringBuilder classes if I want mutable and thread safe objects?

A

StringBuffer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you convert given string to char array?

A

Using toCharArray() method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
How many objects will be created in the following code and where they will be stored?
String s1 = new String("abc");

String s2 = “abc”;

A

Here, two string objects will be created. Object created using new operator(s1) will be stored in the heap memory. The object created using string literal(s2) is stored in the string constant pool.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Where exactly string constant pool is located in the memory?

A

Inside the heap memory. JVM reserves some part of the heap memory to store string objects created using string literals. [more]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

I am performing lots of string concatenation and string modification in my code. which class among string, StringBuffer and StringBuilder improves the performance of my code. Remember I also want thread safe code?

A

StringBuffer class gives better performance in this scenario. As String class is immutable, if you use this class, a new object will be created after every string concatenation or string modification. This will lower the performance of the code. You can use StringBuilder also, but it is not thread safe. So, StringBuffer will be optimal choice here.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is string intern?

A

String object in the string constant pool is called as String Intern. You can create an exact copy of heap memory string object in string constant pool. This process of creating an exact copy of heap memory string object in the string constant pool is called interning. intern() method is used for interning.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
How many objects will be created in the following code and where they will be stored?
String s1 = new String("abc");

String s2 = new String(“abc”);

A

Two objects will be created and they will be stored in the heap memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Can we call String class methods using string literals?

A

Yes, we can call String class methods using string literals. Here are some examples,

“abc”.charAt(0)

“abc”.compareTo(“abc”)

“abc”.indexOf(‘c’)

20
Q

do you have any idea why strings have been made immutable in java?

A

a) Immutable strings increase security. As they can’t be modified once they are created, so we can use them to store sensitive data like username, password etc.
b) Immutable strings are thread safe. So, we can use them in a multi threaded code without synchronization.
c) String objects are used in class loading. If strings are mutable, it is possible that wrong class is being loaded as mutable objects are modifiable.

21
Q

What is the similarity and difference between String and StringBuffer class?

A

The main similarity between String and StringBuffer class is that both are thread safe. The main difference between them is that String objects are immutable where as StringBuffer objects are mutable.

22
Q

What is the similarity and difference between StringBuffer and StringBuilder class?

A

The main similarity between StringBuffer and StringBuilder class is that both produces mutable string objects. The main difference between them is that StringBuffer class is thread safe where as StringBuilder class is not thread safe.

23
Q

What is the difference between equals() method and == operator?

A

The equals() method matches content of the strings whereas == operator matches object or reference of the strings.

24
Q

Does String is thread-safe in Java?

A

Strings are immutable, so we can’t change its value in the program. Hence it’s thread-safe and can be safely used in the multi-threaded environment.

25
Q

Why String is popular HashMap key in Java?

A

Since String is immutable, its hashcode is cached at the time of creation and it doesn’t need to be calculated again. This makes it a great candidate for key in a Map and it’s processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys.

26
Q

Syntax

How to Returns the character at the specified index?

A

char charAt(int index)

ex:
String s = “Strings are immutable”;
char result = s.charAt(8);

27
Q

Syntax

How to Compares this String to another Object?

A

int compareTo(Object o)

boolean equals(Object anObject)

28
Q

Syntax

How to Compares two strings lexicographically?

A

int compareTo(String anotherString)

ex:
result = str1.compareTo( str2 );

29
Q

Syntax

How to Concatenates the specified string to the end of this string?

A

public String concat(String s)

ex:
String s = “Strings are immutable”;
s = s.concat(“ all the time”);

30
Q

Syntax

Returns true if and only if this String represents the same sequence of characters as the specified StringBuffer.

A

boolean contentEquals(StringBuffer sb)

ex:
String str1 = "Not immutable";
      String str2 = "Strings are immutable";
      StringBuffer str3 = new StringBuffer( "Not immutable");
  boolean  result = str1.contentEquals( str3 );
  System.out.println(result);
31
Q

Syntax

Returns a String that represents the character sequence in the array specified.

A

static String copyValueOf(char[] data)

ex:
char[] Str1 = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘w’, ‘o’, ‘r’, ‘l’, ‘d’};
String Str2 = “”;
Str2 = Str2.copyValueOf( Str1 );
System.out.println(“Returned String: “ + Str2);

32
Q

Syntax

How to Tests if this string ends with the specified suffix?

A

boolean endsWith(String suffix)

33
Q

Syntax

How to Compares this String to another String, ignoring case considerations?

A

boolean equalsIgnoreCase(String anotherString)

34
Q

Syntax

How to Returns the index within this string of the first occurrence of the specified character?

A

int indexOf(int ch)

int indexOf(String str)

35
Q

Syntax

How to Returns the index within this string of the last occurrence of the specified character?

A

int lastIndexOf(int ch)

int lastIndexOf(String str)

36
Q

Syntax

How to Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar?

A

String replace(char oldChar, char newChar)

37
Q

Syntax

How to Replaces each substring of this string that matches the given regular expression with the given replacement?

A

String replaceAll(String regex, String replacement)

38
Q

Syntax

How to Replaces the first substring of this string that matches the given regular expression with the given replacement?

A

String replaceFirst(String regex, String replacement)

39
Q

Syntax

How to Splits this string around matches of the given regular expression?

A

String[] split(String regex)

40
Q

Syntax

How to Tests if this string starts with the specified prefix?

A

boolean startsWith(String prefix)

41
Q

Syntax

How to Returns a new string that is a substring of this string?

A

String substring(int beginIndex)

42
Q

Syntax

How to Converts this string to a new character array?

A

char[] toCharArray()

43
Q

Syntax

How to Converts all of the characters in this String to lower case using the rules of the default locale?

A

String toLowerCase()

44
Q

Syntax

How to This object (which is already a string!) is itself returned?

A

String toString()

45
Q

Syntax

How to Converts all of the characters in this String to upper case using the rules of the default locale?

A

String toUpperCase()

46
Q

Syntax

How to Returns a copy of the string, with leading and trailing whitespace omitted.

A

String trim()

47
Q

Syntax

How to Returns the string representation of the passed data type argument.

A

static String valueOf(primitive data type x)