โInterview Questions
What is the difference between String
and StringBuilder
in Java?
String
and StringBuilder
in Java?String
is an immutable class, meaning its value cannot be changed once created. Any modification to aString
results in the creation of a newString
object.StringBuilder
is a mutable class that allows efficient modification of strings. It provides methods to append, insert, delete, and replace characters within a string without creating new objects.
Java Example - String
:
In this example, concatenating the string " World" to the existing str
creates a new String
object, resulting in memory overhead.
Java Example - StringBuilder
:
In this example, the StringBuilder
allows efficient modification of the string by appending " World" to the existing sb
an object without creating new objects.
How can you concatenate strings in Java?
In Java, strings can be concatenated using the +
operator or by using the concat()
method.
Java Example - Using +
operator:
Java Example - Using concat()
method:
Both examples yield the same result, "Hello World", by concatenating the strings.
How can you split a string into an array of substrings in Java?
In Java, you can split a string into an array of substrings using the split()
method, specifying a delimiter.
Java Example:
In this example, the split()
method splits the string str
using the comma (",") delimiter, resulting in an array arr
containing three substrings: "Hello", "World", and "Java".
How can you replace characters in a string in Java?
To replace characters in a string, you can use the replace()
method or the replaceAll()
method in Java.
Java Example - Using replace()
method:
In this example, the replace()
method replaces all occurrences of "o" with "X" in the string str
, resulting in "HellX, WXXrld!".
Java Example - Using replaceAll()
method:
In this example, the replaceAll()
method replaces all lowercase vowels (a, e, i, o, u) with "", resulting in "Hll*, W*rld!".
How can you convert a string to uppercase or lowercase in Java?
To convert a string to uppercase or lowercase, you can use the toUpperCase()
method or the toLowerCase()
method in Java.
Java Example - Converting to uppercase:
In this example, the toUpperCase()
method converts all characters in the string str
to uppercase, resulting in "HEL
LO, WORLD!".
Java Example - Converting to lowercase:
In this example, the toLowerCase()
method converts all characters in the string str
to lowercase, resulting in "hello, world!".
How can you extract a substring from a string in Java?
To extract a substring from a string, you can use the substring()
method in Java, specifying the starting index and optionally the ending index.
Java Example:
In this example, the substring()
method extracts the substring starting from index 7 (inclusive) to index 12 (exclusive) from the string str
, resulting in "World".
How can you check if a string contains a specific substring in Java?
To check if a string contains a specific substring, you can use the contains()
method in Java.
Java Example:
In this example, the contains()
method checks if the string str
contains the substring "World". If the substring is found, it returns true
; otherwise, it returns false
.
How can you remove leading and trailing whitespaces from a string in Java?
To remove leading and trailing whitespaces from a string, you can use the trim()
method in Java.
Java Example:
In this example, the trim()
method removes the leading and trailing whitespaces from the string str
, resulting in "Hello, World!".
How can you check if two strings are equal in Java?
To check if two strings are equal in Java, you can use the equals()
method or the equalsIgnoreCase()
method for case-insensitive comparison.
Java Example - Using equals()
method:
In this example, the equals()
method compares the contents of str1
and str2
and returns true
if they are equal.
Java Example - Using equalsIgnoreCase()
method:
In this example, the equalsIgnoreCase()
method compares the contents of str1
and str2
in a case-insensitive manner and returns true
if they are equal.
How can you reverse a string in Java?
Java Example:
How can you check if a string is empty or null in Java?
To check if a string is empty or null in Java, you can use the isEmpty()
method in combination with null check.
Java Example:
In this example, the isEmpty()
method checks if the string str
is empty, and the null check ensures that the string is not null. If the string is either empty or null, the isNullOrEmpty
variable is set to true
.
How can you count the occurrences of a specific character in a string in Java?
To count the occurrences of a specific character in a string, you can iterate through the string and compare each character with the target character.
Java Example:
In this example, the loop iterates through each character in the string str
and increments the count
variable whenever the target character 'o' is found.
How can you check if a string is a palindrome in Java?
To check if a string is a palindrome, you can compare the string with its reverse using the StringBuilder
class.
Java Example:
In this example, the equals()
method compares the original string str
with its reversed version reversedStr
. If they are equal, the isPalindrome
variable is set to true
.
How can you remove all occurrences of a specific character from a string in Java?
To remove all occurrences of a specific character from a string, you can use the replace()
method.
Java Example:
In this example, the replace()
method replaces all occurrences of the target character 'o' with an empty string, effectively removing them from the original string.
How can you check if a string starts with a specific prefix in Java?
To check if a string starts with a specific prefix, you can use the startsWith()
method.
Java Example:
In this example, the startsWith()
method checks if the string str
starts with the prefix "Hello". If it does, the startsWithPrefix
variable is set to true
.
How can you split a string into an array of substrings in Java?
To split a string into an array of substrings, you can use the split()
method.
Java Example:
In this example, the split()
method splits the string str
into an array of substrings using the delimiter ", ", resulting in ["Hello", "World!"].
How can you convert an integer to a string in Java?
To convert an integer to a string, you can use the Integer.toString()
method or the String.valueOf()
method.
Java Example - Using Integer.toString()
:
In this example, the toString()
method converts the integer number
to a string.
Java Example - Using String.valueOf()
:
In this example, the valueOf()
method converts the integer number
to a string.
How to find any letter like all โeโ from the United States of America or from any String?
Sure, here is the Java code to find all occurrences of the letter e
in the string "United States of America":
This code will print the following output:
Code snippet
The code works by first creating a string called string
. This string is initialized to the value "United States of America". Then, a counter variable is created to keep track of the number of occurrences of the letter e
. Then, a for loop is used to iterate through the string, one character at a time. If the current character is equal to the letter e
, the code prints the character and its index in the string. The code then increments the counter variable.
The main function of the code simply calls the main()
method. The main()
method then prints the list of all occurrences of the letter e
in the string.
How to reverse the United States of America or reverse any string?
Sure, here is the Java code to reverse the string "United States of America":
Java
This code will print the following output:
Code snippet
How to split the string "United States of America" into 4 pieces
This code will print the following output:
Code snippet
The code works by first creating a string called string
. This string is initialized to the value "United States of America". Then, a call is made to the split()
method on the string
object. The split()
method takes a delimiter as input and returns an array of strings. In this case, the delimiter is a space character. The split()
method returns an array of strings, each of which is one of the words in the original string.
Finally, a for loop is used to iterate through the array of strings and print each string.
To loop an array in Java, you can use a for loop. The syntax for a for loop to loop an array is as follows:
In this syntax, array
is the name of the array, i
is a loop variable, and array.length
is the length of the array. The loop will iterate through the array, one element at a time. The loop variable i
will be incremented by 1 after each iteration.
How to find out which number contains 3 from 10 to 30?
This code will print the following output:
Code snippet
The code works by first creating a for loop that iterates from 10 to 30. Inside the loop, the String.valueOf()
method is used to convert the integer i
to a string. Then, the contains()
method is used to check if the string contains the character "3". If the string contains the character "3", the number i
is printed.
๐๏ธ Interviewer: What will be the output of the following program? ๐ค
๐โโ๏ธ Interviewee: The output of the program will be "PANKAJ" because we are assigning the value of s2
to s1
using the assignment operator =
. This program is not comparing the strings using the equality operator ==
, so the case difference between the two strings does not matter in this case. ๐
๐๏ธ Interviewer: What will be the output of the following program? ๐ค
๐โโ๏ธ Interviewee: The output of the program will be false
. Although the intern()
method is called on s2
, which returns the reference to the string from the string pool, the reference is not assigned back to s2
in this case. Therefore, s1
and s2
still have different references, resulting in the comparison s1 == s2
evaluating to false
.
If we change the code on line 3 to s2 = s2.intern();
, then the output will be true
because s2
will now refer to the string from the string pool, making s1
and s2
refer to the same object in memory.
Last updated