Close button

Are You Looking for Developers to Hire?
Have a Look at Our Bench Strength

Hire Developers
Close button
Most Tricky Spring And Java Interview Questions

Most Tricky Spring And Java Interview Questions

By Arun Kumar

Most Tricky Spring And Java Interview Questions

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.

The majority of challenging Java questions are based on perplexing ideas like method overloading and overriding.

Tricky Java Interview Questions

Let’s analyze a few tough and tricky Interview Questions. We will go through all of the major concepts while covering the questions.

  1. Multiple Inheritance is not supported in Java. What is the reason behind it?

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.

  1. How does the String pool work 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.

Below is an example of the same. We create a String a with the value “abc”. A new String, when created,

  1. Scenario-based question: Would you rather use a String or char array for storing passwords?

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.

Tricky Spring Interview Questions

Now let’s analyze some of the most asked and tricky Spring Interview questions.

  1. What is Dependency Injection in Spring and What are its types?

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.

  1. What is ApplicationContext in Spring?

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:

  • By expanding the ResourcePatternResolver interface, you may load file resources.
  • Publish/Post upcoming events for subscribers (This is done via the ApplicationEventPublisher, which is an interface too.)
  • Resolve messages in favor of globalization (This is done via the MessageSource, which is an interface too).

  1. How to convert a Java class to Spring Bean?

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.

  1. Declare via XML

We can declare a Java class as a Bean via XML configuration. 

Example:

Declare via XML

  1. Declare via Annotations

We can also declare a Bean with @Bean annotations. This goes hand in hand with @configuration annotation.

Example

Declare via Annotations

  1. Manual configuration into Spring Context

We can manually configure any class as a bean by putting it into Springs application context. This way, we can reuse the bean.

Example

Manual configuration into Spring Context

Key Takeaway: Java and Spring Interviews

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: 

  • even if one does not know the answer, using common sense and giving it a second thought before answering is always better. 
  • Tricky questions will try to confuse you. So, think out of the box and think practically.
  • Be sure to go through Advanced Java concepts, Java  features, and Spring DI and IOC-related questions before the interview.
  • Java seems to be an OOP language but it is not a fully OOP language due to primitive data types. Always remember this in Java.
  • Spring is the parent of Spring Boot. All the annotations and configurations are abstractions of boilerplate code.

Most Tricky Spring And Java Interview Questions

Most Tricky Spring And Java Interview Questions

Most Tricky Spring And Java Interview Questions

Most Tricky Spring And Java Interview Questions