Core java - Interview Questions and Answers for 'Hitachi' - 3 question(s) found - Order By Newest Very frequently asked in different variations. Frequently asked in Deloitte ( 2 feedback ) , HCL Tech ( 3 feedback ), TCS and Coginizant (CTS) Q1. Explain the scenerios to choose between String , StringBuilder and StringBuffer ?
or
What is the difference between String , StringBuilder and StringBuffer ? Core Java
Ans. If the Object value will not change, use String Class because a String object is immutable.
If the Object value can change and will only be modified from a single thread, use StringBuilder because StringBuilder is unsynchronized(means faster).
If the Object value may change, and can be modified by multiple threads, use a StringBuffer because StringBuffer is thread safe(synchronized). Sample Code for String Sample Code for StringBuffer Sample Code for StringBuilder Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string class   string   stringbuilder   stringbuffer   String vs StringBuffer   String vs StringBuilder   String vs StringBuilder vs StringBuffer   StringBuffer vs stringBuilder Asked in 29 Companies basic   frequent Try 3 Question(s) TestRelated Questions Difference between == and .equals() ? Why is String immutable in Java ? Why Char array is preferred over String for storing password? What are different ways to create String Object? Explain. What is a String Pool ? How does making string as immutable helps with securing information ? How does String Pool pose a security threat ? How is string object immutable if we can concat a string to it ? What is StringJoiner ? Which of the following is false about main method ?
a. It should be declared public and static
b. it should have only 1 argument of type String array
c. We can override main method
d. We can overload main method Q2. Write a method to check if input String is Palindrome? Core Java
Ans. private static boolean isPalindrome(String str) {
if (str == null)
return false;
StringBuilder strBuilder = new StringBuilder(str);
strBuilder.reverse();
return strBuilder.toString().equals(str);
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string   stringbuilder   stringbuilder   string class   code   palindrome Asked in 38 Companies Basic   frequent Related Questions Which keyword is used to provide explicit access of a code block to single thread ?
a. Transient
b. Final
c. Explicit
d. Synchronized How does volatile affect code optimization by compiler? Will this code give error if i try to add two heterogeneous elements in the arraylist. ? and Why ? What is the difference between the following two code lines ?
1. new OuterClass().new InnerClass();
2. new OuterClass.InnerClass(); Which String class does not override the equals() and hashCode() methods, inheriting them directly from class Object? If arrays cannot be resized , Why is this code valid
String[] strArray = new String[2];
strArray = new String[5]; If you are given a choice to implement the code to either Insert a Record or Update if already exist, Which approach will you follow ? Which of the following is not the advantage of Mocking frameworks ? How can we make sure that a code segment gets executed even in case of uncatched exceptions ? Will the static block be executed in the following code ? Why ? Q3. Write a Program to find number of lines , words and characters in a File. Core Java
Ans. import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileUtility {
public static void main(String[] args) {
System.out.println(printFile("/home/userme/notes"));
}
private static int printFile(String filePath) {
int wordCount = 0;
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line = null;
String[] wordsArray = null;
while ((line = br.readLine()) != null) {
wordsArray = line.split(" ");
wordCount = wordsArray.length;
}
} catch (IOException e) {
e.printStackTrace();
}
return wordCount;
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  code  coding Asked in 2 Companies Related Questions Which keyword is used to provide explicit access of a code block to single thread ?
a. Transient
b. Final
c. Explicit
d. Synchronized How does volatile affect code optimization by compiler? Will this code give error if i try to add two heterogeneous elements in the arraylist. ? and Why ? What is the difference between the following two code lines ?
1. new OuterClass().new InnerClass();
2. new OuterClass.InnerClass(); Which String class does not override the equals() and hashCode() methods, inheriting them directly from class Object? If arrays cannot be resized , Why is this code valid
String[] strArray = new String[2];
strArray = new String[5]; If you are given a choice to implement the code to either Insert a Record or Update if already exist, Which approach will you follow ? Which of the following is not the advantage of Mocking frameworks ? How can we make sure that a code segment gets executed even in case of uncatched exceptions ? Will the static block be executed in the following code ? Why ?