tag:blogger.com,1999:blog-72547146384874565822024-03-13T05:17:41.309-07:00devlopersinterviewquestionsUnknownnoreply@blogger.comBlogger5125tag:blogger.com,1999:blog-7254714638487456582.post-69760659346780738682009-07-20T05:50:00.001-07:002010-01-08T04:01:52.222-08:00Servlet Interview Questions<span style="font-weight: bold;">Q. How would you get the browser to request for an updated page in 10 seconds from the server?</span>
<br />response.setHeader(“Refresh”, 10);
<br />Refresh does not stipulate continual updates. It just specifies in how many seconds the next update should take place.
<br />So, you have to continue to supply “Refresh” in all subsequent responses. The “Refresh” header is very useful because it
<br />lets the servlet display a partial list of items or an introductory image to be displayed while the complete results or real
<br />page is displayed later (say in 10 seconds). You can also specify another page to be reloaded as follows:
<br />respose.setHeader(“Refresh”, “10;URL=http://localhost:8080/myCtxt/crm.do”);
<br />The above setting can be directly set in the section of the HTML page as shown below as opposed to setting it
<br />in the servlet. This is useful for static HTML pages.
<br />
<br /><span style="font-weight: bold;">Q. How would you get the browser to request for an updated page in 10 seconds from the server?
<br /></span><span><span style="font-weight: bold;">A:</span> response.setHeader(“Refresh”, 10);
<br />Refresh does not stipulate continual updates. It just specifies in how many seconds the next update should take place.
<br />So, you have to continue to supply “Refresh” in all subsequent responses. The “Refresh” header is very useful because it
<br />lets the servlet display a partial list of items or an introductory image to be displayed while the complete results or real
<br />page is displayed later (say in 10 seconds). You can also specify another page to be reloaded as follows:
<br />respose.setHeader(“Refresh”, “10;URL=http://localhost:8080/myCtxt/crm.do”);
<br />The above setting can be directly set in the section of the HTML page as shown below as opposed to setting it
<br />in the servlet. This is useful for static HTML pages.
<br /></span>
<br />
<br /><span style="font-weight: bold;">Q. What are the two objects a servlet receives when it accepts a call from its client?</span>
<br /><span style="font-weight: bold;">A:</span> “ServletRequest”, which
<br />encapsulates client request from the client and the “ServletResponse”, which encapsulates the communication from the
<br />servlet back to the client.
<br />In addition to both HTTP request and response, HTTP headers are informational additions that convey both essential and
<br />non-essential information. For example: HTTP headers are used to convey MIME (Multipurpose Internet Mail Extension)
<br />type of an HTTP request and also to set and retrieve cookies etc.
<br />Content-Type: text/html
<br />Set-Cookie:AV+USERKEY=AVSe5678f6c1tgfd;expires=Monday, 4-Jul-2006 12:00:00; path=/;domain=.abc.com;
<br />response.setContentType(“text/html”);
<br />response.addCookie(myCookie);
<br />
<br /><span style="font-weight: bold;">Q. What can you do in your Servlet/JSP code to tell browser not to cache the pages?</span>
<br /><span style="font-weight: bold;">A:</span>Another useful header is the
<br />Cache-Control as shown below:
<br />response.setHeader(“Cache-Control”,“no-cache”); //document should never be cached. HTTP 1.1
<br />response.setHeader(“Pragma”, “no-cache”); //HTTP 1.0
<br />response.setDateHeader(“Expires”, 0);
<br />
<br /><span style="font-weight: bold;">Q. What are the different scopes or places where a servlet can save data for its processing?</span>
<br /><span style="font-weight: bold;">A:</span> Data saved in a
<br />request-scope goes out of scope once a response has been sent back to the client (i.e. when the request is completed).
<br />//save and get request-scoped value
<br />request.setAttribute(“calc-value”, new Float(7.0));
<br />request.getAttribute(“calc-value”);
<br />Data saved in a session-scope is available across multiple requests. Data saved in the session is destroyed when the
<br />session is destroyed (not when a request completes but spans several requests).
<br />//save and get session-scoped value
<br />HttpSession session = request.getSession(false);
<br />If(session != null) {
<br />session.setAttribute(“id”, “DX12345”);
<br />value = session.getAttribute(“id”);
<br />}
<br />Data saved in a ServletContext scope is shared by all servlets and JSPs in the context. The data stored in the servlet
<br />context is destroyed when the servlet context is destroyed.
<br />//save and get an application-scoped value
<br />getServletContext().setAttribute(“application-value”, “shopping-app”);
<br />value = getServletContext().getAttribute(“application-value”);
<br />
<br /><span style="font-weight: bold;">Q. Which code line should be set in a response object before using the PrintWriter or the OutputStream?</span>
<br /><span style="font-weight: bold;">A:</span>You
<br />need to set the content type using the setContentType(…) method.
<br />//to return an html
<br />response.setContentType(“text/html”);
<br />PrintWriter out = response.getWriter();
<br />out.println(“…….”);
<br />//to return an image
<br />response.setContentType(“image/gif”);
<br />
<br /><span style="font-weight: bold;">Q : HTTP is a stateless protocol, so, how do you maintain state? How do you store user data between requests?</span>
<br /><span style="font-weight: bold;">A :</span> This is a commonly asked interview question. The “http protocol” is a stateless request/response based protocol.
<br />You can retain the state information between different page requests as follows:
<br />HTTP Sessions are the recommended approach. A session identifies the requests that originate from the same
<br />browser during the period of conversation. All the servlets can share the same session. The JSESSIONID is
<br />generated by the server and can be passed to client through cookies, URL re-writing (if cookies are turned off) or
<br />built-in SSL mechanism. Care should be taken to minimize size of objects stored in session and objects
<br />stored in session should be serializable. In a Java servlet the session can be obtained as follows: CO
<br />HttpSession session = request.getSession(true); //returns a current session or a new session
<br />//To put/get a value in/from the session
<br />Name name = new Name(“Peter”);
<br />session.setAttribute(“Firstname”, name); //session.putValue(…) is deprecated as of 2.2
<br />session.getAttribute(“Firstname”);//get a value. session.getValue(…) is deprecated
<br />//If a session is no longer required e.g. user has logged out, etc then it can be invalidated.
<br />session.invalidate();
<br />//you can also set the session inactivity lease period on a per session basis
<br />session.setMaxInactiveInterval(300);//resets inactivity period for this session as 5 minutes
<br />
<br /><span style="font-weight: bold;">Q. Session tracking uses cookies by default. What would you do if the cookies are turned off?</span>
<br /><span style="font-weight: bold;">A:</span>If cookies are turned off, you can still enable session tracking using URL rewriting. This involves including the
<br />session ID within the link as the name/value pair as shown below.
<br />http://localhost:8080/myWebCtxt/purchase.do;jsessionid=4FB61319542B5D310B243E4BDD6DC64B
<br />Adding session ID to each and every link is cumbersome and hence is simplified by the following methods:
<br />response.encodeURL(givenURL) to associate a session ID with a given URL and if you are using redirection
<br />then response.encodeRedirectURL(givenURL).
<br />//set a value in the session
<br />public class CRMServlet extends HttpServlet {
<br />protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
<br />ServletException, IOException {
<br />req.getSession().setAttribute("key", "ItemNo-1245");
<br />String url = resp.encodeURL("/myWebCtxt/purchase.do");
<br />PrintWriter pw = resp.getWriter();
<br />pw.println("<html>Sample encoded URL --><a href="'">purchase</a></html>");
<br />}
<br />}
<br />//retrieve the previously set value from the session
<br />public class PurchaseServlet extends HttpServlet {
<br />protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
<br />ServletException, IOException {
<br />String value = (String)req.getSession().getAttribute("key");
<br />PrintWriter pw = resp.getWriter();
<br />pw.println("<html>Item to purchase is --> " + value +"</html>");
<br />}
<br />}
<br />When you invoke the method encodeURL(givenURL) with the cookies turned on, then session ID is not appended
<br />to the URL. Now turn the cookies off and restart the browser. If you invoke the encodeURL(givenURL) with the
<br />cookies turned off, the session ID is automatically added to the URL as follows:
<br />http://localhost:8080/myWebCtxt/purchase.do;jsessionid=4FB61319542B5D310B243E4BDD6DC64B
<br />
<br /><span style="font-weight: bold;">Q. What is the difference between using getSession(true) and getSession(false) methods?</span>
<br /><span style="font-weight: bold;">A:</span>getSession(true): This method will check whether there is already a session exists for the user. If a session
<br />exists, it returns that session object. If a session does not already exist then it creates a new session for the user.
<br />getSession(false): This method will check whether there is already a session exists for the user. If a session
<br />exists, it returns that session object. If a session does not already exist then it returns null.
<br />Sessions can be timed out (configured in web.xml) or manually invalidated.
<br />Hidden Fields on the pages can maintain state and they are not visible on the browser. The server treats both
<br />hidden and non-hidden fields the same way.
<br /><input type="”hidden”" name="”Firstname”" value="”Peter”">
<br /><input type="”hidden”" name="”Lastname”" value="”Smith”">
<br />The disadvantage of hidden fields is that they may expose sensitive or private information to others.
<br />URL re-writing will append the state information as a query string to the URL. This should not be used to maintain
<br />private or sensitive information.
<br />Http://MyServer:8080/MyServlet?Firstname=Peter&Lastname=Smith
<br />Cookies: A cookie is a piece of text that a Web server can store on a user’s hard disk. Cookies allow a website to
<br />store information on a user’s machine and later retrieve it. These pieces of information are stored as name-value
<br />pairs. The cookie data moves in the following manner:
<br />�� If you type the URL of a website into your browser, your browser sends the request to the Web server. When
<br />the browser does this it looks on your machine for a cookie file that URL has set. If it finds it, your browser
<br />will send all of the name-value pairs along with the URL. If it does not find a cookie file, it sends no cookie
<br />data.
<br />�� The URL’s Web server receives the cookie data and requests for a page. If name-value pairs are received,
<br />the server can use them. If no name-value pairs are received, the server can create a new ID and then sends
<br />name-value pairs to your machine in the header for the Web page it sends. Your machine stores the name
<br />value pairs on your hard disk.
<br />Cookies can be used to determine how many visitors visit your site. It can also determine how many are new
<br />versus repeated visitors. The way it does this is by using a database. The first time a visitor arrives; the site
<br />creates a new ID in the database and sends the ID as a cookie. The next time the same user comes back, the site
<br />can increment a counter associated with that ID in the database and know how many times that visitor returns.
<br />The sites can also store user preferences so that site can look different for each visitor.
<br />
<br />
<br /><meeta equiv="”Refresh”" content="”5;" url="http://localhost:8080/myCtxt/crm.do”"></meeta>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7254714638487456582.post-56391733911983129892009-07-20T05:49:00.003-07:002009-07-20T06:32:52.145-07:00EJB Interview Questions<span style="font-weight: bold;">Q : What are the different kinds of enterprise beans? </span><br />A :<br />Session Bean: is a non-persistent object that implements some business logic running on the server. Session<br />beans do not survive system shut down. There are two types of session beans<br />�� Stateless session beans (i.e. each session bean can be reused by multiple EJB clients).<br />�� Stateful session beans (i.e. each session bean is associated with one EJB client).<br />Entity Bean: is a persistent object that represents object views of the data, usually a row in a database. They<br />have the primary key as a unique identifier. Multiple EJB clients can share each entity bean. Entity beans can<br />survive system shutdowns. Entity beans can have two types of persistence<br />�� Container-Managed Persistence (CMP) - The container is responsible for saving the bean’s state.<br />�� Bean-Managed Persistence (BMP) – The entity bean is responsible for saving its own state.<br />Message-driven Bean: is integrated with the Java Message Service (JMS) to provide the ability to act as a<br />message consumer and perform asynchronous processing between the server and the message producer.<br /><br /><span style="font-weight: bold;">Q : What is the difference between session and entity beans? </span><br />A :Session Beans Entity Beans<br />Use session beans for application logic. Use entity beans to develop persistent object model.<br />Expect little reuse of session beans. Insist on reuse of entity beans.<br />Session beans control the workflow and transactions of a<br />group of entity beans.<br />Domain objects with a unique identity (i.e.-primary key) shared<br />by multiple clients.<br />Life is limited to the life of a particular client. Handle<br />database access for a particular client.<br />Persist across multiple invocations. Handles database access<br />for multiple clients.<br />Do not survive system shut downs or server crashes.<br />Do survive system shut downs or server crashes.<br /><br /><span style="font-weight: bold;">Q : What is the difference between stateful and stateless session beans? </span><br />A :Stateless Session Beans Stateful Session Bean<br />Do not have an internal state. Can be reused by different<br />clients.<br />Do have an internal state. Reused by the same client.<br />Need not be activated or passivated since the beans are<br />pooled and reused.<br />Need to handle activation and passivation to conserve system<br />memory since one session bean object per client.<br /><br /><span style="font-weight: bold;">Q : What is the difference between Container Managed Persistence (CMP) and Bean Managed Persistence (BMP) entity beans?</span><br />A :Container Managed Persistence (CMP) Bean Managed Persistence (BMP)<br />The container is responsible for persisting state of the bean. The bean is responsible for persisting its own state.<br />Container needs to generate database (SQL) calls. The bean needs to code its own database (SQL) calls.<br />The bean persistence is independent of its database (e.g.<br />DB2, Oracle, Sybase etc). So it is portable from one data<br />source to another.<br />The bean persistence is hard coded and hence may not be<br />portable between different databases (e.g. DB2, Oracle etc).<br /><br /><span style="font-weight: bold;">Q : Can an EJB client invoke a method on a bean directly? </span><br />A : An EJB client should never access an EJB directly. Any access is done through the container. The container will<br />intercept the client call and apply services like transaction, security etc prior to invoking the actual EJB.<br /><br /><span style="font-weight: bold;">Q : How does an EJB interact with its container and what are the call-back methods in entity beans? </span><br />A : EJB interacts with its container through the following mechanisms<br />�� Call-back Methods: Every EJB implements an interface (extends EnterpriseBean) which defines several<br />methods which alert the bean to various events in its lifecycle. A container is responsible for invoking these<br />methods. These methods notify the bean when it is about to be activated, to be persisted to the database, to<br />end a transaction, to remove the bean from the memory, etc. For example the entity bean has the following<br />call-back methods:<br />public interface javax.ejb.EntityBean {<br />public void setEntityContext(javax.ejb.EntityContext c);<br />public void unsetEntityContext();<br />public void ejbLoad();<br />public void ejbStore();<br />public void ejbActivate();<br />public void ejbPassivate();<br />public void ejbRemove();<br />}<br />�� EJBContext: provides methods for interacting with the container so that the bean can request information<br />about its environment like the identity of the caller, security, status of a transaction, obtains remote reference<br />to itself etc. e.g. isUserInRole(), getUserPrincipal(), isRollbackOnly(), etc<br />�� JNDI (Java Naming and Directory Interface): allows EJB to access resources like JDBC connections, JMS<br />topics and queues, other EJBs etc.<br /><br /><span style="font-weight: bold;">Q : What is the difference between EJB 1.1 and EJB 2.0? What is the difference between EJB 2.x and EJB 3.0?</span><br />A : EJB 2.0 has the following additional advantages over the EJB 1.1<br />�� Local interfaces: These are beans that can be used locally, that means by the same Java Virtual Machine,<br />so they do not required to be wrapped like remote beans, and arguments between those interfaces are<br />passed directly by reference instead of by value. This improves performance.<br />�� ejbHome methods: Entity beans can declare ejbHomeXXX(…) methods that perform operations related to<br />the EJB component but that are not specific to a bean instance. The ejbHomeXXX(…) method declared in the<br />bean class must have a matching home method XXXX( …) in the home interface.<br />�� Message Driven Beans (MDB): is a completely new enterprise bean type, which is designed specifically to<br />handle incoming JMS messages.<br />�� New CMP Model. It is based on a new contract called the abstract persistence schema, which will allow the<br />container to handle the persistence automatically at runtime.<br />�� EJB Query Language (EJB QL): It is a SQL-based language that will allow the new persistence schema to<br />implement and execute finder methods. EJB QL also used in new query methods ejbSelectXXX(…), which is<br />similar to ejbFindXXXX(…) methods except that it is only for the bean class to use and not exposed to the<br />client (i.e. it is not declared in the home interface)<br />Let’s look at some of the new features on EJB 2.1<br />�� Container-managed timer service: The timer service provides coarse-grained, transactional, time-based<br />event notifications to enable enterprise beans to model and manage higher-level business processes.<br />�� Web Service support: EJB 2.1 adds the ability of stateless session beans to implement a Web Service<br />endpoint via a Web Service endpoint interface.<br />�� EJB-QL: Enhanced EJB-QL includes support for aggregate functions and ordering of results.<br />Current EJB 2.x model is complex for a variety of reasons:<br />�� You need to create several component interfaces and implement several unnecessary call-back methods.<br />�� EJB deployment descriptors are complex and error prone.<br />�� EJB components are not truly object oriented, as they have restrictions for using inheritance and<br />polymorphism.<br />�� EJB modules cannot be tested outside an EJB container and debugging an EJB inside a container is very<br />difficult.<br /><br /><span style="font-weight: bold;">Q: What are the implicit services provided by an EJB container?</span><br />A :<br /> Lifecycle Management: Individual enterprise beans do not need to explicitly manage process allocation,<br />thread management, object activation, or object destruction. The EJB container automatically manages the<br />object lifecycle on behalf of the enterprise bean.<br /> State Management: Individual enterprise beans do not need to explicitly save or restore conversational<br />object state between method calls. The EJB container automatically manages object state on behalf of the<br />enterprise bean.<br /> Security: Individual enterprise beans do not need to explicitly authenticate users or check authorization<br />levels. The EJB container automatically performs all security checking on behalf of the enterprise bean.<br /> Transactions: Individual enterprise beans do not need to explicitly specify transaction demarcation code to<br />participate in distributed transactions. The EJB container can automatically manage the start, enrolment,<br />commitment, and rollback of transactions on behalf of the enterprise bean.<br /> Persistence: Individual enterprise beans do not need to explicitly retrieve or store persistent object data from<br />a database. The EJB container can automatically manage persistent data on behalf of the enterprise bean.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7254714638487456582.post-15508541248058749782009-07-20T05:49:00.001-07:002009-07-20T06:03:36.984-07:00JSP Interview Questions<span style="font-weight:bold;">Q. How do you prevent multiple submits due to repeated “refresh button” clicks?</span><br />Problem: Very often a user is completely unaware that a browser resends information to the server when a<br />“refresh button” in Microsoft Internet Explorer or a “reload button” in Netscape/Mozilla is clicked. Even if a browser<br />warns user, a user cannot often understand the technical meaning of the warning. This action can cause form data<br />to be resubmitted, possibly with unexpected results such as duplicate/multiple purchases of a same item,<br />attempting to delete the previously deleted item from the database resulting in a SQLException being thrown.<br />Non-idempotent methods are methods that cause the state to change. But some operations like reading a list of<br />products or customer details etc are safe because they do not alter the state of the model and the database.<br />These methods are known as idempotent methods.<br />Solution-1: You can use a Post/Redirect/Get (aka PRG) pattern. This pattern involves the following steps:<br />Step-1: First a user filled form is submitted to the server (i.e. a Servlet) using a “POST” (also a “GET” method).<br />Servlet performs a business operation by updating the state in the database and the business model.<br />Step-2: Servlet replies with redirect response (i.e. sendRedirect() operation as opposed to the forward() operation)<br />for a view page.<br />Step-3: Browser loads a view using a “GET” where no user data is sent. This is usually a separate JSP page,<br />which is safe from “multiple submits”. For e.g. reading data from a database, a confirmation page etc.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7254714638487456582.post-45248972576855075962009-07-20T05:44:00.001-07:002009-07-20T05:58:01.688-07:00J2EE Interview Questions<span style="font-weight:bold;">Q. So what is the difference between a component and a service?</span><br />A component is an application level software unit as shown in the table above. All the J2EE components depend<br />on the container for the system level support like transactions, security, pooling, life cycle management, threading<br />etc. A service is a component that can be used remotely through a remote interface either synchronously or<br />asynchronously (e.g. Web service, messaging system, sockets, RPC etc). A service is a step up from “distributed<br />objects”. A service is a function that has a clearly defined service contract (e.g. interface, XML contract) to their<br />consumers or clients, self contained and does not depend on the context or state of other services.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7254714638487456582.post-69712170902037592312009-07-20T05:14:00.001-07:002009-07-20T21:29:34.850-07:00Java Interview Questions<span style="font-weight: bold;">Q. What do you need to do to run a class with a main() method in a package?</span><br />Example: Say, you have a class named “Pet” in a project folder “c:\myProject” and package named<br />com.xyz.client, will you be able to compile and run it as it is?<br />package com.xyz.client;<br />public class Pet {<br />public static void main(String[] args) {<br />System.out.println("I am found in the classpath");<br />}<br />}<br />To run �� c:\myProject> java com.xyz.client.Pet<br />The answer is no and you will get the following exception: “Exception in thread "main" java.lang.-<br />NoClassDefFoundError: com/xyz/client/Pet”. You need to set the classpath. How can you do that? One of the<br />following ways:<br /><br />1. Set the operating system CLASSPATH environment variable to have the project folder “c:\myProject”.<br /><br />2. Set the operating system CLASSPATH environment variable to have a jar file “c:/myProject/client.jar”, which has the Pet.class file in it.<br /><br />3. Run it with –cp or –classpath option as shown below:<br />c:\>java –cp c:/myProject com.xyz.client.Pet<br />OR<br />c:\>java -classpath c:/myProject/client.jar com.xyz.client.Pet<br />Important: Two objects loaded by different class loaders are never equal even if they carry the same values, which mean a class is uniquely identified in the context of the associated class loader. This applies to singletons too, where each class loader will have its own singleton.<br /><br /><span style="font-weight: bold;">Q : What are the advantages of Object Oriented Programming Languages (OOPL)?</span><br />A : The Object Oriented Programming Languages directly represent the real life objects like Car, Jeep, Account,Customer etc. The features of the OO programming languages like polymorphism, inheritance and encapsulation make it powerful. [Tip: remember pie which, stands for Polymorphism, Inheritance and Encapsulation are the 3 pillars of OOPL]<br /><br /><span style="font-weight: bold;">Q : How does the Object Oriented approach improve software development?</span><br />A : The key benefits are:<br />�� Re-use of previous work: using implementation inheritance and object composition.<br />�� Real mapping to the problem domain: Objects map to real world and represent vehicles, customers, products etc: with encapsulation.<br />�� Modular Architecture: Objects, systems, frameworks etc are the building blocks of larger systems.<br />The increased quality and reduced development time are the by-products of the key benefits discussed above.<br />If 90% of the new application consists of proven existing components then only the remaining 10% of the code have to be tested from scratch.<br /><br /><span style="font-weight: bold;">Q : is the difference between composition and aggregation? </span><br />A : The ‘is a’ relationship is expressed with inheritance and ‘has a’ relationship is expressed with composition. Both inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for code reuse are class inheritance and object composition.<br />Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses extends key word. Composition: is used when House has a Bathroom. It is incorrect to say House is a Bathroom. Composition simply means using instance variables that refer to other objects. The class House will have an instance variable, which refers to a Bathroom object.<br /><br /><span style="font-weight: bold;">Q. Which one to favor, composition or inheritance? </span><br />The guide is that inheritance should be only used when subclass ‘is a’ superclass.<br />�� Don’t use inheritance just to get code reuse. If there is no ‘is a’ relationship then use composition for code reuse. Overuse of implementation inheritance (uses the “extends” key word) can break all the subclasses, if the superclass is modified.<br />�� Do not use inheritance just to get polymorphism. If there is no ‘is a’ relationship and all you want is polymorphism then use interface inheritance with composition, which gives you code reuse .<br /><br /><span style="font-weight: bold;">Q : What do you mean by polymorphism, inheritance, encapsulation, and dynamic binding? </span><br />A : Polymorphism – means the ability of a single variable of a given type to be used to reference objects of different types, and automatically call the method that is specific to the type of object the variable references. In a nutshell, polymorphism is a bottom-up method call. The benefit of polymorphism is that it is very easy to add new classes of derived objects without breaking the calling code uses the polymorphic classes or interfaces. When you send a message to an object even though you don’t know what specific type it is, and the right thing happens, that’s called polymorphism. The process used by object-oriented programming languages to implement polymorphism is called dynamic binding.<br /><br />Inheritance – is the inclusion of behavior (i.e. methods) and state (i.e. variables) of a base class in a derived class so that they are accessible in that derived class. The key benefit of Inheritance is that it provides the formal mechanism for code reuse. Any shared piece of business logic can be moved from the derived class into the base class as part of refactoring process to improve maintainability of your code by avoiding code duplication. The existing class is called the<br />superclass and the derived class is called the subclass. Inheritance can also be defined as the process whereby one object acquires characteristics from one or more other objects the same way children acquire characteristics from their parents. There are two types of inheritances:<br /><br />1. Implementation inheritance (aka class inheritance): You can extend an application’s functionality by reusing functionality in the parent class by inheriting all or some of the operations already implemented. In Java, you can only inherit from one superclass. Implementation inheritance promotes reusability but improper use of class inheritance can<br />cause programming nightmares by breaking encapsulation and making future changes a problem. With implementation inheritance, the subclass becomes tightly coupled with the superclass. This will make the design fragile because if you want to change the superclass, you must know all the details of the subclasses to avoid breaking them. So when using implementation inheritance, make sure that the subclasses depend only on the behavior of the superclass, not on the actual implementation. For example in the above diagram, the subclasses should only be concerned about the behavior known as area() but not how it is implemented.<br /><br />2. Interface inheritance (aka type inheritance): This is also known as subtyping. Interfaces provide a mechanism for specifying a relationship between otherwise unrelated classes, typically by specifying a set of common methods each implementing class must contain. Interface inheritance promotes the design concept of program to interfaces not to implementations. This also reduces the coupling or implementation dependencies between systems. In Java, you can<br />implement any number of interfaces. This is more flexible than implementation inheritance because it won’t lock you into specific implementations which make subclasses difficult to maintain. So care should be taken not to break the implementing classes by modifying the interfaces.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">.What is garbage collection? What is the process that is responsible for doing that in java? </span><br />Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this process<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What kind of thread is the Garbage collector thread? </span><br />It is a daemon thread.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is a daemon thread? </span><br />These are the threads which can run without user intervention. The JVM can exit when there are daemon thread by killing them abruptly.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. How will you invoke any external process in Java?</span><br />Runtime.getRuntime().exec(….)<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is the finalize method do? </span><br />Before the invalid objects get garbage collected, the JVM give the user a chance to clean up some resources before it got garbage collected.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is mutable object and immutable object?</span><br />If a object value is changeable then we can call it as Mutable object. (Ex., StringBuffer, …) If you are not allowed to change the value of an object, it is immutable object. (Ex., String, Integer, Float, …)<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is the basic difference between string and stringbuffer object?</span><br />String is an immutable object. StringBuffer is a mutable object.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is the purpose of Void class?</span><br />The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is reflection?</span><br />Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is the base class for Error and Exception? </span><br />Throwable<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is the byte range?</span><br />-128 to 127<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is the implementation of destroy method in java.. is it native or java code?</span><br />This method is not implemented.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is a package?</span><br />To group set of classes into a single unit is known as packaging. Packages provides wide namespace ability.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What are the approaches that you will follow for making a program very efficient?</span><br />By avoiding too much of static methods avoiding the excessive and unnecessary use of synchronized methods Selection of related classes based on the application (meaning synchronized classes for multiuser and non-synchronized classes for single user) Usage of appropriate design patterns Using cache methodologies for remote invocations Avoiding creation of variables within a loop and lot more.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is a DatabaseMetaData?</span><br />Comprehensive information about the database as a whole.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is Locale?</span><br />A Locale object represents a specific geographical, political, or cultural region<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. How will you load a specific locale? </span><br />Using ResourceBundle.getBundle(…);<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is JIT and its use? </span><br />Really, just a very fast compiler… In this incarnation, pretty much a one-pass compiler — no offline computations. So you can’t look at the whole method, rank the expressions according to which ones are re-used the most, and then generate code. In theory terms, it’s an on-line problem.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. Is JVM a compiler or an interpreter?</span><br />Interpreter<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. When you think about optimization, what is the best way to findout the time/memory consuming process?</span><br />Using profiler<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is the purpose of assert keyword used in JDK1.4.x?</span><br />In order to validate certain expressions. It effectively replaces the if block and automatically throws the AssertionError on failure. This keyword should be used for the critical arguments. Meaning, without that the method does nothing.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. How will you get the platform dependent values like line separator, path separator, etc., ?</span><br />Using Sytem.getProperty(…) (line.separator, path.separator, …)<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is skeleton and stub? what is the purpose of those?</span><br />Stub is a client side representation of the server, which takes care of communicating with the remote server. Skeleton is the server side representation. But that is no more in use… it is deprecated long before in JDK.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is the final keyword denotes?</span><br />final keyword denotes that it is the final implementation for that method or variable or class. You can’t override that method/variable/class any more.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is the significance of ListIterator?</span><br />You can iterate back and forth.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is the major difference between LinkedList and ArrayList? </span><br />LinkedList are meant for sequential accessing. ArrayList are meant for random accessing.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is nested class?</span><br />If all the methods of a inner class is static then it is a nested class.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is inner class?</span><br />If the methods of the inner class can only be accessed via the instance of the inner class, then it is called inner class.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is composition?</span><br />Holding the reference of the other class within some other class is known as composition.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is aggregation?</span><br />It is a special type of composition. If you expose all the methods of a composite class and route the method call to the composite method through its reference, then it is called aggregation.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What are the methods in Object?</span><br />clone, equals, wait, finalize, getClass, hashCode, notify, notifyAll, toString<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. Can you instantiate the Math class?</span><br />You can’t instantiate the math class. All the methods in this class are static. And the constructor is not public.<br /><br /><span style="font-weight: bold;">Q</span><span style="font-weight: bold;">. What is singleton?</span><br />It is one of the design pattern. This falls in the creational pattern of the design pattern. There will be only one instance for that entire JVM. You can achieve this by having the private constructor in the class. For eg.,<br />public class Singleton {<br /> private static final Singleton s = new Singleton();<br /> private Singleton() { }<br /> public static Singleton getInstance() { return s; }<br /> // all non static methods … }<br /><br /><span style="font-weight: bold;"></span>Unknownnoreply@blogger.com0