More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. We can follow the same design which we follow in singleton Classes. We can have a static counter that will keep the count of number of objects already created, We can keep the constructor private and being called through the static method. We can keep incrementing the counter before calling the constructor and put a check to call only if it's lesser than n.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes its useless if we are not going to use its objects within Hash collection, For example - HashSet , HashMap. HashCode is used internally by these collections for Search.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Its used to access the object properties using the object reference or class properties using the Class Name. Moreover its used to access the classes and Interfaces of a package.
Help us improve. Please let us know the company, where you were asked this question :
Ans. 1. Security and Safety - They can be shared across multiple threads as they are thread safe. Moreover, it protects then from bad state due to interception by the other code segment. One such problem due to mutability and access by alternate code segment could be the change of hash code and then the impact on its search with hash collections.
2. Reuse - In some cases they can be reused as only one copy would exist and hence it can be relied upon. For example - String Pool
Help us improve. Please let us know the company, where you were asked this question :
Ans. String class has a public method intern() that returns a canonical representation for the string object. String class privately maintains a pool of strings, where String literals are automatically interned.
these are automatically interned so as to have efficient String comparison using == operator instead of equals which is usually slower.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Compilation is the process of converting Java source files to class files which can be executed by the Java runtime environment whereas Decompilation is the process of converting class files back to the Java Source code.
Help us improve. Please let us know the company, where you were asked this question :
Ans. public class Class{
public static void main(String[] args){
List<Integer> collector = new ArrayList();
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
while(x != 0){
collector.add(x);
x = scanner.nextInt();
}
System.out.println(collector.stream().collect(Collectors.averagingInt(p->((Integer)p))));
}
}
Help us improve. Please let us know the company, where you were asked this question :
Ans. public class Class{
public static void main(String[] args){
String string1 = "Hello I am Jack. I live in United States. I live in california state.";
String string2 = "I live in";
if(string1.indexOf(string2) >= -1){
System.out.println("string2 is sub string of string1");
} else {
System.out.println("string2 is not sub string of string1");
}
}
}
Help us improve. Please let us know the company, where you were asked this question :
Ans. public class Class{
public static void main(String[] args){
String string1 = "Hello I am Jack. I live in United States. I live in california state.";
String string2 = "I live in";
int startIndex = 0;
int endIndex = string1.length()-1;
Q609. Write a Program to check if string is a Colidrome ?
(Colidrome is a word that has n alphabets followed by the reverse of the n alphabets, for ex - mallom)
Ans.
public class Class {
public static void main(String[] args) {
String str = "mallam";
String firstHalf = str.substring(0, str.length() / 2);
String secondHalf = str.substring(str.length() / 2);
if (firstHalf.equals(new StringBuilder(secondHalf).reverse().toString())) {
System.out.println("It's a Colidrome");
} else {
System.out.println("It's not a Colidrome");
}
}
}
Help us improve. Please let us know the company, where you were asked this question :
Q610. Write a program in Java that prints the numbers from 1 to 100. But for multiples of three print Fizz instead of the number and for the multiples of five print Buz. For numbers which are multiples of both three and five print FizzBuzz