Search Interview Questions | ![]() ![]() Click here and help us by providing the answer. ![]() Click Correct / Improve and please let us know. |
|
| |||||
Interview Questions and Answers - Expert Level - 81 question(s) found - Order By Newest | |||||
![]() | |||||
![]() | |||||
| |||||
Ans. "equals" is the method of object class which is supposed to be overridden to check object equality, whereas "==" operator evaluate to see if the object handlers on the left and right are pointing to the same object in memory. x.equals(y) means the references x and y are holding objects that are equal. x==y means that the references x and y have same object. Sample code: String x = new String("str"); String y = new String("str"); System.out.println(x == y); // prints false System.out.println(x.equals(y)); // prints true | |||||
![]() | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() ![]() | |||||
![]() | |||||
![]() | |||||
| |||||
Ans. 1. String Pool - When a string is created and if it exists in the pool, the reference of the existing string will be returned instead of creating a new object. If string is not immutable, changing the string with one reference will lead to the wrong value for the other references. Example - String str1 = "String1"; String str2 = "String1"; // It doesn't create a new String and rather reuses the string literal from pool // Now both str1 and str2 pointing to same string object in pool, changing str1 will change it for str2 too 2. To Cache its Hashcode - If string is not immutable, One can change its hashcode and hence it's not fit to be cached. 3. Security - String is widely used as parameter for many java classes, e.g. network connection, opening files, etc. Making it mutable might possess threats due to interception by the other code segment. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() ![]() | |||||
![]() | |||||
| |||||
Ans. 1. Stack Segment - Contains primitives, Class / Interface names and references. 2. Heap Segment - Contains all created objects in runtime, objects only plus their object attributes (instance variables), Static variables are also stored in heap. 3. Code Segment - The segment where the actual compiled Java bytecodes resides when loaded | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() ![]() | |||||
![]() | |||||
| |||||
Ans. String is immutable in java and stored in String pool. Once it's created it stays in the pool until unless garbage collected, so even though we are done with password it's available in memory for longer duration and there is no way to avoid it. It's a security risk because anyone having access to memory dump can find the password as clear text. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() | |||||
| |||||
Ans. OOPs or Object Oriented Programming is a Programming model which is organized around Objects instead of processes. Instead of a process calling series of processes, this model stresses on communication between objects. Objects that all self sustained, provide security by encapsulating it's members and providing abstracted interfaces over the functions it performs. OOP's facilitate the following features 1. Inheritance for Code Reuse 2. Abstraction for modularity, maintenance and agility 3. Encapsulation for security and protection 4. Polymorphism for flexibility and interfacing | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() ![]() | |||||
![]() | |||||
| |||||
Ans. Its an anonymous method without any declaration. Lambda Expression are useful to write shorthand Code and hence saves the effort of writing lengthy Code. It promotes Developer productivity, Better Readable and Reliable code. | |||||
![]() | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() ![]() | |||||
![]() | |||||
![]() | |||||
| |||||
Ans. Using new operator - new xyzClass() Using factory methods - xyzFactory.getInstance( ) Using newInstance( ) method - (Class.forName(xyzClass))emp.newInstance( ) By cloning an already available object - (xyzClass)obj1.clone( ) | |||||
![]() ![]() | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() | |||||
![]() | |||||
| |||||
Ans. Final variable means a variable that has been declared final and hence cannot be de referenced after initialization. Effective final means a variable that has not been declared final but haven't been reassigned the value after initialization. First is the regulation that restricts the reassignment and will raise a compilation error if we try to do so. Second is the outcome without the restriction. Effective Final is the eventual treatment of the variable that is required for many features. For eq - Java 8 requires that local variables referenced from a lambda expression must be final or effectively final.It means all local referenced from lambda expressions must be such that their value shouldn't be changed after initialization whether declared final or not. | |||||
![]() | |||||
![]() ![]() ![]() ![]() | |||||
![]() | |||||
| |||||
Ans. String is widely used as parameter for many java classes, e.g. network connection, opening files, etc. Making it mutable might possess threats due to interception by the other code segment or hacker over internet. Once a String constant is created in Java , it stays in string constant pool until garbage collected and hence stays there much longer than what's needed. Any unauthorized access to string Pool pose a threat of exposing these values. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() | |||||
![]() | |||||
| |||||
Ans. Volatile is an instruction that the variables can be accessed by multiple threads and hence shouldn't be cached. As volatile variables are never cached and hence their retrieval cannot be optimized. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() | |||||
| |||||
Ans. It's weird that compiler doesn't complain if we declare transient with static variable because it makes no sense. At least a warning message saying "transient is useless in this situation" would have helped with code cleaning. Static variables are never serialized and transient is an indication that the specified variable shouldn't be serialized so its kind of double enforcement not to serialize. It could be that as it makes no different to the variable behavior and hence using both keywords with a variable are permitted. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() ![]() | |||||
![]() | |||||
| |||||
Ans. No. Java doesn't allow multi thread access to object constructors so synchronization is not even needed. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() ![]() | |||||
![]() | |||||
| |||||
Ans. When multiple external resources are trying to access the DB locks and runs into cyclic wait, it may makes the DB unresponsive. Deadlock can be avoided using variety of measures, Few listed below - Can make a queue wherein we can verify and order the request to DB. Less use of cursors as they lock the tables for long time. Keeping the transaction smaller. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() ![]() | |||||
| |||||
Ans. Predicate represents an anonymous function that accepts one argument and produces a result. Supplier represents an anonymous function that accepts no argument and produces a result. Consumer represents an anonymous function that accepts an argument and produces no result. | |||||
![]() ![]() ![]() | |||||
![]() | |||||
![]() ![]() ![]() ![]() | |||||
| |||||
Ans. java.lang.StringBuffer. | |||||
![]() | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() | |||||
| |||||
Ans. One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializable Exception. | |||||
![]() | |||||
![]() ![]() ![]() ![]() | |||||
| |||||
Ans. You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses. One of the main reasons is because you probably don't want to override the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() ![]() | |||||
![]() | |||||
| |||||
Ans. Factory Pattern deals with creation of objects delegated to a separate factory class whereas Abstract Factory patterns works around a super-factory which creates other factories. | |||||
![]() | |||||
![]() ![]() ![]() ![]() | |||||
| |||||
Ans. No. compareTo method is declared final for the Enumerations and hence cannot be overriden. This has been intentionally done so that one cannot temper with the sorting order on the Enumeration which is the order in which Enum constants are declared. | |||||
![]() | |||||
![]() ![]() ![]() ![]() | |||||
| |||||
Ans. HashTable has been deprecated. As an alternative, ConcurrentHashMap has been provided. It uses multiple buckets to store data and hence much better performance than HashTable. Moreover, there is already a raw type HashMap. The only difference between the HashTable and HashMap is that Hashtable is synchronized whereas HashMap is not. Most of the synchronized collections have been deprecated and their raw alternative have been presented as preferred.Synchronization has a cost. Using synchronized collection in places where there is no need of it leads to useless utilization of resources. As these collections are rarely used in a static context or shared among threads, Java might have thought it better to just provide the raw collection and let developers implement synchronization if he feels the need to do so. HashMap is now presented as the default and the preferred way of using Map with read optimized hashing, and ConcurrentHashMap has been provided for synchronized access which provides better performance than HashTable. Because of this, Java thought it right to deprecate the use of HashTable.' Synchronization has a cost. Using synchronized collection at a place where there is hardly any need of it would means useless utilization of resources. As these collections are rarely used in static context or shared among threads, Java might have thought it better to just provide the raw collection and let developer implement synchronization if he feels the need to do so. As HashMap has been presented as default and preferred way of using Map with read optimized hashing, and ConcurrentHashMap has been provided for synchronized access which provides better performance than HashTable, Java thought it right to deprecate the use of HashTable. | |||||
![]() | |||||
![]() ![]() ![]() ![]() | |||||
![]() | |||||
| |||||
Ans. member variable are loaded into heap, so they are initialized with default values when an instance of a class is created. In case of local variables, they are stored in stack until they are being used. | |||||
![]() | |||||
![]() ![]() ![]() ![]() | |||||
| |||||
Ans. If we don't declare the list to be of specific type, it treats it as list of objects. int 1 is auto boxed to Integer and "1" is String and hence both are objects. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() | |||||
| |||||
Ans. static loading - Classes are statically loaded with Java new operator. dynamic class loading - Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time. Class.forName (Test className); | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() ![]() | |||||
| |||||
Ans. The memory pool containing all the reflective data of the java virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas. The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods may be stored here. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() | |||||
| |||||
Ans. The Permanent Generation (PermGen) space has completely been removed and is kind of replaced by a new space called Metaspace. The consequences of the PermGen removal is that obviously the PermSize and MaxPermSize JVM arguments are ignored and you will never get a java.lang.OutOfMemoryError: PermGen error. | |||||
![]() | |||||
![]() ![]() ![]() ![]() | |||||
| |||||
Ans. Yes, We can call garbage collector directly but it doesn't guarantees that the gc will start executing immediately. This gc( ) method appears in both Runtime and System classes of java.lang package. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() | |||||
![]() | |||||
| |||||
Ans. co-variant return type states that return type of overriding method can be subtype of the return type declared in method of superclass. it has been introduced since jdk 1.5 | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() | |||||
| |||||
Ans. At the beginning of an object's life, the Java virtual machine (JVM) allocates memory on the heap to accommodate the object's instance variables. When that memory is first allocated, however, the data it contains is unpredictable. If the memory were used as is, the behavior of the object would also be unpredictable. To guard against such a scenario, Java makes certain that memory is initialized, at least to predictable default values before it is used by any code. | |||||
![]() | |||||
![]() ![]() ![]() ![]() | |||||
![]() | |||||
| |||||
Ans. Since String is immutable, its hashcode is cached at the time of creation and it doesnt need to be calculated again. This makes it a great candidate for key in a Map and its processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() ![]() | |||||
| |||||
Ans. intern() method keeps the string in an internal cache that is usually not garbage collected. Moreover provide reference for scp object for corresponding string object present in heap memory. | |||||
![]() | |||||
![]() ![]() ![]() ![]() ![]() | |||||
![]() | |||||