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 Newest | ||||
![]() ![]() | ||||
| ||||
Ans. Goto statement results in unstructured jumps in control from one code location to another and hence makes the code very difficult to read , maintain and debug. Moreover Goto can always be replaced using other statements like break / continue and hence results in structured movement of programming control. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. JVM and JDK are getting smaller. New command line tool called jshell that will add native support and popularize a Java way to REPL (Read-Eval-Print-Loop) private methods in interfaces Introduction of the module system or project Jigsaw JavaDoc Search Light version of JSON API HTTP2 client | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
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. 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. It means that Java is compiled to byte code that is platform independent and hence can be run anywhere or any machine. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
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. 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. 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. We can get an array out of ArrayList by using toArray() method of an array list. String a[] = arrayList.toArray(); | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
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. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. http://www.devinline.com/2015/10/Lock-Vs-synchronized-in-java.html | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
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. We cannot explicitly initiate destruction of an object as no destroy method is available in java. Only Garbage collection can destroy an object. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. When it is no longer referenced or it looses all references which were earlier pointing to it. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
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. 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. 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. 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. Platform is the environment in which a program runs. Java provides a software platform for running JVM based applications and programs. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. A final variable that is not initialized while declaration is a blank final variable. Yes, we can initialize it within a constructor. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
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. 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. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
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. All methods are not expected to return something but Yes, all methods are expected to have a return type. If a method returns nothing, it can be declared with the return type void. Constructors are not expected to have any return types , not even void. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. BuggyBread method without any return type is the constructor which get's called upon object creation whereas BuggyBread method with return type of void is just another method that needs to be called explicitly for it's invocation. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. No, first method is a constructor whereas the second method is just a normal method. There is no way a constructor can be called explicitly and hence all explicit calls to BuggyBread() would result in compilation error. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. No we cannot call a constructor like that. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
![]() ![]() | ||||