{"id":2017,"date":"2011-04-15T11:28:18","date_gmt":"2011-04-15T05:58:18","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2017"},"modified":"2011-04-15T11:28:18","modified_gmt":"2011-04-15T05:58:18","slug":"java-j2ee-interview-questions-1","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-j2ee-interview-questions-1\/","title":{"rendered":"Java &#8211; J2EE Interview Questions &#8211; 1"},"content":{"rendered":"<p><strong>1. What is Generic Servlet ?<\/strong><br \/>\n<strong> Answer :<\/strong> Defines a generic, protocol-independent servlet. <a title=\"GenericServlet API\" href=\"http:\/\/download.oracle.com\/javaee\/5\/api\/javax\/servlet\/GenericServlet.html\" target=\"_blank\">GenericServlet <\/a>implements the <a title=\"Servlet API\" href=\"http:\/\/download.oracle.com\/javaee\/5\/api\/javax\/servlet\/Servlet.html\" target=\"_blank\"><em>Servlet <\/em><\/a>and <a title=\"ServletConfig API\" href=\"http:\/\/download.oracle.com\/javaee\/5\/api\/javax\/servlet\/ServletConfig.html\" target=\"_blank\"><em>ServletConfig<\/em><\/a> interfaces. <em>GenericServlet <\/em>makes writing servlets easier. It provides simple versions of the lifecycle methods <em>init <\/em>and <em>destroy<\/em>. To use Protocol specific servlet like http protocol, we can use <a title=\"HttpServlet\" href=\"http:\/\/download.oracle.com\/javaee\/5\/api\/javax\/servlet\/http\/HttpServlet.html\" target=\"_blank\">HttpServlet<\/a>. Difference in HttpServlet and GenericServlet is that, HttpServlet provides few more methods like <em>doGet <\/em>and <em>doPost<\/em>. Same operations can be done in <em>service<\/em> method of generic servlet.\u00a0 Read <a title=\"Servlet Life Cycle\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/servlet\/life-cycle-of-servlet\/\" target=\"_blank\">Servlet Life cycle<\/a><\/p>\n<hr \/>\n<p><strong>2. Can abstract class have Constructor ?<\/strong><br \/>\n<strong> Answer : <\/strong>Yes. abstract class can have a constructor.<\/p>\n<hr \/>\n<p><strong>3. We cannot instantiate the abstract class, then what is the need of constructor and how it is called.<\/strong><br \/>\n<strong> Answer : <\/strong>You may have not come across this situation. but answer i very easy. If want to initialize few parameters for all the subclasses then we can use the constructor of abstract class. Constructor calling mechanism is same as normal inheritance in java. Default constructor gets called automatically and to call other constructors we have to explicitly call it by using keyword &#8220;super&#8221;.<\/p>\n<hr \/>\n<p><strong><!--more-->4. Does java have virtual function ?<\/strong><br \/>\n<strong> Answer : <\/strong>As such there is no keyword &#8220;<em>virtual<\/em>&#8221; present in java. but except static, final and private method all the methods are virtual bu default.\u00a0 Refer <a title=\"Virtual function in java\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/virtual-function-in-java\/\" target=\"_blank\">this <\/a>article to read more on it.<\/p>\n<hr \/>\n<p><strong>5. How you will create custom tag in JSP ?<\/strong><br \/>\n<strong> Answer : <\/strong><\/p>\n<ol>\n<li>Create tld file in &#8220;WEB-INF\/tlds&#8221; folder and configure required properties.<\/li>\n<li>Create a class which implements interface <a title=\"Tag interface API\" href=\"http:\/\/download.oracle.com\/javaee\/1.4\/api\/javax\/servlet\/jsp\/tagext\/Tag.html\" target=\"_blank\">Tag <\/a>\/ <a title=\"BodyTag Interface\" href=\"http:\/\/download.oracle.com\/javaee\/1.4\/api\/javax\/servlet\/jsp\/tagext\/BodyTag.html\" target=\"_blank\">BodyTag <\/a>or extends class <a title=\"TagSupport API\" href=\"http:\/\/download.oracle.com\/javaee\/1.4\/api\/javax\/servlet\/jsp\/tagext\/TagSupport.html\" target=\"_blank\">TagSupport <\/a>\/ <a title=\"BodyTagSupport API\" href=\"http:\/\/download.oracle.com\/javaee\/1.4\/api\/javax\/servlet\/jsp\/tagext\/BodyTagSupport.html\" target=\"_blank\">BodyTagSupport<\/a>. In that class write getter and setter for defined property. Few methods required by interface are <em>doStartTag()<\/em>, <em>doEndTag() <\/em>and <em>release()<\/em>. Read how to <a title=\"Create Custom Tag\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/how-to-create-jsp-custom-tag-%E2%80%93-using-bodytag-interface-or-bodytagsupport\/\" target=\"_blank\">create custom tag<\/a> in detail.<\/li>\n<\/ol>\n<hr \/>\n<p><strong>6. What is Externalization ?<\/strong><br \/>\n<strong> Answer :<\/strong> Externalization is the interface provided by the JAVA which extends interface Serializable. It provides two more methods as compared to Serializable interface &#8211;<\/p>\n<ol>\n<li>void readExternal(ObjectInput in)<\/li>\n<li>void writeExternal(ObjectOutput out)<\/li>\n<\/ol>\n<p>The above two methods gives the flexibility and control over how the object is serialized and deserialized. for further read <a title=\"Externalization Tutorial\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/explain-externalizable-in-java\/\">refer this article<\/a>.<\/p>\n<hr \/>\n<p><strong>7. What is the use of serialVersionUID in JAVA ?<\/strong><br \/>\n<strong> Answer : <\/strong>Whenever object is created, one unique ID is associated with object and its implementation may defer from compiler to compiler. Whenever any object is serialized and deserealized, this variable is used to determine whether the object is in same state or not ? assignment of default serialVersionUID is mostly depends upon the methods and fields present in class and if any field is added or removed from the class definition then its serialVersionUID also changes and if the value does not match then it results in &#8220;InvalidClassException&#8221;. Because of this reason it is recommended to have a default unique serialVersionUID with each serialized undergoing class. To read in detail with example, <a title=\"Tutorial of serialVersionUID in JAVA\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/explain-serialversionuid-in-java\/\" target=\"_blank\">refer this article<\/a>.<\/p>\n<hr \/>\n<p><strong>8. If i don&#8217;t want to serialize some fields in class, then how to achieve this ?<\/strong><br \/>\n<strong> Answer :<\/strong> This question can also be asked as &#8220;What is the <strong>transient<\/strong> variable ?&#8221;. &#8220;<strong>transient variables and static variables<\/strong>&#8221; can be used to avoid the field being serialized in Java. (As static variable is class level variable, and it does not associated at object level &#8211; it is not serialized).<\/p>\n<hr \/>\n<p><strong>9. What will happen if one of the members in the class doesn&#8217;t implement Serializable interface?<\/strong><br \/>\n<strong> Answer :<\/strong> If you try to serialize an object of a class which implements Serializable, but the object includes a reference to an non- Serializable class then a &#8220;\u02dc<strong><a title=\"Java API\" href=\"http:\/\/download.oracle.com\/javase\/6\/docs\/api\/java\/io\/NotSerializableException.html\" target=\"_blank\">NotSerializableException<\/a><\/strong>&#8216; will be thrown at runtime.<\/p>\n<hr \/>\n<p><strong>10. If a class is Serializable but its super class in not, what will be the state of the instance variables inherited from super class after deserialization?<\/strong><br \/>\n<strong> Answer : <\/strong>Java serialization process only continues in object hierarchy till the class is Serializable and values of the instance variables inherited from super class will be initialized <strong>by calling constructor<\/strong> of Non-Serializable Super class during deserialization process .<\/p>\n<hr \/>\n<p><strong>11. What is the conceptual difference between interface and abstract class ? When to use interface and abstract class ?<\/strong><br \/>\n<strong> Answer :<\/strong><\/p>\n<p><strong>Abstract class and its usage <\/strong>:<\/p>\n<p>A class which is partially implemented and have few abstract methods are know an &#8220;<strong>abstract class<\/strong>&#8220;. few developers think that why anybody should create a partial implementation ? The best example i have in my mind is &#8220;DataMapper Pattern&#8221;. For the CRUD operation &#8211; open connection and close connection and sequence of save method will be known to the programmer however which query will be executed to save or retrieve differs for different object. in that case createInsertSQL() method can be made abstract and internally save() will call openConnection() and closeConnection() which is implemented. below code snap explains the save() method.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nabstract public String createInsertSQL();\/\/abstract method\n\npublic void save() \/\/method implemented\n{\n     try{\n          openConnection();\/\/method implemented\n          String sql = createInsertSQL(); \/\/abstract method\n          executeStatement();\/\/method implemented\n      }\n      catch(SQLException ex){\n\n     }\n     finally{\n        closeconnection();\/\/method implemented\n     }\n}\n\n<\/pre>\n<p>As you can see in above code snap, only unknown implementation is the SQL Query which is made abstract and others are implemented. and therefore underlying child class must implement the method createInsertSQL() and call save().<br \/>\n<strong> <\/strong><\/p>\n<p><strong>Interface and its usage :<\/strong><br \/>\ninterface are also known as &#8220;contract&#8221;. when we know the skeleton\/ structure of the class but it complete implementation differs then we must use interface. in following scenario we must use interface :<\/p>\n<ol>\n<li>Dependency injection<\/li>\n<li>Runtime polymorphism<\/li>\n<\/ol>\n<p>In design pattern programming to interface is preferable rather than programming to implementation (inheritance).<\/p>\n<hr \/>\n<p><strong>12. How to iterate over keys of HashMap in JDK 4 and 5?<\/strong><br \/>\n<strong>Answer :<\/strong> This is the common question asked in interview.<\/p>\n<p><strong>In JAVA 5 : <\/strong>we can use advance for loop as shown in above code, use map.keySet(). This will return the\u00a0<a title=\"Set Interface API\" href=\"http:\/\/download.oracle.com\/javase\/1.5.0\/docs\/api\/java\/util\/Set.html\" target=\"_blank\">Set <\/a>(As Keys must be unique)<\/p>\n<p><strong>In JAVA 4 :<\/strong> use map.keySet() and get the\u00a0<a title=\"Iterator API\" href=\"http:\/\/download.oracle.com\/javase\/1.5.0\/docs\/api\/java\/util\/Iterator.html\" target=\"_blank\">Iterator <\/a>object using map.<a title=\"iterator() method API\" href=\"http:\/\/download.oracle.com\/javase\/1.5.0\/docs\/api\/java\/util\/Set.html#iterator%28%29\" target=\"_blank\">iterate()<\/a> . then using while loop , get the value for each key.<\/p>\n<hr \/>\n<p><strong>13. What is Exception chaining \u00a0and how to achieve ?<\/strong><br \/>\n<strong> Answer :<\/strong> When one exception causes another exception then the second exception must print the log of the first exception also.<\/p>\n<p>We can use initcause() method of the &#8220;Throwable&#8221; class to retain the cause of exception.<\/p>\n<p><a title=\"Exception chaining\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/exception-chaining\/\">Read more..<\/a><\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>JAVA &#8211; J2EE Interview Questions &#8211; 1, JAVA &#8211; J2EE Interview Questions &#8211; 1,custom tag  JSP, Externalization , serialVersionUID, difference between interface and abstract class, iterate  HashMap <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[125,329],"class_list":["post-2017","post","type-post","status-publish","format-standard","hentry","category-java","tag-j2ee","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1439,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/difference-in-httpservlet-and-genericservlet\/","url_meta":{"origin":2017,"position":0},"title":"Difference in HttpServlet and GenericServlet","author":"Jitendra","date":"February 4, 2011","format":false,"excerpt":"What is the basic Difference in HttpServlet and GenericServlet","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1959,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/create-servlet-using-annotation\/","url_meta":{"origin":2017,"position":1},"title":"Create Servlet using Annotation &#8211; Servlet 3.0","author":"Jitendra","date":"April 13, 2011","format":false,"excerpt":"Create the Servlet without any deployment descriptor. Using Annotations","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1976,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/j2ee-servlet-jsp-jsf-and-jms-version-table\/","url_meta":{"origin":2017,"position":2},"title":"J2EE &#8211; Servlet , JSP , JSF and JMS Version Table","author":"Jitendra","date":"April 13, 2011","format":false,"excerpt":"J2EE - Servlet , JSP , JSF and JMS Version Table","rel":"","context":"In &quot;JSP&quot;","block_context":{"text":"JSP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/jsp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1586,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/read-file-present-inside-java-and-j2ee-project\/","url_meta":{"origin":2017,"position":3},"title":"Read File Present inside Java and J2EE Project","author":"Jitendra","date":"February 24, 2011","format":false,"excerpt":"How to Read File Present inside Java and J2EE Project","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"Read File Present inside Java and J2EE Project","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Read-File-Present-inside-Java-and-J2EE-Project.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1494,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/life-cycle-of-servlet\/","url_meta":{"origin":2017,"position":4},"title":"Life Cycle of Servlet","author":"Jitendra","date":"February 12, 2011","format":false,"excerpt":"Explain Life Cycle of Servlet","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"Servlet Life Cycle","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Servlet-Life-Cycle.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1474,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/difference-in-servletrequest-getrequestdispather-and-servletcontext-getrequestdispatcher-in-servlet\/","url_meta":{"origin":2017,"position":5},"title":"difference in ServletRequest.getRequestDispather() and ServletContext.getRequestDispatcher() in Servlet","author":"Jitendra","date":"February 11, 2011","format":false,"excerpt":"What is difference in ServletRequest.getRequestDispather() and ServletContext.getRequestDispatcher() in Servlet","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2017"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2017\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}