By Arun Kumar
Interview questions for Java and Spring that are difficult to answer include surprises. You will probably fall short if you attempt to respond to a challenging issue using common sense. Since such questions need technical understanding.
The majority of challenging Java questions are based on perplexing ideas like method overloading and overriding. Multithreading is a complicated concept to grasp. Even if you don't know the answer, try to think twice. Mindset and analytical thinking are the most crucial components of answering a challenging issue.
Let’s analyze a few tough and tricky Interview Questions. We will go through all of the major concepts while covering the questions.
The reason why multiple inheritances are not supported in Java is due to compiler ambiguity. This is famously known as the Diamond problem. Below is a practical example of it.
Suppose there is a class A which has a method named area(). Now, class B and class C extend class A and have their own implementation of method area(). There is also a class D which extends class B and class C (considering for a while what would happen if multiple inheritances are supported). Now, if we directly call method area() with class D instance, the compiler won’t know which method to refer to, class C or class B.
Thus, multiple inheritances are not supported in Java.
Strings in Java are a very special entity in themselves. When a String is created, Java uses a very special pool of memory called the String pool memory. Every String is stored there. Now, Strings are a very popular choice for keys in hashmaps.
We already know that HashMap keys should be unique and should refer to only 1 unique memory. When a String is created, memory is assigned to that String literal in the String pool. When we create another String literal with the same String, the same instance created in the pool earlier is used. Thus, just a pointer is created that points to that instance.
Below is an example of the same. We create a String a with the value “abc”. A new String, when created, is created in the String pool in Java. Now, when String b is created as shown, it refers to the same instance of String. This behavior is overridden when we use the “new” keyword.
We have previously seen how the String pool works. Java has its garbage collector which works independently. As Strings are created and stored in the String pool, there is much reusability that happens within strings. Due to this, Strings are stored for a very long duration in String pools.
Considering the scenario when we store a password as String. Anyone who has GC logs access can see the password clearly as text. This poses a major security threat.
Instead, if a password is stored as a char array, the contents are hidden. As it is stored as an array object, it is also not stored for long durations in GC. Thus, considering the security point of view, using a char array is better than using a String for passwords.
Now let’s analyze some of the most asked and tricky Spring Interview questions.
Dependency injection is the concept of Spring. In DI, one does not create objects. Instead, describe how they should be created. Then expect pre-created objects to be passed in. Likewise, one doesn’t need to directly connect components. Instead, describe which components are needed. This is done with either a configuration file or an annotation. The Spring container is responsible for the rest.
DI can be either constructor based or setter-based. Constructor-based DI is achieved when the container invokes a class constructor with several arguments. Each represents a dependency on other classes. Setter-based dependency injection is achieved when the container calls setter methods on a bean after instantiating it.
An interface that expands the capabilities of the BeanFactory is called an ApplicationContext. ApplicationContext offers the following capabilities in addition to those of the BeanFactory:
There are multiple ways to convert a Java class to a Spring Bean. The foremost thing is the java class should follow the POJO pattern. Then, it could be declared as a Spring Bean in multiple ways as below.
We can declare a Java class as a Bean via XML configuration.
We can also declare a Bean with @Bean annotations. This goes hand in hand with @configuration annotation.
We can manually configure any class as a bean by putting it into Springs application context. This way, we can reuse the bean.
Java and Spring have many questions which can be considered tough and tricky. One needs a great deal of in-depth knowledge to tackle such questions. We have discussed a few Java and Spring-based questions which are the most commonly asked tricky questions. These questions serve as examples and the list is not exhaustive.
Always remember the below points: