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.
Singleton is used when we would like to reuse an object if object is not supposed to hold request or thread specific information. Inversely Prototype is used in situations where we would like to reuse the object information but the request / thread may require it own data to be persisted.
In short, Singleton is used in situations where we can live with single object being shared across multiple requests or threads whereas Prototype is used when we need duplicate copies of objects.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Object Oriented Design Patterns is the science of identifying the pattern to make objects communicate in a way to effectively implement a Solution.
Factory Design Patterns is the pattern that recommends creation of separate Factory Object for creation of other object. So its like saying - If you want to create an object of ClassA, Talk to FactoryObject ( which is an object of FactoryClass ). FactoryObject in itself encapsulates the inputs and logic required to make the decision regarding the creation and construction of object.
Advantage of Factory Pattern - Loose Coupling and Segregation of Responsibilities. Now instead of hard binding the complete logic to decide the nature and shape of the object on the basis of some conditions, you are assigning the responsibility to some other object and hence making the relationship loosely coupled and hence main tenable.
Disadvantages - Before Understanding the Dis-advantages , we should understand that these patterns were chosen after a period of evolution and research and almost best fit for the required solution, otherwise these patterns would have easily been replaced by now.
Though the advantages of these pattern surpass the disadvantages keeping in mind the decreasing cost of resources and increasing scale of applications, but still loose coupling by means of additional objects results in decreased performance.
Help us improve. Please let us know the company, where you were asked this question :
Ans. A finally block of code always executes, whether or not an exception has occurred.The only time finally won't be called is if you call System.exit() or if the JVM crashes first.
Help us improve. Please let us know the company, where you were asked this question :
Ans. System is a class within java.lang package that contains several useful class fields and methods. It cannot be instantiated and hence can use only statically.even in this case this has been used statically i.e with class name itself and without creating an instance.
out is the static reference of Printstream declared as following in the System Class -
public final static PrintStream out = null;
println is the method of PrintStream class.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes, we can do that. Compiler wont complain. But using object reference we can only access methods which have been defined for object class i.e clone(), equals(), hashCode(), toString() etc.
We cannot access methods defined in String class or in any class in hierarchy between String and Object.
For example - we cannot do obj.append("abc") as it will now give compile time error.
Help us improve. Please let us know the company, where you were asked this question :
Q431. Write a program to print all even numbers first and then all odd numbers till 100. For example the output should be
2
4
6
8
...... 100
and then
1
3
5
7
...... 99
using only 1 for loop ? Is it possible with just one loop ?
public class BuggyBread{
public static void main (String args[]) {
int x = 50;
for(int i=1;i <= 100;i++){
if(i<=50){
System.out.println(i*2);
} else {
System.out.println(i-x);
x = x - 1;
}
}
}
}
Help us improve. Please let us know the company, where you were asked this question :
2. for Each loop syntax is more clean if we have to iterate over the elements of a collection and we need not keep track of the record count
3. For is preferred when we need loops without the usage of collections or Array of objects and entirely primitives are being used
4. for loop is preferred if we need to keep track of record count and have to perform some action of the basis of that. For example - If we have to print something after every 5 records, With for each loop in such case, we will have to keep a separate counter.
Help us improve. Please let us know the company, where you were asked this question :
Ans. static keyword is used to specify that the respective programming construct ( method , variable ) belongs to the class and not to its instance and is supposed to be shared by all instances of the class.
Help us improve. Please let us know the company, where you were asked this question :
Ans. int duplicateArray[] = { 1, 2, 2, 3, 4, 5, 6, 8, 9}
Set unique = new HashSet();
for (int i = 0; i < duplicateArray.length; i) {
if (unique.contains(duplicateArray[i])) {
System.out.println(duplicateArray[i]);
} else {
unique.add(duplicateArray[i]);
}
}
Complexity O(n) = nHashSet contains and add has O(n) = 1
Help us improve. Please let us know the company, where you were asked this question :
Ans. Binary tree is a tree in which each node has up to two children.Tree is a data structure composed of nodes.Each tree has a root node(not necessary in graph theory). The root node has zero or more child nodes.Each child node has zero or more child nodes, and so on.The tree cannot contain cycles.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Selenium WebDriver is a tool for automating web application testing.It helps in replicating the manual tester behavior like keyboard entry, mouse events etc and then matching the output against the expected.
Help us improve. Please let us know the company, where you were asked this question :
This question was recently asked at 'Booking.com,InterContinental Hotels,Caissa,Workday'.This question is still unanswered. Can you please provide an answer.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Amazon Simple Storage Service or S3 is object storage with a simple web service interface to store and retrieve any amount of data from anywhere on the web.
Help us improve. Please let us know the company, where you were asked this question :