Java interview questions and answers for experienced | Live Mock | coding interview

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it's not a game [Music] all right so today we have a kill with us and a kill first of all you know we're gonna have a mock okay and as we have planned you know we have planned this interview i know for last couple of days it's not happening and let me tell you guys you know before we get started it's ocul's 12 maybe maybe uh 11 p.m in the evening and thank you very much for joining okay and i i just woke up so if anything is happening like in anything wrong is happening please forgive both of us all right so first of all yeah thank you very much akil for joining in so we'll make a very casual discussion no kill and i'll be asking you a lot of questions so make sure few things okay whenever you're answering okay just make a little bit of um you know kind of elaborate kind of answer so that makes sense to everyone and um basically the thing is that i want to take as much as i can from you right okay i mean i'm not going to judge you anything i know how good you are yes it's only the thing is that you know now we just have a normal conversation okay that's it cool so first of all tell me okay so i know um i know you were killed first of all you know before we get started let me tell you i knocked it pretty well because you know he has taken couple of bootcamp from me and i know him from a long time uh maybe for from last eight nine months okay so the thing is that i know what what what uh the things that he already has gone through and what his skills are and what our weaknesses are so basically um main three topics will be focusing okay will be java spring hibernate and a little bit of ms all right so tell me from which topic you want to guess get me started with yes so village we can start with java first of all java yes okay then i won't be starting with code let me start with spring [Music] okay so i have written few questions here okay for you okay just to make the interview pretty so let me first of all make it very very simple and let me ask you a very simple question okay first of all uh just introduce yourself a little bit like you know about your past work experience and everything yes yeah sure so i have around five years of experience working as a java developer and i've worked on various uh flavors of spring like spring boot spring mvc and some part of spring security as well and i've experienced working on restful web services hibernate gpa and worked on sql databases as well and i've also worked on microservice architecture and uh recently i'm working with a financial client perfect can i know your year stuff express five years of experience five years yeah all right okay okay so let us get started with a very simple you know question just to make it comfortable i know you can answer me this tell me what is the difference between ad component and ad service and at repository basically tell me the uses of all these things so basically these annotations are used to uh uh define an object basically we're giving the authority to spring to create objects for us so so for that we need to add those annotations in uh on the respective classes like service repository and controller so there are respective annotations for each of these classes like for controller we use address controller or we can use android address controller if you are working on a rest api and for the service layer for the service class basically we can have the annotation as address service where we will be defining the business logic and then for the dow layer basically which will be interacting with the database for that particular class we can have the annotation as added repository where we'll have we'll be adding those queries whatever we want to do with respect to the database perfect just tell me one thing you know if you have a dao layer right and if you are marking your doo with ad service or ad component so what do you think like you know uh what is the difference it's gonna make and why spring is recommending you to write at repository over a diao layer so can you just tell me the benefits of doing that yeah so basically if we write a added component annotation for dowel layer it will work fine so and but one of the advantages like it gives a user a readability view like uh he is able to guess okay so this is the repository class it will contain your dow logic so that annotation will say it says that all if you write it as address component so it's kind of a generic so no one will come to know what logic have you written there so just uh for a better visibility it's uh preferable to use android repository and also there is another advantage that address repository helps you to handle uh persistence related exceptions that spring provides so that's one of the advantage for using address repository but that advantage advantage you won't be getting if you you will be using at that component annotation you remember the root uh class that spring provides us for for handling the generic exceptions i mean for the for all the dao exceptions we have a generic class right yes then that class is data access exception exactly that's what i want to hear so basically you know any exception will be thrown by your persistence layer uh you know doesn't matter what kind of jdbc technology that you guys are using maybe it's a hibernate or spring jdbc or mybatis or whatever so any kind of exception it will be throwing spring will wrap that up with its own exception what do you call that data accessory yes correct perfect and um okay with ad service also you know just want to add one more point here okay with ad service also you know uh doesn't matter like you know how much feature ad service annotation is containing but if you have a service layer then of course you know we should mark that layer at with add service because maybe in future if spring comes comes with some new features with that service then if your class does have ad service then of of course we'll get those features and we don't have to make any questions yes correct hang on so the next question okay this is gonna be um okay first of all you just tell me about the bean life cycle a little bit okay then i'll just show you some code and it just will tell me like you know what will happen in some scenario best question okay first explain me a little about the bean life cycle what do you understand about it and yeah what are the annotations we have there yeah right so bean life cycle for bean lifecycle we have two annotations either at post construct and pre-destroy so what post construct does is like whenever your container is created your greens will be instantiated and then that particular uh method where you have annotated your uh basically the method which you have annotated with that post construct that will be called first and then further activities will take place and then at the end when you are the method which you where you have written the pre-destroy method that will be called just before the container is about to get destroyed and your bean is about to get out of the container so just before that your pre-destroy method will be called so giving you an example for that suppose you can use post construct while you're creating a database connection so you don't want to create your database connection again and again so that will be one time activity that you can have it in the post construct method your database connection will be established at once during the beginning and then you can perform the other database related activities therefore in your application and then the pre-destroy you can close your database connection before like just like a clean up work so that will be beneficial there perfect do you remember there is a couple of interfaces which does the same work as i mean that's in the xml or just same same as your at pre destroy and uh sorry free destroy and post construct we have couple of classes and if you want to see your spring's internal classes okay you're gonna see that those spring internal classes basically implements or extends to those interfaces yes there are some interfaces and disposable bin do you remember yes yes yes all right no problem same thing only okay so i have a question here okay so you said whenever whenever you have a application context right that's your container i mean whenever you are initializing the application context let's say that you know uh i am initializing the application context manually in a standalone application then of course i just said you know your post construct method will be called right yes yes let's say i'm manually stopping an application okay and it's a standalone application i'm doing context dot stop not close i'm using context dot stop to stop my application so do you think the pre-destroy method will be called in that case getting my question yes you're basically not destroying the container right yeah yeah basically i'm trying to destroy the containers so what do you think like you know uh the container will be destroyed i'm gonna show you some quarter kill that's gonna make more sense to you and for that so here we go you have a bean lifecycle project over here okay can you see my screen yes yes i can see it okay just see over here i have a bean here called my b just see the code that we have here okay all right a stack method okay with a log called stack method called with add push construct and a stop method with marked as add pre-destroy okay and it's a component so if i'll go as i mean by your definition and uh i will go to my app con update java so here i'm creating my application context you can see over here okay uh annotation config application context because i'm using annotation based approach and it's a standalone application not a web application not a springboard application so here if i go to my app config this does a component scan only and it's basically scans all everything starts with com so go back to your app.java so here i have annotation config application context okay and here i'm initializing my application so as for you if i'm going to start my application right now your is i'm gonna uh you know use this appconfig.java upconfig.java is scanning for all com packages so this my bin is also under the com. selenium express or something package so do you think this particular method will be called yes okay so let me go to my app.java and let me run this out and let's see that what's gonna happen well the method is called so bang on you are you are you you are absolutely on point now this is my question i'm going to do context dot stop so i'm stopping the context basically i'm wanting to destroy uh my container object okay okay so now my question is if i'm gonna run this okay if i'm gonna run this then what will happen yes please yes so basically you're trying to destroy your container but just before it get destroyed the post the pre-destroy method should be called yes so so you you are thinking your pre-destroy method will be called yes yeah but there is a catch that's why i asked you that because in the web application that will happen okay whatever you said but in standalone application see that your stop method did not get called so do you remember go back to your spring code days i know long back so the thing is that this is a standalone application okay okay uh any idea anything you remember about shutdown hook um no what is up now okay so let me tell you that okay if i'm gonna use context.close okay it's gonna do the similar kind of work sorry it's gonna close the container right it's also going to destroy the container object if i'm gonna do run this then whatever you said that's gonna happen and the stop method is called right and because the stop method is marked as at predestroy and you have explained me about that right yes but the thing is that i don't want to stop my application like this and this is not good actually in your normal application in standalone application is not good if you are going to close your application like this rather i'm going to tell my jvm uphill that whenever you're gonna exit or whenever the jbm is gonna exit at that time it also gonna destroy my application context object and to do that and to do that even i don't need to call the stock method i can simply do context dot register shutdown register shutdown hook if you remember that so this is basically a book which is gonna be telling the jvm that whenever you're gonna get cleaned uh make sure that is you're also going to clean your container and destroy that particular object so now if i believe if i'm gonna run this my stop method will be called there you go yes okay and also if you are calling your stop method like this no problem also right now also there won't be any difference over here where are the samples right okay i mean you know already all these things i just want to explain to everyone i actually i'm not explaining this to akhil uh maybe a lot of things he already knows maybe one line of answer will be enough but i just want to make sure that whenever we are talking with all these things we all are on the same page whoever whoever is even hearing this particular recording okay all right so uh let me close everything up here okay and i will just show you one more scenario okay so just tell me one thing okay in your normal spring applications okay like in your standalone spring application so we basically create our container object by ourself by doing something like this the code that i have shown you okay something like this but in web application do we create our i mean container object manually or how does that particular thing happen no no we don't create it manually spring takes care of uh upgrading the container based applications correct so basically when you say spring takes care of that i mean what exactly you mean for an example i'm going to show you the same thing the similar code whatever i have written here okay let me close this same code i have copy pasted here okay okay so see here source main java i got i have a bean my bean over here okay same class only start method marked with that post construct stop method marked with that pdestroy this method i'm not at all using okay okay and in the config file i have a app config the same file that i have shown you okay just a configuration a file who's going to scan my packages starts with com and a dispatcher software initializer which is gonna uh you know initialize the dispatcher software because it's a web best application okay so now my question is if i'm gonna start this application okay so right now i don't have anything i have a dispatcher subject file i have a configuration file i have a bin file okay so now if i'm gonna start this particular uh i mean if i'm gonna deploy this particular server kill in tomcat or in jboss so do you think your startup method will be called basically your uh post construct method do you think this is going to be called automatically yes it should be called automatic correct because the context will be created by dispatcher software itself right yeah spring i mean basically that's what you mean i will lift this patches up right right because we are starting the dispatches yes if i'm going to deploy this application then i believe that we should have this particular method called and start method is called right over here because whenever dispatcher sublet is initializing it is also initializing our container right and yes when the container initializes as we discussed earlier that you know our startup method will be called one more question here okay when the close method will be called or when that particular pre-destroy method will be called when you're stopping the server and your uh bean is getting destroyed correct so basically whenever we stop the server our dispatcher servlet is obviously going to be uh shut down and also it is going to destroy our container and everything so if i'm gonna stop my server so let's go there into the console just adding one more thing to this conversation if somebody is going to counter you with their questions like okay whenever you're gonna start your server your post construct method will be called and whatever that you have over there that will be uh you know executed whenever dispatcher servlet will be initialized as we are not starting the container by ourselves and dispatcher servlet is doing that for us and whenever we are basically stopping the server your pre-destroy method will be called whenever uh you are destroying your dispatcher servlet your container object will be also be destroyed and your pe destroy method will be called so who is basically helping you to do this if somebody counted you this question you just go to a class do a control shift key and go to a class called context loader listener and if you're going to go to this particular class this is the class which will help you in a web application to initialize your context and destroy your contact so if you're going to go back to this class i mean if you want to go down you can see that there is a method called context initialized and this method is going to help you if you're going to read the documentation that initialize the root web application context for you and we're going to see there is a method also there called context destroyed and this method is going to help you to close the root web application context so anybody is counting you like you know how it is getting happened behind the scene let him remind this particular class called context loader listener and this couple of methods that we have inside called context initialize for initializing a web application context and context destroyed to close uh the root web application context hope is making sense all right okay circle um tell me uh what is the rest client that you have used in your project so i have used rest template and also open fiend uh-huh client so all right so uh akil what is the use of i mean how you'll be using the rest template in your project and tell me what is the difference between get for for entity and get for object what is the difference between these two methods in your res template okay so firstly coming to rest template so suppose you have a rest api right and now rest template is nothing but a client so consume your data basically by hitting uh just like we hit a url from a postman right so similarly we get a response in json or xml format whatever we want to set so similarly in the code level we'll be using rest template to make our call to any api like a get http method uh post but whatever method we want to make a call and consume the consumer data or make any changes suppose we want to uh we have a endpoint as post and we want to suppose we want to insert a data in some other person's uh database so we'll he will be hitting that particular url from our rest line that is our s template and then we'll be making changes toward it inserting a record or database whatever operation we want to perform and we can do it via the rest template and then coming to difference between get for entity and get for object so get for object these are basically a methods defined in a rest template so both methods belong to rest template and coming to get for object it will return only your object basically what type you want to get your obj uh response uh and then get for entity it will return a response entity with the response entity you will have control of headers the body the http status whatever you want to catch so this is the benefit what we get from get for entity absolutely correct okay so do you know there is another method is there in your rest template by using that you can make a rest call another couple of methods are there i believe you have just specifically about the uh get for object and get for entity right couple couple more are there yeah yeah we have exchange method as well correct so same thing another one is there no kill called execute or something execute i believe but whatever exchange is another one right exactly that's another one yeah so akil um so whenever you're writing any rest api okay or do you want to just return any plain object directly or you want to use response entity and if you're using response entity class and you are returning a rest object or any object that you are trying to return then what is the benefits that you will be getting if you are using entity to return a particular object while you are writing any you know api yeah yes so basically with the response entity the benefit you get is like you can send the appropriate http status like uh like okay bad request whatever is it and along with that you can send the body okay so yeah so and you can also send your headers correct correct perfect okay this is a simple question for you for delete uh okay for if you're writing an api for delete okay so for delete um what kind of uh you know the stacker score you will be returning pretty simple now yes so it should be content not found container four two or four you can also return two zero zero is it okay if you're gonna return to zero zero for a delete request yes uh you can you can that's gonna be your personal choice anyhow all right okay so i have asked you about your ask about your rest template okay okay just explain me a concept so do you think we have to handle time out whenever we are going for a rest template and if you're thinking uh then why do you think so why do why it is important to handle timeout in your uh while while you are calling to any of the rest uh end point yeah basically basically when we are connecting to our server right so those connection are made with the threads so if suppose uh the connection is not being established so that thread will be particularly blocked so if one thread is blocked then another user is trying to access that particular end point and then that threat because connection is not being established so that another thread will also be blocked so again and again when the news the users are coming hitting those end points the threads will be blocked so we can so just to avoid that situation we have to do a timeout so that the thread is released basically so so in that sense so timeout is unnecessary correct okay just tell me one thing uh in a server if you talk about any kind of server let's say you are using tomcat server or jbl server or anything uh so can we have many threads inside the server i mean in the throttle and in the trade pool of the server can we yes you can have by the connection pooling okay no no i'm not talking about pulling i'm talking about this timer stuff only imagine i'm using whatever you said that you know we will have a server let's say we want to heat a particular endpoint right then obviously our server needs to connect to the other server that you are making a connection yes then obviously one thread will take that particular call and gonna make a call to the other server and of course if that particular server is not responding us back in a particular time and it is taking time for forever then particular that particular thread will be blocked right and that's what you said okay all these threads where it is available in the server you do you know this that was only the thing is that we we will have minimum number of threats okay maybe imagine we have 200 number of threats so we have to make sure with that 200 uh trades we have to serve everyone right yes yes very costly resource so instead of blocking one particular trade while making a call and if that particular web services is not responding us back then basically we can time that out okay yes do you know um there are two things in the timeout one is called read timeout and session timer one is server timeout okay so how how are you gonna differentiate these two things so server timeout is like you are not just like we were talking like we are not able to establish a connection to the server you're not getting a connection back so that will be a server timeout basically that's going to happen and really time what happens when we are able to establish a connection to the server but we are trying to read some data but at that time we're not get we are not able to read the data so we are not getting we are not getting a response back so that is a read uh time out that's gonna happen correct correct so the next question okay okay let me ask you some simple question tell me how you'll be what do you think about worsening okay in rest and how you'll be implementing versioning and rest so versioning means like suppose we are adding some new functionality to our api and we want that particular new functionality to be used by the users instead of the old one so it's likely to define that this is the like we'll define an api or that method with the old like a version we are giving that okay this is the old functionality so versioning can be defined with multiple uh things like using request parameters like uh and you can have it in the headers and uh yeah and yeah there's a couple of other couple of other things as well i think yeah okay so you told me that the version can be achieved by the params by the headers and by the uri okay so versioning means you know just a new version of that particular person perfect okay so here was the simple question for you so the question is how authentication in rest web services work and how will be making your rest uh web services secure okay so basically we'll be uh implementing security code we have will have a security configuration class for that in our api which will be extending to web security configure adapter and then we'll be overwriting we'll go into that uh you know don't make it so much technical just tell me how the authorization basically works okay uh okay how basic auth works let's just explain yeah okay so basically when we are hitting a request it will go to the authentication filter and the authentication filter will further uh uh give us an authentication object basically but that is not a validated object that object will be passed to the authentication manager builder the builder will further find the appropriate authentication provider the provider will basically validate the authentication object okay so there are multiple authentication providers like dow authentication provider or whatever like there are other as well we can define our authentication provider as well but basically what it will be doing it will be validating that particular authentication object then that authentication provider will further pass it object to the authentication manager builder then that builder will further pass the authentic object to the security context holder why it's passing to the security context folder so that when when the user is authenticating like when the user is trying to sign in next time it won't be asking for the credentials it will check check that on authentication object from the security contacts folder itself and then it will just allow the user to uh sign in without any credentials okay so okay got it i just want to understand one thing okay uh let's not go into the technical thing i know i know you are pretty good with that only one thing just let me know whenever you're making a simple rest collar kill okay imagine you know from from your postman or you are using your rest template or open pane to make a call yes so you are making a call and you are also passing something in the header right i want to hear that yes so basically we are passing we can pass tokens token-based authentication so we can pass a jwt token uh in our header and that will be will that will be further used for authentication purpose when whenever we are hitting a call to another system or another microservice so basically that token will be validated by the server it will authenticate that okay this is the token which i only generated or this is it carries my own signature because it it there should be a particular signature to validate that okay this is the authenticated engine deputy token and yes you can go ahead and access all other apis for all other end points basically whatever you want to access so hang on will be passing that token so that's about the jwd authentication token i am just going to come back to that in the basic authentication account okay in the basic authentication yes where you give the username and password yes and that particular you know value the username and password value with a base64 encoded format you have to send it by making uh use of the authorization header attribute yes yes okay so okay you have made me understand two things you you discussed about the spring security default implementation of course this is a session based implementation right yes and also you have discussed about uh jwt tokens just tell me one thing okay i mean what you will be using i'll be giving you an option or a chance to design some restful web services so which approach you'll be following like you know you'll be following the session based approach or you'll be following the jwt best approach and why yes yes according to me i'll follow uh uh token based authentication uh for in my rest apis the reason is that because those are those are stateless correct yes so by stateless i mean like that like the server will not be storing the state of your client side so it's like it's the responsible responsibility of the client to send all the relevant information everything in the headers so that the server is able to process that request so in that case i will prefer uh token based like a stateless uh authentication i don't want to go with a session based because in session which that is only particular to a particular session that is only restricted to a particular session only suppose if i'm trying to hit a request to another system okay that that system won't recognize my session right because that session is already built in my uh in my particular system only like in my system but that won't be uh authenticated by that particular system because the session is not built there in that system the session is in my system so that authentication uh will be tricky perfect perfect and also you know in microservices environment we scale uh our application isn't it yes we'll create many instances so if you're creating session then that session will be specific to a particular server right yes and in microservices environment we'll have many servers right so if the client is giving us the jwt token then every server will have our secret key or our public key or private key using that will verify the signature and we'll just validate the token so we don't have any responsibility if i mean as a provider so the the client will be giving us the you know authentication token right yes yes okay in the jwe token actually you just write something called br or bruh so what do you mean by that basically that's a what do you want if there's a qr why do you write that just like you know in the authorization header whenever you are sending a authorization token you write basic then you give the encoded value of that particular token right the username and password encoded value you have to provide in the authorization header by proceeding by basic right the basic is basically the keyword because you are doing a basic authentication just like that whenever you're doing a jw authentication you are writing uh b error right yeah so what is the why do you write that i mean what is the meaning of that i think beard means like uh you are the owner owner yeah you are the owner there is username or password yeah i mean there is no further validation is needed you are getting this token means you are the owner of that owner of it yes yes and there is no further validation required because you got that token because you have previously authenticated uh with our server once you give a request once you do a authentication with our server then only you get that particular token back right yes here goes the next question knocking when it when it comes to jwt is basically used used for authentication or authorization basically jwt is used for authentication authentication okay so think about it one more time yeah youtube yeah jwp is used for authorization authorization because whenever you'll be giving the username and password then only you will get back the authentic authorization token then you will be giving us that particular token and we'll be seeing that whether we have given you that particular token previously or not so jwt token you will be only getting once the authentication has been done then you get the token back and you will be keep giving us that particular token authorization purpose perfect so the next question what is content negotiation yeah so content negotiation means like what kind of content you want to accept what like what kind of content you want your api to accept and what kind of content you want to get a response from your api what kind of content you want your api should produce basically to send basically as a response so it can be achieved via media type like produces or consumes in your api so media type produces means what your api want to produce like the format does it wants to produce xml or json or any format what you want the api to produce and then comes the consumes consumes will further uh give us the like allow us what api can uh consume like uh what format xml or json so what format like whatever format you want api to consume you will be using consumes and for producers that will be extermination whatever you want hang on okay i will hire you right now okay but you know i don't have any vacancies for you okay so the next question uh so the next question akhil is tell me about the cross cutting constants in spring and how do you want to implement that in your spring project yeah so cross cutting concerns basically belongs to the spring aop okay and uh yeah so basically those are basically used for logging purpose when we want to uh log something like a global log or uh or we want to lock uh uh basically we want to lock after this method we can we can have those control basically with this cross cut and we can define a method name whatever like a parameter we can give and we can perform some operations with those across using this cross cutting concern perfect it's not only uh the log okay perfect yeah logging is one option uh monitoring or security right after one method i want my security to happen i want the authentication to happen so you can put their things um as a cross-cutting constraints only and they just said we can implement that by using spring eop okay i will be going in depth on that i know you can keep me keep explaining that particular things okay all right so the next question okay okay in microservices environment okay listen to this question very carefully in microservices environment let's say you want to implement all your cross-cutting concerns like your logging your tracing monitoring security whatever so where you will be implementing these things in which label i mean in your i mean in how you'll be implementing these things at the gateway level perfect just answer i want to hear yes perfect um this is fine okay the next question do i have any hmm so what how do you create custom validators in spring custom validators so you you have to extend to you to implement some class like custom validate whatever yeah yeah and then you can have those annotations at the right retention and the runtime and all such annotations which are required and then there is a method which has to be overwritten there you have to define your logic what basically you want your how do you create annotations in java using which qr class interface annotations annotations how do you create annotations at a target target at target at runtime and everything you have to specify over your class yes yes and i mean just like you define a class you're writing class class name right in annotation how do you do that yeah i'm not typical at the right interface okay edit interface okay [Music] all right okay so the next question so uh let me just go to forza by a little and then we'll come back again to spring or hibernate one more time just tell me a little about a kill about the hashmap internal if you know about it do you know about hashmap internal yes so basically that or i'll choose my question no you can go ahead yeah so basically hashtag contains two important methods equals and the hash code what equals method does it compares the objects based on the key value and it belongs to the object class the method and make sure if you're overriding the equals method you have to overwrite the hashcode method as well so hashcode method returns the memory reference of the object basically it gives us a bucket number where that object belongs to and what are buckets buckets are nothing but array of nodes they are called buckets and each node has a linky structure in hashmap and there can also be a possibility that two nodes can share the same bucket number so this is how hash map works internally based on these methods and linked list data structure i can counter any question on that right now but let's not do it because i'll be getting into the list part of it i think you can uh basically handle the hashmap questions let's talk about the list but maybe that's something we never focus right so let's talk about least tell it so i will have few questions right now okay just see your screen and just let me know the answer about it okay the first question that i'll be asking you on list okay can we just just to you know just to be on the same page can you just quickly tell me about the you know the differences between the list and set quickly yeah yeah sure so list maintains the insertion order set doesn't does not maintains any insertion order and uh list uh basically the implementation classes for list are array list linked list and all but for set it is like a headset or linked headset so those are the some of the implementations and also uh set doesn't allow storage of duplicate elements whereas list can allow you to show duplicate elements so these are some of the differences here okay this question is just to get you set okay i know it's pretty easy for you so uh okay so i'm gonna show you some things right now let's talk about least a little and let's talk about some interview questions and uh let's see that how how well we can you know complete these things the first question that i'll be asking you okay see the first lineup first few lines of code that i have here maybe i can just uncomment this just try to see the core from the lines that i'm on commenting and and i will be just giving you a simple piece of program just tell me how this particular things works okay for an example i have a list here okay a list of strings okay and here i'm initializing my list with two i mean with the capacity two all right so right now i'm adding the first element the second element okay so now i'm adding the third element so what do you think it's gonna it's gonna take the third element and add it to the bucket because initially i'm specifying the size as two so what about when the size will be pulled because this will this will be the first element this will be the second element when i'll be adding the third one do you think we'll get an exception or you know it should be it should be added it should be added because why why can you go ahead please because it dynamically creates the assign like a size in their list exactly can i start it in debug for a moment okay yes yes so um okay one second so by this time our list size should be if i'll go to here okay and if i can drag and drag my co i mean variable view and if i'll go to the uh if i'll do so logical structure right over here okay the object size is two right now initially we have started our release with number two so if i'll go into that i have a couple of variables and uh i mean a couple of variables are already assigned with zero and first uh indexes right now my question is when i'll be adding the third one then what will happen obviously if i add the third one then obviously as you see the arraylist grows and obviously you know yes i just said it will be automatically grow and you know it will trick for us perfect no noises so now it's list to my second question and after that i'll go into the internal the second question goes like this okay i'm gonna commend these things out okay and just tell me just see the score okay and just familiarize yourself with this piece of code so what i'm doing here actually i'll tell you the purpose of this particular program first then i'll just walk you through with the code so listen to me so here i'm trying to add uh let's say one million object okay a string object to a particular list okay okay i will be adding one million object imagine using uh the rls constructor and specifying the one million object inside the rls constructor for one time and another time i won't be specifying one million data in the rlist constructor for an example right here you can see i'm creating an error list and i'm not specifying the capacity or the rlist initial value that i should be initialized or a list with okay so by default you know that how many numbers of elements it will be 15 maybe 10 yeah the initial capacity i believe it is 10. so right now i'll be keep padding so you can see i have a for loop here i'm going to keep adding the data to this name list right you can see nameless dot add and this loop will continue for let's say i don't know how many zeros i have given over here okay so those data will be added to this name list so what i'm trying to do over here i have a start date over here and i have a end date over here i just initialized couple of date instances and i'm finding the time that it has taken to uh in short all those data to my area list okay my question is here okay first time whenever i am i'm initializing the error list i'm not specifying any data over here and i'm gonna run this particular application so first of all i'll resume this the previous one and now i'm gonna run this okay now see how much time it's gonna take it's taking six one three millisecond all right here goes the question if i'm gonna copy this and if i'm gonna put it over here okay so do you think it's gonna be work faster slower or what do you assume think about it take a minute and then answer me do you think it's gonna take less time to insert the data right now as i'm specifying the capacity or it's gonna take more time i think it should take you more time why is that okay i know i uh i think it it will take you less time because you're specifying the space already uh like a concrete yeah it doesn't have to look over it in the memory uh or some random space basically when you are not allocating that particular size so basically of course if i'm gonna run this so you will see that it's gonna take very less time significantly less and less 193 milliseconds previously it was 600 something okay and of course the base reason is that the way the other list has been implemented all right so basically as we have discussed a little if you if you are you know go to the area list um you know let's say any analyst constructor all right in the rls class if you're gonna see the implementation of this add method right this is the private method let me you let me go with the odd method that we are using and this is the odd method that we are using okay and you know that how this add method works right first of all it is calling it another add method internally and this add method does what is gonna it's gonna check always like you know whether um you know the size of the array is basically equals to the element data length like you know if you have 10 data 10 elements in your other list and again you are inserting i mean the other list is full let's say you have 10 10 uh elements in our list already and again uh though you have already inserted 10 elements to the other list and it's gonna check the size and dynamically it's gonna grow as you have told me already so there is a grow method here and this grow method is basically assigned a new capacity and the way this grow measure works is calls the grow method another grow methods internally and if you're going to see over here this is basically where it is basically assigning the old capacity i mean it's basically assigning the new capacity and basically is the way it is assigning the new capacity by doing a right shift operation basically it does old capacity right stripped of one so basically that's that's basically means that what is your current capacity let's say your current capacity is 10 then it will do 10 divided by whatever your right step to 1 that means 10 divided by 2 so 10 divided by 2 is going to be 5 okay then it's going to be 10 plus 5 is going to be 15 is gonna be your new capacity the next time again 15 divided by 2 so it's going to be 7 so 15 plus 7 is gonna be 22 is going to be the new capacity okay that's how the thing works so the way it's gonna work and you can see in the very next line it is doing arrays of copy off okay and it's creating a new array by copying the previous array isn't it okay i think you know about this right if you remember the way the stack implementation and with you design the dynamic array you basically copy the elements of the previous array and then you have put those elements in the new array and the previously previous array will be eligible for garbage collection isn't it yes yes so so so in that case if you have lot of elements right in the same time if you are specifying the capacity here whenever you are initializing the array list then you don't have to copy the elements from the previous array to the new array and then you you don't have to do that again and again and of course that's going to save a lot of memory for us isn't it actually right yes so that's why if you're gonna initialize this with no element then again it's gonna be dynamically growth first it's going to be 10 i believe so i'm going to put a breakpoint over here if i'm going to do uh debug as java application just quickly if i'm going to debug it i mean just to see like you know what the default capacity is and if i i'm going to do a step over step over so step over step over and this is going to be my name list let me open this the default is going to be 10 right now but i am sure if it is going to be after let's say i'll be adding 10 elements here 7 8 9 10 then it's going to be 15 the new size yes because this is going to be right shift of 1 then it's 10 divided by 2 is going to be 5 so 10 plus 5 is gonna be 15 and after this is gonna be 22. uh after 15 is gonna be 22. i believe so yeah there you go right and so basically a new array is getting created the data get the data are getting copied and you know blah blah blah you know about that perfect so yeah let's not dig into this pretty much that's fine uh let me stop this account we are done with this all right so can we go to the next question it's gonna be pretty simple and straightforward first you tell me your view about the fail fast and the fail step operator oh sorry fail fast in the failsafe stuff uh not how do i frame this particular question pale past and pale step collections uh in um java okay not stuffs stops does mean something else you know recently that susantra's sushan singh raspberry remember this hard news like you know stops means something else in bollywood i'm not talking about that stuffs right so yeah so talking about fail fast and fail safe basically these are the iterators so basically uh uh talking about fail uh fail fast first so fail fast iterator is being used by arraylist or the linked lists so what basically happens in this during the iteration if you are trying to perform any modification operation like in adding an element or removing an element you're basically playing with existing structure of a list so that it will give you an exception that is concrete modification exception okay so that those are the fail fast uh operators coming to the fail safe ipad iterator just the name say safe so it means it is thread safe so it means like if you're so the example could be copy and write uh error list it could be and congruent hashmap those uses the failsafe iterators in that if you're trying to perform any modification operation to the during the iteration so it won't give you that concrete modification modification exception it will work perfectly fine so this is uh why it is failsafe for iterators and those are called fail fast iterators the ones used by the idealist perfect hang on and i will just go with a program in the last stop i mean at last we'll just see a program and wrap this particular conversation okay but i uh i mean i don't have any questions on this so we're good so okay just tell me one thing uh if you talk about fail uh fast okay and the fail step which is gonna consume more memory the fail faster the fail step uh you know collections uh fail safe will consume more memory why is that because uh just like talking about copy on right arrow list so basically the operations if you're trying to perform those operations will be performed on a copy not on that particular array list it will perform on a copy so that for that copy you will require more memory correct so basically it won't perform the operation in the original collection isn't it collect correct right correct perfect all right okay so this is the last question i have asked you about the fail fast and the fail step collections and let's look at some classes that i have written over here and i'll be asking you a few questions okay so i have for each test a class over here and let's see what i'm trying to do here and i'll be asking you questions so you have to let me know what will be the uh you know the things that you are expecting from this particular program okay okay just familiarize yourself with this piece of code okay so basically what i've done here i'm creating a area list okay then i i'm using rs.list and i'm having few here yes okay and then it's gonna return me a list of uh you know list of strings because obviously i'm putting some strings over here and i'm gonna get a list of string and i'm adding this list of string with the serialist okay so i don't have to add those things one by one by doing odd art right so in one shot only i'm adding all these elements to my name list all right yes so now we can see i'm iterating over that name list this particular uh collection that i have and i'm printing those variables right yeah now my first question is while i'm printing it you know that we have many ways we can paint and collection we have stream we have for each we have iterator and a lot of different things my question is if i'm going to uncomment this particular code obviously if i'm going to run this first let me just show you by running this application it's going to print all these players that i have search in store of rahul yuvraj viru and lakshman okay so now look at this while i'm iterating over this name list if i'm gonna uncomment this nameless dot remove then what do you think what's gonna happen yeah it will give you an exception concrete modification exception perfect bang on yes let me just do a run is java application and it's gonna get me the concurrent modification exception because i'm trying to you know modify the existing list file iterating right and of course you know that is not possible perfect so i'm going to remove this uh remove remove from here so basically right now this just lists me to my next question or okay so while iterating i'm not getting any option to remove or add or and change any kind of um any kind of thing with this with this particular uh collection so let's say if i want some feature like you know if i want to remove something from this particular collection while i'm iterating it then any anything um available for that any any kind of support that we have in java yeah blush you can use it correct so basically yeah if you have the iterator activated so if you perform that remove operation using the iterator for that arraylist then you will be able to remove it perfect i have written the code for you already look at this code and familiarize yourself so i'm doing namelist.iterator so you can see i have the name list over here we've got we've got all these players because with this name list i've added everything right okay so now with this name list i'm just getting an iterator over here and then i'm checking whether uh the iterator um is actually having any next element if it is having any element i'm just getting that particular element and if the name is sort of okay if i have a player in my current player if it is sort of then i am removing that particular player from the iterator so do you think right now sourav will be removed from the original list which is nameless yes it should be removed because just just like i said if you use it with the iterator and you will be able to perform the remove operation that operation here correct so you are saying that if i'm going to do this if i'm going sorry i don't need to do this if i'm gonna do this right now if i'm gonna again iterate right over here on the same collection which is nameless then things should work fine isn't it yes yes okay let me do ctrl s i can do right click run his java application and you are thinking right okay i should have done something right otherwise i would not have asked you this question uh okay we're still getting a concurrent modification exception why is that so did i okay maybe because of this remove uh inside the forest loop but i will get something else and i want to ask you that question and uh just take a little bit of time service removed right sort of is removed from here because that's the iterator behavior okay okay okay just tell me one thing this rs.sllist also giving me a list right this is called list of names yes yes and this list only i'm adding it to the original list correct this is also a list right so can i do list of names dot iterator is it gonna work in the same way and from this collection it's just gonna remove sora i think to get confused this is obviously yes yes yes yes it should work right if i'm gonna do control s right now if i'm gonna do run is job application see what's gonna happen and tell me why okay giving you unsupported operation exception removed i think that iterator is specific to that original list and uh it's not working with a a new list okay what what's what's the issue with rs.slist if you remember ras.slist is gonna get us a unmodifiable exception sorry iris.list is gonna get us and not modifiable collection it's not modifiable collection isn't it does it basically give a immutable collection back and that with that collection we will not be able to uh what do we call that uh we cannot change this collection if you're gonna build the collection with artist at sllist and the solution for this um maybe you know you can just put this rh.s list with a linked list new link list sorry control s first issue import link list okay now i have to specify say list of string okay now is now i'm building a new link list uh with this particular elements and i think right now this is going to work fine because with this list of names which is going to be this one there won't be any problem and it won't be giving any kind of error like this because that rs.list only was causing the issue because it was returning away on modified uh collection right so now if i'm gonna run this this should fix the issue correct there you go okay forum is not removed so because this is here i'm printing from the name list i should be printing it from the list of names ctrl c ctrl v ctrl s run this okay cool um all right so that's it and uh okay i just want to uh know one more thing okay so let's say i'm let me just report back my code to whatever it was okay then i'll just ask you one one or two more questions and we'll wrap up so name list okay everything is fine ctrl s okay so now if i'm gonna run this thing should work as usual and um i mean okay things are working as usual i'm printing my list over here and i just reported back my code i have one question for you okay right now while i have created the iterator over here i'm creating the iterator over this name list this name list i have defined over here okay now while i'm iterating over this particular collection let's say here i am trying to um you know add something to my collection if i'm gonna do uh my collection which is gonna be nameless let's say with this name list i'm trying to add one more element let's say kef so do you think it's going to work think about it for the name list iterator i'm iterating on yes yes sure are you sure you're using no no you should not you know it should not work why because you're trying to modify you're trying to modify the structure of that modification exception okay there you go how i'll be fixing this this is my need right now and i want this cap to be added uh to my name list so if this name list if i'm gonna do a control click this is gonna be a uh this is right now defined as a list of string so how i'm gonna fix it okay yeah okay so you can so you want to use error list only uh yeah i want to use rlist but i don't want this concurrent modification exception i want to change my collection even while i'm iterating on this maybe you can use copy and right arrow list correct that's what i want to hear copy on right arrow list copy this paste it over here and there you go this should fix the issue unsupported operations um why is that okay so the problem is here okay you know i have not uh commented out this player iterator dot remove okay so let me comment out this player iterator dot remove and let me just try to run this particular things and i think it should work right now even we are adding um the uh you know new element to our existing collection which is named list still these things are working because this name list has been defined as a copy on right at the list of course we're gonna change this arraylist to your normal arraylist then of course the things will not work as we know that this is not a concurrent collection and the normal collection that we have are fail fast so any kind of changes that you will try to do whenever you are iterating over the element then of course it's going to throw and throw us an exception called concurrent modification exception and to make it fail safe you have to use uh the concurrent the collection from the concurrent package isn't it yes all right um okay uh can you just tell me or kill this concurrent uh collections that we have like you know copy on write our release concurrent hashmap and all these things in which package it is in notice i'm not expecting any answers just can you just recall if you can concurrent package i believe again okay because these things are only there just to help us in a multi-threaded environment right all right okay we'll talk we'll see some programs in the end of the session uh sorry not in the session and the interview today i mean so all right good so okay so okay the next question um i'll be asking you about the single turn design pattern can you just tell me what do you understand about single turn design pattern and i want to link that particular thing with a spring just tell me what is the difference between single turn design pattern you first explain that then tell me the difference between you know the gof singleton design pattern and the spring scope there is a scope called singleton right yes can you please tell me about that yeah yeah so basically a singleton pattern is nothing but the in java you can only create an uh object for a particular class only once no more objects can be created for that singleton class okay so and coming to the uh spring singleton uh like the scope what we have it's like one object per spring ioc container so it means like that only one bean can be created for that spring ioc container no more beans can be created but in java we can we can only have uh one of uh one object uh but that is not uh limited limited to the container just like we're talking about spread so so in the entire run time what you are trying to say yes there will be only one and only one object if you are implementing the single turn design pattern correct correct but in uh spring is gonna be one for container right we're gonna make the scope a singleton okay yes one for application context what we can say yes perfect um and how can you make your class single turn and now and how can you okay the breaking part i want we will be discussing later maybe people who are hearing this recording or hearing this particular interview make sure that whenever you are answering this question the counter will be you know how many way you can break these particular things and we can have a 20 minutes talk on that but let's not go into that because it's going to be a very long interview then just tell me okay uh how will we implement the single turn design pattern in java yes so basically for implementing singleton design pattern you will be requiring a private constructor and then you will be needing a static method with a return type object object with the return type update class basically which will give you the object for that class and uh yeah basically these are two main things basically private constructor and that's static method matters which will give you that object back and the type should be same as the class okay can you just tell me one example uh in your spring or hibernate or whatever the framework you have used when you have come across a single turn uh class basically uh uh we can talk about the password encoder that uses a singleton pattern which is password encoder bakery yeah no password encoder just confusing you yeah no off is the right answer okay so yeah okay don't go by me okay okay so the next one is okay just tell me um a couple of examples of factory design pattern uh that you have come across in your framework that you're using currently in your project just like spring or hybrid couple of instances where you are using factory yeah so yeah basically a factory pattern is being used by session factory instance basically when we are trying to get the uh session object from the session factory so there that particular uh thing is using the factory design pattern basically session factory uh the name says it all so from session factory we'll get the session object and then we can perform at uh crud operations in spring in spring uh in spring i'm not able to been in factory bean factory yeah yeah just now we have discussed containers and all right yes a very simple example is one of the bean factory or you're you are doing get bean and is giving you the object of that particular class whichever class you are demanding so of course cool uh we are good with this so the next question okay i'm running out of questions right now so yeah we're good so kill you know i will just do a few things and we'll wrap up this interview uh i just want you to give me a complete internal deep dive of how the authentication the form authentication works in spring security by sharing the code in your machine and letting me know everything step by step and i'll be counting you on that particular thing so could you please share your screen yes i just want to know like you know how a form-based authentication will work uh in spring framework whenever you will be using spring security and just let me know about that yeah sure so let me share my screen take your time please i think it is disabled the party's dependent i'm just giving you the permission so imagine you know i have spring security implemented in this particular project and i get a login screen i enter my username and password okay on the screen then how the flow will go internally right so let me enable the spring security first in my palm the starter sure so whenever uh the user will be hitting the request a security login page will be populated just like a form-based authentication and whenever you are submitting the credentials okay so that your request will go to your uh yeah using a password authentication filter let me show you password authentic username authentication filter your request will come here in this class okay all right so then in this class your uh this method will be called attempt authentication so here it it will obtain your username from your form what you submitted your password okay and then it will generate authentication object with this username with the implementation classes username password authentication token authentication is nothing but an interface but it has multiple implementation classes and this is one of that class okay so it will create an authentication object it will go to the authentication manager as you can see authentication manager will further call the authenticate uh method of the particular authentication provider class okay so let me go here open implementation on authenticate and this provider manager one second one second one second okay i just have a question over here so you said your username and password will be entered by the form right yes and where you are capturing those things in which line and in the line number 77 and line number 80. okay and with those two uh username and password you are constructing the username password authentication token and you are validating that token by by the help of the authentication manager right correctly okay so now now okay so now what is the implementation of the authentication manager in spring the default implementation authentication manager for spring uh that authenticate method that you will be calling right authentication provider can you just show me the installation yeah that's so what is the work of the authentication manager can you just click over there provider manager authenticate method let's say it's gonna take your authenticate of authentication object the authentication token object then what will happen from here yeah so basically it will uh it will look over that the appropriate authentication provider that will be further validating your authentication object if you can see there is a loop going on for checking the appropriate authentication provider so the role of the authentication manager is to get the appropriate authentication provider for you there can be multiple providers even you can create your own provider for authenticating for authenticating a object okay so it will check for that supports method in the provider class so if it gets the uh if it if it doesn't matches so it will come out of the for loop so what is that flow test line number 174 can you do a control click on the two tests two tests yeah sure so it's basically uh checking that uh uh the type of the authentication right i mean the class name what's your last name yeah so there what class we'll have because we are getting the username password authentication token isn't it so the class will be username username password authentication token right and that's what you're gonna you're gonna send it to your provider so which is the default provider that we have in spring security we'll be having a dow authentication provider the authentication provider correct and what the authentication provider will do can you go to that can you this uh so where if that supports right the flow will come to where let's say if you're repairs finds your authentication manager einstein come here okay so basically you are saying line number 182 okay yes so can you just find the implementation of that provider authenticate method so just show me uh which particular absolutely i mean class is that you know logic the authentication logic yeah it should be abstract user details authentication provider correct correct yeah and here how oh no come down yeah how that authenticate method is basically working we're basically loading the things yeah you just scrolled it down uh can you go up a little yeah authenticate method yeah there you go like you know where it uh because right now you're inside the abstract user detailed authentication provider so you said you know dow authentication provider so when the dao authentication provider will be get called yes yes so basically we are we are in the abstract user details authentication provider class now so it will look over the appropriate provider for it which is the daw authentication provider so from where can you find out the authentication provider is from this line my number 133 user retrieve user can you just go to the retrieve user implementation yeah sure so you will be going to the dar authentication provider and you can see yeah the authentication provider and now here we are using the load user by username so basic which basically loads our username okay so right now uh how many how many types of user details service so it just comes free with your spring framework i mean how many types of user data service i mean there are so some default user details manager provided by the spring security what are the memory authentication jdbc authentication and uh you can also create your own user details but um there is also one of that along with that in memory right right so okay so let's say whatever those are whatever the things that i have registered in my configuration file whatever the service that i have register may be user details or in memory or your own uh user data service then you are saying that that load user by username method will be called that will be called and it will basically get you the username from the memory maybe from database from ldap or from your wherever you have that and then it will be uh get you the actual user right and that user will be again validated by your manager yes yes all right perfect yes uh good job okay that's what i wanted to hear [Music] all right so that's it okay thank you very much and uh i mean it's glad talking to you it's been one hour and a half and uh yeah so i i just i don't want to i don't want to thank anything to the people so basically in between our call got interrupted for some time and we have researched the same thing maybe um you know after a few hours restoring screen recording file recovery in progress [Laughter] [Music] so yeah we have sued this particular things in couple of shots but i believe this particular uh you know interview is gonna help everyone okay and maybe i'll just try to invite you one more time maybe after a couple of months once we're done with the marwan stuff and we'll talk about some micro services and docker and some more advanced thing and we'll see like how the things cost you want to say something right yeah thanks a blood it has been a pleasure talking to you and uh the boot camp i got from you from the last couple of months it has been really helpful for me and yeah don't promote my courses okay thank you very much thank you very much for saying that okay yeah but you know i mean it's been a really great uh talking to you as well okay actually you know my main intention was you know whatever you guys have learned for last few months um maybe from me or from i mean the project that you are working with we just want to have a healthy conversation so that everyone you know who will be watching this recording at least they will be finding it helpful so thank you very much for this but maybe we'll have it one more time once we complete uh your the the current bootcamp that you are attending uh it's been a pleasure okay and that's it thank you very much thank you again bye nice talking to you thank you bye [Music] you
Info
Channel: Selenium Express
Views: 96,501
Rating: 4.9313769 out of 5
Keywords: spring interview questions, java interview questions coding, java coding interview questions and answers, collections in java interview questions, java interview questions for experienced, java interview questions experienced, java interview questions programs, java interview questions, core java interview question and answers for freshers, top java interview questions and answers, top 10 java interview questions, restful web services interview questions, selenium express, javamock
Id: yX2w-Sof95s
Channel Id: undefined
Length: 78min 53sec (4733 seconds)
Published: Sun Aug 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.