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. The projects aims to organize the Java source code into independent modules and hence facilitate building them independently.So now we will have multiple core java jars instead of single one. This way of organizing the code would offer following advantages
1. Will make applications lighter as only required modules will be built with the application. Moreover there are some legacy packages currently which anyone hardly use.
2. Reuse same class whole identifiers across different modules.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It means that now individual modules will be built independently instead of complete jdk built in a single jar file. So we will have multiple jars instead of single jar. Moreover this organization would result in lighter applications as they can just built themselves with the required jars and hence ignoring the not required and legacy modules.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Enumeration can iterate only legacy collections like Vector , HashTable and Stack whereas Iterator can iterate both legacy and non legacy collections.
Enumeration is less safer than Iterator
Enumeration is fail safe whereas Iterator is fail fast
Iterator allows for removal of element while traversal whereas Enumeration doesn't have remove method.
Enumerations were introduced in Java 1 whereas Iterators were introduced with Java 2
Enumerations have methods like hasMoreElements and nextElement whereas Iterators have methods like hasNext, next and remove
Help us improve. Please let us know the company, where you were asked this question :
Ans. dynamic method dispatch is a process in which a call to an
overridden method is resolved at runtime rather than at compile-time. This is used to achieve runtime polymorphism in java.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes. Both concepts are independent of each other. Overriding depends on the methods with same name and signature whereas Overloading on same name but with different signatures. All definitions of an overloaded method can be individually overridden in the derived class.
For example -
Class A has overloaded methods "public void myMethod(String str)" and "public void myMethod(int x)"
We can have derived class ClassB extends ClassA and hence can override both methods myMethod(String str) and myMethod(int x) in ClassB
Help us improve. Please let us know the company, where you were asked this question :
Ans. mutual exclusion i.e the resource is not shareable and hence a process should have exclusive access to it at a particular time
Hold and Wait i.e the process doesn't release all existing resources held by it before requesting for another one.
No preemption i.e noone can forcefully release a resource held by another process and the process should voluntarily release the resource held by it.
Circular Wait - For ex, P1 is waiting on resource to be released by P2, P2 is waiting on resource to be released by P3 and P3 is waiting on the resource to be released by P1
Help us improve. Please let us know the company, where you were asked this question :
Ans. Public constructor is simple and easy as it's the default way of object creation. So there are no additional coding overheads as compiler provides the default constructor if none is provided by coder.
With static final methods, it facilitates loose coupling by segregating the responsibility of object creation to a separate method. Validation can be done on the constructor arguments before calling it. Moreover if any adaption on the arguments is required that can achieved easily with factory method.On the flip side, there is coding overhead and additional method call.
Ans. It will print "true" with integers as well as strings. The reason is "Integer constant pool" and "String pool"
String pool maintains pool of string literals. When a string literal is used for the first time, a new string object is created and is added to the pool. Upon it's subsequent usage , the reference for the same object is returned. Similarly java uses integer constant pool.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Synchronized List locks the whole list to provide synchronization and thread safety during the read or write operation, while, CopyOnWriteArrayList doesn’t lock the whole list during these operations.
The CopyOnWriteArrayList class works according to its name i.e. copy-on-write which performs different actions for reading and write operations. For every write operation (add, set, remove, etc), it makes a new copy of the elements in the list. and for the read operations (get, iterator, listIterator, etc), it works on a different copy. So there is no additional overhead during a read operation and its read operation is faster than Collections.SynchronizedList(). Thus, COWAL is better for reading operation than Synchronized List.
Help us improve. Please let us know the company, where you were asked this question :
Q1697. Given a string with multiple opening and closing brackets, determine if the string is valid or not ( to see if the string has all closing brackets for each opening bracket ) ?
Just like Strings, java maintains an integer constant pool too. So 1 will be maintained in integer constant pool and hence reference int2 will point to same integer in pool.
String pool is only for string literals ( defined by "" ) and not for newly created objects and hence str1 == str2 will return false as they are separate objects in memory.
String pool is used for storing string literals so that they can be reused and str3 and str4 will point to same string literal in string pool.
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  string pool  integer constant pool  string comparison  integer comparison
Q1702. Does a Junit without any assertions makes any sense ?
Ans. Yes, If we are testing a code segment to check if throws / doesn't throw an exception. Moreover while testing void methods, we may just verify and not use assert.
Help us improve. Please let us know the company, where you were asked this question :
Q1703. If we have to maintain a value "7" denoting "number of days in a week" as constant variable, Which of the following is the best constant name and Why ?
Constant should be such that it uniquely identifies the value it holds and context of the value. SEVEN uniquely identifies the value but doesn't express the context.
Though NUMBER_OF_DAYS_IN_WEEK and NUMBER_OF_DAYS_IN_WEEK_7 makes sense in terms of unique value identification and context but 7 at the end of constant name appears useless as all weeks have 7 days and hence it's the only value it can hold. If it would have been "number of days in a month", it would have made sense to include number of days at the end like
Q1704. If we have to maintain a value "31" denoting "number of days in a month" as constant variable, Which of the following is the best constant name and Why ?
Constant should be such that it uniquely identifies the value it holds and context of the value. THIRTY_ONE uniquely identifies the value but doesn't express the context. NUMBER_OF_DAYS_IN_MONTH expresses the context but doesn't specify the value clearly as different month can have different number of days.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Objective of constant variable is to provide a context to the value and provide a single point of change. In terms of context, i don't see much need for having constant name for null as null is already self explanatory. In terms of providing single point of change, it doesn't make much sense as it would necessitate that all such placeholders either hold null or a common value.
Help us improve. Please let us know the company, where you were asked this question :
Ans. String Pool makes Java more memory efficient by providing a reusable place for string literals. It might be a little performance inconvenience but results in good amount memory saving.
Help us improve. Please let us know the company, where you were asked this question :
Ans. equals is the method of Object class that is overridden by the classes to specify object equality criteria.
As every class extends Object class, the default implementation of equals is carried to them. Default implementation specified in the Object class is that two objects are treated equal if they are same.
i.e
Object x = new Object();
Object y = new Object();
x.equals(y); // false
x=y;
x.equals(y); // true
i.e x.equals(y) if only x==y
Now every class has the option to specify their object equality by overriding equals method. For example - String class has implemented in a manner if the string value contained in them is exactly same.
Help us improve. Please let us know the company, where you were asked this question :