Java and spring - Interview Q&A

 

è  Spring interview questions 

Ø Marker interface : is the interface that doesn't has any constant or methods inside it.

So it is a kind of meta data about the class which tells what extra the class will do.

Example : Cloneable, serializable , repository in spring interfaces.




Using this we can override clone method from object class.

Ø Why we need marker interface?

Coz it adds extra features ,

Ex: if we want to override clone method  in above student class then it cant be done without implementing Cloneable interface, it will give cloneNotSupportException.


 è 5 most accuring annotations :

Ø  BeanCreationException: in case of circular dependency we get this exception . Ex A is dependent on B and vice versa so while creation of bean it will fail.

Ø  NoSuchBeanDefinationException : if the bean which is being autowired is not annotated with @Component / its bean is not instansiated in Spring container.

Ø  NoUniqueBeanDefinationsException : In case of multiple implementation of single interface we get this error .

Ø  BeanInstantiationException :

Ø  ApplicationContextExcepton : In case we forget to include @SpringBootApplication in springboot project.


    

 













String Constant pool ( or String Literal Pool ) : is an area in heap memory where java stores String literal values. 



So values for string literals i.e s2 is stored in SCP and since s1 is an object so here 2 copies of s1 object will be created one in SLP for future reference and one in outside heap.

the string literal present SCP are not applicable for Garbage Collection coz refrence variable is internally maintained by JVM. 

in case of s3 only 1 object will be created in heap not in SCP since it is already present coz of s1.
here for s4 only 1 object will be created SCP since it is a literal.
for s4 & s5 no new objects will be created since they are already created while creating s1 object .
SCP: contains unique values only.




String Objects/Literals are Immutable : Immutable means can't be changed 

means changes made in actual object using another string literals/object will not change the original object instead it will create new object and another string literal will get stored in SCP for future reference.


string buffer ?

string builder?












> Arraylist vs Linked List :

Arraylist : here fetch operations are fast so when we have frequent fetch operations there we use arraylist.

Linked List : here for insertion and deletion is fast so in this case where we have frequent insertion we use Linked List. 

StringBuffer vs StringBuilder : used to make String mutable we use String builder and String buffer ,as in java strings are immtable i.e change in original object of string is not done java created new object if change is done.

StringBuffer : Thread Safe, So should be use if we have multiple threads in our program.

StringBuilder : Not thread Safe ,So should be use if we do not have multiple threads in our program.

Collections: 



Spring :

>@Srevice / @Repository instead of @Component :

@Component is more generic
@Service: for service layer which makes it more specific/readable.

@Repository : is used in DAO layer for :
  • increasing readability for the developer.
  • also help in handling persistence related exception by parent exception class i.e Data Access exception.

> Bean Life-Cycle :




  1. we provide java bean/class along with configuration file for that bean to spring container.
  2. then the object of bean is initialized and properties for that is set by spring container
  3. then we use init() i.e @PostConstruct [config, connecting DB etc]
  4. further required/functionality code
  5. destroy() for clean up i.e @PreDestroy
 




Note: In a Standalone Spring application -

In main Class if we are manually stopping application using:

  • context.stop() : in this case it will not go to PreDestroy() it will directly stop the application.
  • context.close() : here it will call PreDestroy() before closing application.


> Rest Clients for project? ->using these client we can hit any rest API within our code ,Similarly as we do in Postman.

for example we can use Rest Template : 

by rest template We can use:
  • getForObject() -  will return only object as above image.
  • getForEntity()  -  will return many other values other that Object itself like body ,status ,headers etc as shown in image below, So this is the advantage while using ResponseEntity i.e we can fetch status from response also any many other values.

Similarly we get execute() ,exchange() method with restTemplate.


> Response Timeout : is used bcoz in case we have no response from server so that thread will get blocked forever so in case we have multiple threads calling to any server and not getting any response in that case we will need to specify timeout for each request's response. 
these threads are present in thread pool which are limited and blocking a thread for a longer duration can make response for other request delay too, so this will make it costly. 


  1. Server Timeout : in case while making connection to server and unable to connect .
  2. Read Timeout : in case when connection to server is stablished but unable to read data from server.


> Securing REST web service:



will have security configuration class in our API which will be extending to web security configuration adapter.

Request comes -> Authentication Filter ( it will give authentication object ) -> auth. manager builder -> auth. Provider ( will validate auth. object )  -> auth. manager builder (pass auth. object) -> Security context folder (so when user is trying to login again it will directly let them enter without asking for credentials)

Request will have tokens (JWT token) in header for uniquely authenticating user for token based authentication.

REST should be stateless that is no data should be stored by server sent from client system for further use so use JWT auth. instead of session based auth. coz in JWT we store token inside header of request.


> Content Negotiation : 


tells about the type of data that an API will consume and/or produce.


>Spring AOP : 

In AOP, aspects enable the modularization of concerns such as transaction management, logging or security that cut across multiple types and objects (often termed crosscutting concerns)

It does this by adding additional behavior to existing code without modifying the code itself.

The cross-cutting concerns help in increasing the modularity and separate it from the business logic of an application.


before executing makePayment() method of paymentService below method will execute.
done without making any changes in actual code of makePayment()

can use .. in makePayemt( . . ) if there are any arguments coming for the method.




 
















Comments

Popular posts from this blog

Durgesh - Exam Portal Project using Spring Boot and Angular