Search Interview Questions | Click here and help us by providing the answer. Click Correct / Improve and please let us know. |
|
|||
|
| ||||
| Interview Questions and Answers - Order By Rating | ||||
| ||||
| Ans. void is a return type which indicates that the method returns nothing but returns the control to the line next to the method call. All methods need to have a return type and hence a method returning nothing needs to be declared void. | ||||
| ||||
| 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. | ||||
| ||||
| Ans. Platform is the environment in which a program runs. Java provides a software platform for running JVM based applications and programs. | ||||
| Ans. Eclipse Vert.x is a tool kit for building reactive applications on the JVM. Vert.x is event driven and non blocking. This means the application can handle lot of concurrency using a small number of kernel threads. Vert.x lets the application scale with minimal hardware. | ||||
| ||||
| ||||
| 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. | ||||
| ||||
| Ans. NUMBER_OF_DAYS_IN_MONTH_31 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. | ||||
| ||||
| Ans. NUMBER_OF_DAYS_IN_WEEK 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 NUMBER_OF_DAYS_IN_MONTH_30 NUMBER_OF_DAYS_IN_MONTH_31 NUMBER_OF_DAYS_IN_MONTH_27 NUMBER_OF_DAYS_IN_MONTH_28 | ||||
| ||||
| 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. | ||||
| ||||
| Ans. true false true 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. | ||||
| ||||
| Ans. When it is no longer referenced or it looses all references which were earlier pointing to it. | ||||
| ||||
| Ans. We cannot explicitly initiate destruction of an object as no destroy method is available in java. Only Garbage collection can destroy an object. | ||||
| ||||
| ||||
| 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. | ||||
| ||||
| ||||
| Ans. http://www.devinline.com/2015/10/Lock-Vs-synchronized-in-java.html | ||||
| ||||
| 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. | ||||
| ||||
| 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. We can get an array out of ArrayList by using toArray() method of an array list. String a[] = arrayList.toArray(); | ||||
| ||||
| Ans. Livelock is when two threads or processes get deadlocked because they try to perform the same action to come out of a potential deadlock. | ||||
| ||||
| 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 | ||||
| ||||
| 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 | ||||
| ||||
| 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. | ||||
| ||||
| 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 | ||||
| ||||
| Ans. It means that Java is compiled to byte code that is platform independent and hence can be run anywhere or any machine. | ||||
| ||||
| 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. | ||||
| ||||
| 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. | ||||
| ||||
| Ans. Voltage converters / Power Adapters | ||||
| ||||
| Ans. It introduces a layer of adaptations before it reaches the final and desired interface. Moreover sometimes all requests are forwarded to adapter class. Some of such requests doesn't even require any sort of adaptions as they are qualified to call the final interface directly and introducing overheads. | ||||
| ||||
| Ans. Such a class still can have member elements which can be inherited and hence facilitate code reuse. Moreover Abstract class can have non final static elements whereas interfaces are only allowed to have static final elements. | ||||