<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-9044200624497812337</id><updated>2024-11-01T16:34:50.312+05:30</updated><category term="Advanced EJB interview questions"/><category term="Good questions asked during Java interview"/><category term="Image in Frame"/><category term="J2EE interview questions and answers"/><category term="JDBC and JSP interview questions and answers"/><category term="OOPS concept in java"/><category term="SCJP Questions 101-110"/><category term="SCJP Questions 111-120"/><category term="SCJP Questions 121-130"/><category term="SCJP Questions 131-14"/><category term="SCJP Questions 141-150"/><category term="SCJP Questions 151-160"/><category term="SCJP Questions 161-170"/><category term="SCJP Questions 171-180"/><category term="SCJP Questions 181-190"/><category term="SCJP Questions 191-200"/><category term="SCJP Questions 201-210"/><category term="SCJP Questions 211-224"/><category term="SCJP Questions 31-40"/><category term="SCJP Questions 41-50"/><category term="SCJP Questions 51-60"/><category term="SCJP Questions 61-70"/><category term="SCJP Questions 71-80"/><category term="SCJP Questions 81-90"/><category term="SCJP Questions 91-100"/><category term="Socket Communications"/><category term="java class"/><category term="java how to"/><category term="java memory"/><category term="java program"/><category term="scjp questions 1-10"/><category term="scjp questions 11-20"/><category term="scjp questions 21-30"/><category term="swing 3D effect"/><title type='text'>Java help</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>38</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-5363586436439083144</id><published>2012-06-27T20:19:00.002+05:30</published><updated>2012-06-27T20:19:49.248+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="J2EE interview questions and answers"/><title type='text'>J2EE interview questions and answers</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;ol style=&quot;border: 0px; font-family: Georgia, serif; font-size: 12px; line-height: 18px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 18px 3em; outline: 0px; padding: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What makes J2EE suitable for distributed multitiered Applications?&lt;/b&gt;&lt;br /&gt;- The J2EE platform uses a multitiered distributed application model. Application logic is divided into components according to function, and the various application components that make up a J2EE application are installed on different machines depending on the tier in the multitiered J2EE environment to which the application component belongs. The J2EE application parts are:&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;/div&gt;
&lt;ul style=&quot;background-color: transparent; border: 0px; list-style: square; margin: 0px 0px 18px 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Client-tier components run on the client machine.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Web-tier components run on the J2EE server.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Business-tier components run on the J2EE server.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Enterprise information system (EIS)-tier software runs on the EIS server.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span id=&quot;more-124&quot; style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is J2EE?&lt;/b&gt;&amp;nbsp;- J2EE is an environment for developing and deploying enterprise applications. The J2EE platform consists of a set of services, application programming interfaces (APIs), and protocols that provide the functionality for developing multitiered, web-based applications.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What are the components of J2EE application?&lt;/b&gt;&lt;br /&gt;- A J2EE component is a self-contained functional software unit that is assembled into a J2EE application with its related classes and files and communicates with other components. The J2EE specification defines the following J2EE components:&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;/div&gt;
&lt;ol style=&quot;background-color: transparent; border: 0px; list-style: upper-alpha; margin: 0px 0px 0px 3em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Application clients and applets are client components.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Java Servlet and JavaServer Pages technology components are web components.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Enterprise JavaBeans components (enterprise beans) are business components.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Resource adapter components provided by EIS and tool vendors.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What do Enterprise JavaBeans components contain?&lt;/b&gt;&amp;nbsp;- Enterprise JavaBeans components contains Business code, which is logic&lt;br /&gt;that solves or meets the needs of a particular business domain such as banking, retail, or finance, is handled by enterprise beans running in the business tier. All the business code is contained inside an Enterprise Bean which receives data from client programs, processes it (if necessary), and sends it to the enterprise information system tier for storage. An enterprise bean also retrieves data from storage, processes it (if necessary), and sends it back to the client program.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Is J2EE application only a web-based?&lt;/b&gt;&amp;nbsp;- No, It depends on type of application that client wants. A J2EE application can be web-based or non-web-based. if an application client executes on the client machine, it is a non-web-based J2EE application. The J2EE application can provide a way for users to handle tasks such as J2EE system or application administration. It typically has a graphical user interface created from Swing or AWT APIs, or a command-line interface. When user request, it can open an HTTP connection to establish communication with a servlet running in the web tier.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Are JavaBeans J2EE components?&lt;/b&gt;&amp;nbsp;- No. JavaBeans components are not considered J2EE components by the J2EE specification. They are written to manage the data flow between an application client or applet and components running on the J2EE server or between server components and a database. JavaBeans components written for the J2EE platform have instance variables and get and set methods for accessing the data in the instance variables. JavaBeans components used in this way are typically simple in design and implementation, but should conform to the naming and design conventions outlined in the JavaBeans component architecture.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Is HTML page a web component?&lt;/b&gt;&amp;nbsp;- No. Static HTML pages and applets are bundled with web components during application assembly, but are not considered web components by the J2EE specification. Even the server-side utility classes are not considered web components, either.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What can be considered as a web component?&lt;/b&gt;&amp;nbsp;- J2EE Web components can be either servlets or JSP pages. Servlets are Java programming language classes that dynamically process requests and construct responses. JSP pages are text-based documents that execute as servlets but allow a more natural approach to creating static content.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the container?&lt;/b&gt;&amp;nbsp;- Containers are the interface between a component and the low-level platform specific functionality that supports the component. Before a Web, enterprise bean, or application client component can be executed, it must be assembled into a J2EE application and deployed into its container.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What are container services?&lt;/b&gt;&amp;nbsp;- A container is a runtime support of a system-level entity. Containers provide components with services such as lifecycle management, security, deployment, and threading.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the web container?&lt;/b&gt;&amp;nbsp;- Servlet and JSP containers are collectively referred to as Web containers. It manages the execution of JSP page and servlet components for J2EE applications. Web components and their container run on the J2EE server.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is Enterprise JavaBeans (EJB) container?&lt;/b&gt;&amp;nbsp;- It manages the execution of enterprise beans for J2EE applications.&lt;br /&gt;Enterprise beans and their container run on the J2EE server.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is Applet container?&lt;/b&gt;&amp;nbsp;- IManages the execution of applets. Consists of a Web browser and Java Plugin running on the client together.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How do we package J2EE components?&lt;/b&gt;&amp;nbsp;- J2EE components are packaged separately and bundled into a J2EE application for deployment. Each component, its related files such as GIF and HTML files or server-side utility classes, and a deployment descriptor are assembled into a module and added to the J2EE application. A J2EE application is composed of one or more enterprise bean,Web, or application client component modules. The final enterprise solution can use one J2EE application or be made up of two or more J2EE applications, depending on design requirements. A J2EE application and each of its modules has its own deployment descriptor. A deployment descriptor is an XML document with an .xml extension that describes a component’s deployment settings.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is a thin client?&lt;/b&gt;&amp;nbsp;- A thin client is a lightweight interface to the application that does not have such operations like query databases, execute complex business rules, or connect to legacy applications.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What are types of J2EE clients?&lt;/b&gt;&amp;nbsp;- Following are the types of J2EE clients:&lt;ul style=&quot;background-color: transparent; border: 0px; list-style: square; margin: 0px 0px 18px 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Applets&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Application clients&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Java Web Start-enabled rich clients, powered by Java Web Start technology.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Wireless clients, based on Mobile Information Device Profile (MIDP) technology.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is deployment descriptor?&lt;/b&gt;&amp;nbsp;- A deployment descriptor is an Extensible Markup Language (XML) text-based file with an .xml extension that describes a component’s deployment settings. A J2EE application and each of its modules has its own deployment descriptor. For example, an enterprise bean module deployment descriptor declares transaction attributes and security authorizations&lt;br /&gt;for an enterprise bean. Because deployment descriptor information is declarative, it can be changed without modifying the bean source code. At run time, the J2EE server reads the deployment descriptor and acts upon the component accordingly.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the EAR file?&lt;/b&gt;&amp;nbsp;- An EAR file is a standard JAR file with an .ear extension, named from Enterprise ARchive file. A J2EE application with all of its modules is delivered in EAR file.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is JTA and JTS?&lt;/b&gt;&amp;nbsp;- JTA is the abbreviation for the Java Transaction API. JTS is the abbreviation for the Jave Transaction Service. JTA provides a standard interface and allows you to demarcate transactions in a manner that is independent of the transaction manager implementation. The J2EE SDK implements the transaction manager with JTS. But your code doesn’t call the JTS methods directly. Instead, it invokes the JTA methods, which then call the lower-level JTS routines. Therefore, JTA is a high level transaction interface that your application uses to control transaction. and JTS is a low level transaction interface and ejb uses behind the scenes (client code doesn’t directly interact with JTS. It is based on object transaction service(OTS) which is part of CORBA.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is JAXP?&lt;/b&gt;&amp;nbsp;- JAXP stands for Java API for XML. XML is a language for representing and describing text-based data which can be read and handled by any program or tool that uses XML APIs. It provides standard services to determine the type of an arbitrary piece of data, encapsulate access to it, discover the operations available on it, and create the appropriate JavaBeans component to perform those operations.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is J2EE Connector?&lt;/b&gt;&amp;nbsp;- The J2EE Connector API is used by J2EE tools vendors and system integrators to create resource adapters that support access to enterprise information systems that can be plugged into any J2EE product. Each type of database or EIS has a different resource adapter. Note: A resource adapter is a software component that allows J2EE application components to access and interact with the underlying resource manager. Because a resource adapter is specific to its resource manager, there is typically a different resource adapter for each type of database or enterprise information system.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is JAAP?&lt;/b&gt;&amp;nbsp;- The Java Authentication and Authorization Service (JAAS) provides a way for a J2EE application to authenticate and authorize a specific user or group of users to run it. It is a standard Pluggable Authentication Module (PAM) framework that extends the Java 2 platform security architecture to support user-based authorization.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is Java Naming and Directory Service?&lt;/b&gt;&amp;nbsp;- The JNDI provides naming and directory functionality. It provides applications with methods for performing standard directory operations, such as associating attributes with objects and searching for objects using their attributes. Using JNDI, a J2EE application can store and retrieve any type of named Java object. Because JNDI is independent of any specific implementations, applications can use JNDI to access multiple naming and directory services, including existing naming and&lt;br /&gt;directory services such as LDAP, NDS, DNS, and NIS.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is Struts?&lt;/b&gt;&amp;nbsp;- A Web page development framework. Struts combines Java Servlets, Java Server Pages, custom tags, and message resources into a unified framework. It is a cooperative, synergistic platform, suitable for development teams, independent developers, and everyone between.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How is the MVC design pattern used in Struts framework?&lt;/b&gt;&amp;nbsp;- In the MVC design pattern, application flow is mediated by a central Controller. The Controller delegates requests to an appropriate handler. The handlers are tied to a Model, and each handler acts as an adapter between the request and the Model. The Model represents, or encapsulates, an application’s business logic or state. Control is usually then forwarded back through the Controller to the appropriate View. The forwarding can be determined by consulting a set of mappings, usually loaded from a database or configuration file. This provides a loose coupling between the View and Model, which can make an application significantly easier to create and maintain. Controller: Servlet controller which supplied by Struts itself; View: what you can see on the screen, a JSP page and presentation components; Model: System state and a business logic JavaBeans.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/5363586436439083144/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2012/06/j2ee-interview-questions-and-answers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/5363586436439083144'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/5363586436439083144'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2012/06/j2ee-interview-questions-and-answers.html' title='J2EE interview questions and answers'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-2611027602885082434</id><published>2012-06-27T20:18:00.000+05:30</published><updated>2012-06-27T20:18:08.653+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Good questions asked during Java interview"/><title type='text'>Good questions asked during Java interview</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;ol style=&quot;border: 0px; font-family: Georgia, serif; font-size: 12px; line-height: 18px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 18px 3em; outline: 0px; padding: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Is “abc” a primitive value?&lt;/b&gt;&amp;nbsp;- The String literal “abc” is not a primitive value. It is a String object.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What restrictions are placed on the values of each case of a switch statement?&lt;/b&gt;- During compilation, the values of each case of a switch statement must evaluate to a value that can be promoted to an int value.&lt;/li&gt;
&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span id=&quot;more-149&quot; style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What modifiers may be used with an interface declaration?&lt;/b&gt;&amp;nbsp;- An interface may be declared as public or abstract.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Is a class a subclass of itself?&lt;/b&gt;&amp;nbsp;- A class is a subclass of itself.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between a while statement and a do statement?&lt;/b&gt;&amp;nbsp;- A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What modifiers can be used with a local inner class?&lt;/b&gt;&amp;nbsp;- A local inner class may be final or abstract.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the purpose of the File class?&lt;/b&gt;&amp;nbsp;- The File class is used to create objects that provide access to the files and directories of a local file system.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can an exception be rethrown?&lt;/b&gt;&amp;nbsp;- Yes, an exception can be rethrown.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;When does the compiler supply a default constructor for a class?&lt;/b&gt;&amp;nbsp;- The compiler supplies a default constructor for a class if no other constructors are provided.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;If a method is declared as protected, where may the method be accessed?&lt;/b&gt;&amp;nbsp;- A protected method may only be accessed by classes or interfaces of the same package or by subclasses of the class in which it is declared.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Which non-Unicode letter characters may be used as the first character of an identifier?&lt;/b&gt;&amp;nbsp;- The non-Unicode letter characters $ and _ may appear as the first character of an identifier&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What restrictions are placed on method overloading?&lt;/b&gt;&amp;nbsp;- Two methods may not have the same name and argument list but different return types.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is casting?&lt;/b&gt;&amp;nbsp;- There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the return type of a program’s main() method?&lt;/b&gt;&amp;nbsp;- A program’s main() method has a void return type.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What class of exceptions are generated by the Java run-time system?&lt;/b&gt;&amp;nbsp;- The Java runtime system generates RuntimeException and Error exceptions.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What class allows you to read objects directly from a stream?&lt;/b&gt;&amp;nbsp;- The ObjectInputStream class supports the reading of objects from input streams.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between a field variable and a local variable?&lt;/b&gt;&amp;nbsp;- A field variable is a variable that is declared as a member of a class. A local variable is a variable that is declared local to a method.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How are this() and super() used with constructors?&lt;/b&gt;&amp;nbsp;- this() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the relationship between a method’s throws clause and the exceptions that can be thrown during the method’s execution?&lt;/b&gt;&amp;nbsp;- A method’s throws clause must declare any checked exceptions that are not caught within the body of the method.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Why are the methods of the Math class static?&lt;/b&gt;&amp;nbsp;- So they can be invoked as if they are a mathematical code library.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What are the legal operands of the instanceof operator?&lt;/b&gt;&amp;nbsp;- The left operand is an object reference or null value and the right operand is a class, interface, or array type.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What an I/O filter?&lt;/b&gt;&amp;nbsp;- An I/O filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;If an object is garbage collected, can it become reachable again?&lt;/b&gt;&amp;nbsp;- Once an object is garbage collected, it ceases to exist. It can no longer become reachable again.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What are E and PI?&lt;/b&gt;&amp;nbsp;- E is the base of the natural logarithm and PI is mathematical value pi.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Are true and false keywords?&lt;/b&gt;&amp;nbsp;- The values true and false are not keywords.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between the File and RandomAccessFile classes?&lt;/b&gt;&amp;nbsp;- The File class encapsulates the files and directories of the local file system. The RandomAccessFile class provides the methods needed to directly access data contained in any part of a file.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What happens when you add a double value to a String?&lt;/b&gt;&amp;nbsp;- The result is a String object.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is your platform’s default character encoding?&lt;/b&gt;&amp;nbsp;- If you are running Java on English Windows platforms, it is probably Cp1252. If you are running Java on English Solaris platforms, it is most likely 8859_1.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Which package is always imported by default?&lt;/b&gt;&amp;nbsp;- The java.lang package is always imported by default.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What interface must an object implement before it can be written to a stream as an object?&lt;/b&gt;&amp;nbsp;- An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How can my application get to know when a HttpSession is removed?&lt;/b&gt;&amp;nbsp;- Define a Class HttpSessionNotifier which implements HttpSessionBindingListener and implement the functionality what you need in valueUnbound() method. Create an instance of that class and put that instance in HttpSession.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Whats the difference between notify() and notifyAll()?&lt;/b&gt;&amp;nbsp;- notify() is used to unblock one waiting thread; notifyAll() is used to unblock all of them. Using notify() is preferable (for efficiency) when only one blocked thread can benefit from the change (for example, when freeing a buffer back into a pool). notifyAll() is necessary (for correctness) if multiple threads should resume (for example, when releasing a “writer” lock on a file might permit all “readers” to resume).&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Why can’t I say just abs() or sin() instead of Math.abs() and Math.sin()?&lt;/b&gt;&amp;nbsp;- The import statement does not bring methods into your local name space. It lets you abbreviate class names, but not get rid of them altogether. That’s just the way it works, you’ll get used to it. It’s really a lot safer this way.&lt;br /&gt;However, there is actually a little trick you can use in some cases that gets you what you want. If your top-level class doesn’t need to inherit from anything else, make it inherit from java.lang.Math. That *does* bring all the methods into your local name space. But you can’t use this trick in an applet, because you have to inherit from java.awt.Applet. And actually, you can’t use it on java.lang.Math at all, because Math is a “final” class which means it can’t be extended.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Why are there no global variables in Java?&lt;/b&gt;&amp;nbsp;- Global variables are considered bad form for a variety of reasons: Adding state variables breaks referential transparency (you no longer can understand a statement or expression on its own: you need to understand it in the context of the settings of the global variables), State variables lessen the cohesion of a program: you need to know more to understand how something works. A major point of Object-Oriented programming is to break up global state into more easily understood collections of local state, When you add one variable, you limit the use of your program to one instance. What you thought was global, someone else might think of as local: they may want to run two copies of your program at once. For these reasons, Java decided to ban global variables.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What does it mean that a class or member is final?&lt;/b&gt;&amp;nbsp;- A final class can no longer be subclassed. Mostly this is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve. Methods may be declared final as well. This means they may not be overridden in a subclass. Fields can be declared final, too. However, this has a completely different meaning. A final field cannot be changed after it’s initialized, and it must include an initializer statement where it’s declared. For example, public final double c = 2.998; It’s also possible to make a static field final to get the effect of C++’s const statement or some uses of C’s #define, e.g. public static final double c = 2.998;&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What does it mean that a method or class is abstract?&lt;/b&gt;&amp;nbsp;- An abstract class cannot be instantiated. Only its subclasses can be instantiated. You indicate that a class is abstract with the abstract keyword like this:&lt;pre style=&quot;background-color: transparent; border: 0px; font-family: Monaco, monospace; font-size: 11px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt; public abstract class Container extends Component {
&lt;/pre&gt;
&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
Abstract classes may contain abstract methods. A method declared abstract is not actually implemented in the current class. It exists only to be overridden in subclasses. It has no body. For example,&lt;/div&gt;
&lt;pre style=&quot;background-color: transparent; border: 0px; font-family: Monaco, monospace; font-size: 11px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt; public abstract float price();
&lt;/pre&gt;
&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
Abstract methods may only be included in abstract classes. However, an abstract class is not required to have any abstract methods, though most of them do. Each subclass of an abstract class must override the abstract methods of its superclasses or itself be declared abstract.&lt;/div&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is a transient variable?&lt;/b&gt;&amp;nbsp;- transient variable is a variable that may not be serialized.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How are Observer and Observable used?&lt;/b&gt;&amp;nbsp;- Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can a lock be acquired on a class?&lt;/b&gt;&amp;nbsp;- Yes, a lock can be acquired on a class. This lock is acquired on the class’s Class object.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What state does a thread enter when it terminates its processing?&lt;/b&gt;&amp;nbsp;- When a thread terminates its processing, it enters the dead state.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How does Java handle integer overflows and underflows?&lt;/b&gt;&amp;nbsp;- It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between the &amp;gt;&amp;gt; and &amp;gt;&amp;gt;&amp;gt; operators?&lt;/b&gt;&amp;nbsp;- The &amp;gt;&amp;gt; operator carries the sign bit when shifting right. The &amp;gt;&amp;gt;&amp;gt; zero-fills bits that have been shifted out.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Is sizeof a keyword?&lt;/b&gt;&amp;nbsp;- The sizeof operator is not a keyword.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Does garbage collection guarantee that a program will not run out of memory?&lt;/b&gt;&amp;nbsp;- Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can an object’s finalize() method be invoked while it is reachable?&lt;/b&gt;&amp;nbsp;- An object’s finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object’s finalize() method may be invoked by other objects.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What value does readLine() return when it has reached the end of a file?&lt;/b&gt;&amp;nbsp;- The readLine() method returns null when it has reached the end of a file.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can a for statement loop indefinitely?&lt;/b&gt;&amp;nbsp;- Yes, a for statement can loop indefinitely. For example, consider the following: for(;;) ;&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;To what value is a variable of the String type automatically initialized?&lt;/b&gt;&amp;nbsp;- The default value of an String type is null.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is a task’s priority and how is it used in scheduling?&lt;/b&gt;&amp;nbsp;- A task’s priority is an integer value that identifies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks before lower priority tasks.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the range of the short type?&lt;/b&gt;&amp;nbsp;- The range of the short type is -(2^15) to 2^15 - 1.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the purpose of garbage collection?&lt;/b&gt;&amp;nbsp;- The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources may be reclaimed and reused.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What do you understand by private, protected and public?&lt;/b&gt;&amp;nbsp;- These are accessibility modifiers. Private is the most restrictive, while public is the least restrictive. There is no real difference between protected and the default type (also known as package protected) within the context of the same package, however the protected keyword allows visibility to a derived class in a different package.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is Downcasting ?&lt;/b&gt;&amp;nbsp;- Downcasting is the casting from a general to a more specific type, i.e. casting down the hierarchy&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can a method be overloaded based on different return type but same argument type ?&lt;/b&gt;&amp;nbsp;- No, because the methods can be called without using their return type in which case there is ambiquity for the compiler&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What happens to a static var that is defined within a method of a class ?&lt;/b&gt;&amp;nbsp;- Can’t do it. You’ll get a compilation error&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How many static init can you have ?&lt;/b&gt;&amp;nbsp;- As many as you want, but the static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference amongst JVM Spec, JVM Implementation, JVM Runtime ?&lt;/b&gt;&amp;nbsp;- The JVM spec is the blueprint for the JVM generated and owned by Sun. The JVM implementation is the actual implementation of the spec by a vendor and the JVM runtime is the actual running instance of a JVM implementation&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Describe what happens when an object is created in Java?&lt;/b&gt;&amp;nbsp;- Several things happen in a particular order to ensure the object is constructed properly: Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its superclasses. Implemenation-specific data includes pointers to class and method data. The instance variables of the objects are initialized to their default values. The constructor for the most derived class is invoked. The first thing a constructor does is call the consctructor for its superclasses. This process continues until the constrcutor for java.lang.Object is called, as java.lang.Object is the base class for all objects in java. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What does the “final” keyword mean in front of a variable? A method? A class?&lt;/b&gt;&amp;nbsp;- FINAL for a variable: value is constant. FINAL for a method: cannot be overridden. FINAL for a class: cannot be derived&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between instanceof and isInstance?&lt;/b&gt;&amp;nbsp;- instanceof is used to check to see if an object can be cast into a specified type without throwing a cast class exception. isInstance() Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Why does it take so much time to access an Applet having Swing Components the first time?&lt;/b&gt;&amp;nbsp;- Because behind every swing component are many Java objects and resources. This takes time to create them in memory. JDK 1.3 from Sun has some improvements which may lead to faster execution of Swing applications.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/2611027602885082434/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2012/06/good-questions-asked-during-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/2611027602885082434'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/2611027602885082434'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2012/06/good-questions-asked-during-java.html' title='Good questions asked during Java interview'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-6748860995943582438</id><published>2012-06-27T20:16:00.000+05:30</published><updated>2012-06-27T20:16:27.721+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="JDBC and JSP interview questions and answers"/><title type='text'>JDBC and JSP interview questions and answers</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;ol style=&quot;border: 0px; font-family: Georgia, serif; font-size: 12px; line-height: 18px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 18px 3em; outline: 0px; padding: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the query used to display all tables names in SQL Server (Query analyzer)?&lt;/b&gt;&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;/div&gt;
&lt;pre style=&quot;background-color: transparent; border: 0px; font-family: Monaco, monospace; font-size: 11px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt; select * from information_schema.tables
&lt;/pre&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How many types of JDBC Drivers are present and what are they?&lt;/b&gt;- There are 4 types of JDBC Drivers&lt;ul style=&quot;background-color: transparent; border: 0px; list-style: square; margin: 0px 0px 18px 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;JDBC-ODBC Bridge Driver&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Native API Partly Java Driver&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Network protocol Driver&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;JDBC Net pure Java Driver&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span id=&quot;more-148&quot; style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can we implement an interface in a JSP?&lt;/b&gt;- No&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between ServletContext and PageContext?&lt;/b&gt;- ServletContext: Gives the information about the container. PageContext: Gives the information about the Request&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?&lt;/b&gt;- request.getRequestDispatcher(path): In order to create it we need to give the&amp;nbsp;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;relative&lt;/b&gt;&amp;nbsp;path of the resource, context.getRequestDispatcher(path): In order to create it we need to give the&amp;nbsp;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;absolute&lt;/b&gt;path of the resource.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How to pass information from JSP to included JSP?&lt;/b&gt;- Using &amp;lt;%jsp:param&amp;gt; tag.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between directive include and jsp include?&lt;/b&gt;- &amp;lt;%@ include&amp;gt;: Used to include static resources during translation time. JSP include: Used to include dynamic content or static content during runtime.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between RequestDispatcher and sendRedirect?&lt;/b&gt;- RequestDispatcher: server-side redirect with request and response objects. sendRedirect : Client-side redirect with new request and response objects.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How does JSP handle runtime exceptions?&lt;/b&gt;- Using errorPage attribute of page directive and also we need to specify isErrorPage=true if the current page is intended to URL redirecting of a JSP.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How do you delete a Cookie within a JSP?&lt;/b&gt;&lt;pre style=&quot;background-color: transparent; border: 0px; font-family: Monaco, monospace; font-size: 11px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt; Cookie mycook = new Cookie(&quot;name&quot;,&quot;value&quot;);
 response.addCookie(mycook);
 Cookie killmycook = new Cookie(&quot;mycook&quot;,&quot;value&quot;);
 killmycook.setMaxAge(0);
 killmycook.setPath(&quot;/&quot;);
 killmycook.addCookie(killmycook);
&lt;/pre&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How do I mix JSP and SSI #include?&lt;/b&gt;- If you’re just including raw HTML, use the #include directive as usual inside your .jsp file.&lt;pre style=&quot;background-color: transparent; border: 0px; font-family: Monaco, monospace; font-size: 11px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt; &amp;lt;!--#include file=&quot;data.inc&quot;--&amp;gt;
&lt;/pre&gt;
&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
But it’s a little trickier if you want the server to evaluate any JSP code that’s inside the included file. If your data.inc file contains jsp code you will have to use&lt;/div&gt;
&lt;pre style=&quot;background-color: transparent; border: 0px; font-family: Monaco, monospace; font-size: 11px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt; &amp;lt;%@ vinclude=&quot;data.inc&quot; %&amp;gt;
&lt;/pre&gt;
&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
The &amp;lt;!–#include file=&quot;data.inc&quot;–&amp;gt; is used for including non-JSP files.&lt;/div&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;I made my class Cloneable but I still get&amp;nbsp;&lt;i style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can’t access protected method clone&lt;/i&gt;. Why?&lt;/b&gt;- Some of the Java books imply that all you have to do in order to have your class support clone() is implement the Cloneable interface. Not so. Perhaps that was the intent at some point, but that’s not the way it works currently. As it stands, you have to implement your own public clone() method, even if it doesn’t do anything special and just calls super.clone().&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Why is XML such an important development?&lt;/b&gt;- It removes two constraints which were holding back Web developments: dependence on a single, inflexible document type (HTML) which was being much abused for tasks it was never designed for; the complexity of full SGML, whose syntax allows many powerful but hard-to-program options. XML allows the flexible development of user-defined document types. It provides a robust, non-proprietary, persistent, and verifiable file format for the storage and transmission of text and data both on and off the Web; and it removes the more complex options of SGML, making it easier to program for.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the fastest type of JDBC driver?&lt;/b&gt;- JDBC driver performance will depend on a number of issues:&lt;ul style=&quot;background-color: transparent; border: 0px; list-style: square; margin: 0px 0px 18px 1.5em; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;the quality of the driver code,&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;the size of the driver code,&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;the database server and its load,&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;network topology,&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;the number of times your request is translated to a different API.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
In general, all things being equal, you can assume that the more your request and response change hands, the slower it will be. This means that Type 1 and Type 3 drivers will be slower than Type 2 drivers (the database calls are make at least three translations versus two), and Type 4 drivers are the fastest (only one translation).&lt;/div&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How do I find whether a parameter exists in the request object?&lt;/b&gt;&lt;pre style=&quot;background-color: transparent; border: 0px; font-family: Monaco, monospace; font-size: 11px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;boolean hasFoo = !(request.getParameter(&quot;foo&quot;) == null
 || request.getParameter(&quot;foo&quot;).equals(&quot;&quot;));
&lt;/pre&gt;
&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
or&lt;/div&gt;
&lt;pre style=&quot;background-color: transparent; border: 0px; font-family: Monaco, monospace; font-size: 11px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;boolean hasParameter =
 request.getParameterMap().contains(theParameter); //(which works in Servlet 2.3+)
&lt;/pre&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How can I send user authentication information while makingURLConnection?&lt;/b&gt;- You’ll want to use HttpURLConnection.setRequestProperty and set all the appropriate headers to HTTP authorization.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/6748860995943582438/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2012/06/jdbc-and-jsp-interview-questions-and.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/6748860995943582438'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/6748860995943582438'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2012/06/jdbc-and-jsp-interview-questions-and.html' title='JDBC and JSP interview questions and answers'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-7000175425338275230</id><published>2012-06-27T20:13:00.000+05:30</published><updated>2012-06-27T20:13:43.267+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Advanced EJB interview questions"/><title type='text'>Advanced EJB interview questions</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;ol style=&quot;border: 0px; font-family: Georgia, serif; font-size: 12px; line-height: 18px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 18px 3em; outline: 0px; padding: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Are enterprise beans allowed to use Thread.sleep()?&lt;/b&gt;&amp;nbsp;- Enterprise beans make use of the services provided by the EJB container, such as life-cycle management. To avoid conflicts with these services, enterprise beans are restricted from performing certain operations: Managing or synchronizing threads&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Is it possible to write two EJB’s that share the same Remote and Home interfaces, and have different bean classes? if so, what are the advantages/disadvantages?&lt;/b&gt;&amp;nbsp;- It’s certainly possible. In fact, there’s an example that ships with the Inprise Application Server of an Account interface with separate implementations for CheckingAccount and SavingsAccount, one of which was CMP and one of which was BMP.&lt;/li&gt;
&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span id=&quot;more-147&quot; style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Is it possible to specify multiple JNDI names when deploying an EJB?&lt;/b&gt;&amp;nbsp;- No. To achieve this you have to deploy your EJB multiple times each specifying a different JNDI name.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Is there any way to force an Entity Bean to store itself to the db? I don’t wanna wait for the container to update the db, I want to do it NOW! Is it possible?&lt;/b&gt;&amp;nbsp;- Specify the transaction attribute of the bean as RequiresNew. Then as per section 11.6.2.4 of the EJB v 1.1 spec EJB container automatically starts a new transaction before the method call. The container also performs the commit protocol before the method result is sent to the client.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;I am developing a BMP Entity bean. I have noticed that whenever the create method is invoked, the ejbLoad() and the ejbStore() methods are also invoked. I feel that once my database insert is done, having to do a select and update SQL queries is major overhead. is this behavior typical of all EJB containers? Is there any way to suppress these invocations?&lt;/b&gt;&amp;nbsp;- This is the default behaviour for EJB. The specification states that ejbLoad() will be called before every transaction and ejbStore() after every transaction. Each Vendor has optimizations, which are proprietary for this scenario.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can an EJB send asynchronous notifications to its clients?&lt;/b&gt;&amp;nbsp;- Asynchronous notification is a known hole in the first versions of the EJB spec. The recommended solution to this is to use JMS, which is becoming available in J2EE-compliant servers. The other option, of course, is to use client-side threads and polling. This is not an ideal solution, but it’s workable for many scenarios.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How can I access EJB from ASP?&lt;/b&gt;&amp;nbsp;- You can use the Java 2 Platform, Enterprise Edition Client Access Services (J2EETM CAS) COM Bridge 1.0, currently downloadable from&amp;nbsp;&lt;a href=&quot;http://developer.java.sun.com/developer/earlyAccess/j2eecas/&quot; style=&quot;background-color: transparent; border: 0px; color: #2772b3; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;&quot;&gt;Sun&lt;/a&gt;&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Is there a guarantee of uniqueness for entity beans?&lt;/b&gt;&amp;nbsp;- There is no such guarantee. The server (or servers) can instantiate as many instances of the same underlying Entity Bean (with the same PK) as it wants. However, each instance is guaranteed to have up-to-date data values, and be transactionally consistent, so uniqueness is not required. This allows the server to scale the system to support multiple threads, multiple concurrent requests, and multiple hosts.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How do the six transaction attributes map to isolation levels like “dirty read”? Will an attribute like “Required” lock out other readers until I’m finished updating?&lt;/b&gt;&amp;nbsp;- The Transaction Attributes in EJB do not map to the Transaction Isolation levels used in JDBC. This is a common misconception. Transaction Attributes specify to the container when a Transaction should be started, suspended(paused) and committed between method invocations on Enterprise JavaBeans. For more details and a summary of Transaction Attributes refer to section 11.6 of the EJB 1.1 specification.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;I have created a remote reference to an EJB in FirstServlet. Can I put the reference in a servlet session and use that in SecondServlet?&lt;/b&gt;&amp;nbsp;- Yes. The EJB client (in this case your servlet) acquires a remote reference to an EJB from the Home Interface; that reference is serializable and can be passed from servlet to servlet. If it is a session bean, then the EJB server will consider your web client’s servlet session to correspond to a single EJB session, which is usually (but not always) what you want.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can the primary key in the entity bean be a Java primitive type such as int?&lt;/b&gt;- The primary key can’t be a primitive type–use the primitive wrapper classes, instead. For example, you can use java.lang.Integer as the primary key class, but not int (it has to be a class, not a primitive)&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What’s new in the EJB 2.0 specification?&lt;/b&gt;&amp;nbsp;- Following are the main features supported in EJB 2.0: Integration of EJB with JMS, Message Driven Beans, Implement additional Business methods in Home interface which are not specific for bean instance, EJB QL.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How many types of protocol implementations does RMI have?&lt;/b&gt;&amp;nbsp;- RMI has at least three protocol implementations: Java Remote Method Protocol(JRMP), Internet Inter ORB Protocol(IIOP), and Jini Extensible Remote Invocation(JERI). These are alternatives, not part of the same thing, All three are indeed layer 6 protocols for those who are still speaking OSI reference model.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the need of Remote and Home interfaces. Why can’t there be one?&lt;/b&gt;&amp;nbsp;- In a few words, I would say that the main reason is because there is a clear division of roles and responsabilities between the two interfaces. The home interface is your way to communicate with the container, that is who is responsable of creating, locating even removing one or more beans. The remote interface is your link to the bean, that will allow you to remotely access to all its methods and members. As you can see there are two distinct elements (the container and the beans) and you need two different interfaces for accessing to both of them.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between Java Beans and EJB?&lt;/b&gt;&amp;nbsp;- Java Beans are client-side objects and EJBs are server side object, and they have completely different development, lifecycle, purpose.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Question With regard to Entity Beans, what happens if both my EJB Server and Database crash, what will happen to unsaved changes? Is there any transactional log file used?&lt;/b&gt;&amp;nbsp;- Actually, if your EJB server crashes, you will not even be able to make a connection to the server to perform a bean lookup, as the server will no longer be listening on the port for incoming JNDI lookup requests. You will lose any data that wasn’t committed prior to the crash. This is where you should start looking into clustering your EJB server. Any unsaved and uncommited changes are lost the moment your EJB Server crashes. If your database also crashes, then all the saved changes are also lost unless you have some backup or some recovery mechanism to retrieve the data. So consider database replication and EJB Clustering for such scenarios, though the occurence of such a thing is very very rare. Thx, Uma All databse have the concept of log files(for exampe oracle have redo log files concept). So if data bases crashes then on starting up they fill look up the log files to perform all pending jobs. But is EJB crashes, It depend upon the container how frequenlty it passivates or how frequesntly it refreshes the data with Database.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Question Can you control when passivation occurs?&lt;/b&gt;&amp;nbsp;- The developer, according to the specification, cannot directly control when passivation occurs. Although for Stateful Session Beans, the container cannot passivate an instance that is inside a transaction. So using transactions can be a a strategy to control passivation. The ejbPassivate() method is called during passivation, so the developer has control over what to do during this exercise and can implement the require optimized logic. Some EJB containers, such as BEA WebLogic, provide the ability to tune the container to minimize passivation calls. Taken from the WebLogic 6.0 DTD - “The passivation-strategy can be either “default” or “transaction”. With the default setting the container will attempt to keep a working set of beans in the cache. With the “transaction” setting, the container will passivate the bean after every transaction (or method call for a non-transactional invocation).”&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Does RMI-IIOP support dynamic downloading of classes?&lt;/b&gt;&amp;nbsp;- No, RMI-IIOP doesn’t support dynamic downloading of the classes as it is done with CORBA in DII (Dynamic Interface Invocation).Actually RMI-IIOP combines the usability of Java Remote Method Invocation (RMI) with the interoperability of the Internet Inter-ORB Protocol (IIOP).So in order to attain this interoperability between RMI and CORBA,some of the features that are supported by RMI but not CORBA and vice versa are eliminated from the RMI-IIOP specification.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Does EJB 1.1 support mandate the support for RMI-IIOP ? What is the meaning of “the client API must support the Java RMI-IIOP programming model for portability, but the underlying protocol can be anything” ?&lt;/b&gt;&amp;nbsp;- EJB1.1 does mandate the support of RMI-IIOP. There are 2 types of implementations that an EJB Server might provide: CORBA-based EJB Servers and Proprietry EJB Servers. Both support the RMI-IIOP API but how that API is implemented is a different story. (NB: By API we mean the interface provided to the client by the stub or proxy). A CORBA-based EJB Server actually implements its EJB Objects as CORBA Objects (it therefore encorporates an ORB and this means that EJB’s can be contacted by CORBA clients (as well as RMI-IIOP clients) A proprietry EJB still implements the RMI-IIOP API (in the client’s stub) but the underlying protocol can be anything. Therefore your EJB’s CANNOT be contacted by CORBA clients. The difference is that in both cases, your clients see the same API (hence, your client portability) BUT how the stubs communicate with the server is different.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;The EJB specification says that we cannot use Bean Managed Transaction in Entity Beans. Why?&lt;/b&gt;&amp;nbsp;- The short, practical answer is… because it makes your entity beans useless as a reusable component. Also, transaction management is best left to the application server - that’s what they’re there for. It’s all about atomic operations on your data. If an operation updates more than one entity then you want the whole thing to succeed or the whole thing to fail, nothing in between. If you put commits in the entity beans then it’s very difficult to rollback if an error occurs at some point late in the operation.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can I invoke Runtime.gc() in an EJB?&lt;/b&gt;&amp;nbsp;- You shouldn’t. What will happen depends on the implementation, but the call will most likely be ignored. You should leave system level management like garbage collection for the container to deal with. After all, that’s part of the benefit of using EJBs, you don’t have to manage resources yourself.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is clustering? What are the different algorithms used for clustering?&lt;/b&gt;&amp;nbsp;- Clustering is grouping machines together to transparantly provide enterprise services.The client does not now the difference between approaching one server or approaching a cluster of servers.Clusters provide two benefits: scalability and high availability. Further information can be found in the JavaWorld article J2EE Clustering.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the advantage of using Entity bean for database operations, over directly using JDBC API to do database operations? When would I use one over the other?&lt;/b&gt;&amp;nbsp;- Entity Beans actually represents the data in a database. It is not that Entity Beans replaces JDBC API. There are two types of Entity Beans Container Managed and Bean Mananged. In Container Managed Entity Bean - Whenever the instance of the bean is created the container automatically retrieves the data from the DB/Persistance storage and assigns to the object variables in bean for user to manipulate or use them. For this the developer needs to map the fields in the database to the variables in deployment descriptor files (which varies for each vendor). In the Bean Managed Entity Bean - The developer has to specifically make connection, retrive values, assign them to the objects in the ejbLoad() which will be called by the container when it instatiates a bean object. Similarly in the ejbStore() the container saves the object values back the the persistance storage. ejbLoad and ejbStore are callback methods and can be only invoked by the container. Apart from this, when you use Entity beans you dont need to worry about database transaction handling, database connection pooling etc. which are taken care by the ejb container. But in case of JDBC you have to explicitly do the above features. what suresh told is exactly perfect. ofcourse, this comes under the database transations, but i want to add this. the great thing about the entity beans of container managed, whenever the connection is failed during the transaction processing, the database consistancy is mantained automatically. the container writes the data stored at persistant storage of the entity beans to the database again to provide the database consistancy. where as in jdbc api, we, developers has to do manually.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the role of serialization in EJB?&lt;/b&gt;&amp;nbsp;- A big part of EJB is that it is a framework for underlying RMI: remote method invocation. You’re invoking methods remotely from JVM space ‘A’ on objects which are in JVM space ‘B’ — possibly running on another machine on the network. To make this happen, all arguments of each method call must have their current state plucked out of JVM ‘A’ memory, flattened into a byte stream which can be sent over a TCP/IP network connection, and then deserialized for reincarnation on the other end in JVM ‘B’ where the actual method call takes place. If the method has a return value, it is serialized up for streaming back to JVM A. Thus the requirement that all EJB methods arguments and return values must be serializable. The easiest way to do this is to make sure all your classes implement java.io.Serializable.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is EJB QL?&lt;/b&gt;&amp;nbsp;- EJB QL is a Query Language provided for navigation across a network of enterprise beans and dependent objects defined by means of container managed persistence. EJB QL is introduced in the EJB 2.0 specification. The EJB QL query language defines finder methods for entity beans with container managed persistenceand is portable across containers and persistence managers. EJB QL is used for queries of two types of finder methods: Finder methods that are defined in the home interface of an entity bean and which return entity objects. Select methods, which are not exposed to the client, but which are used by the Bean Provider to select persistent values that are maintained by the Persistence Manager or to select entity objects that are related to the entity bean on which the query is defined.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/7000175425338275230/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2012/06/advanced-ejb-interview-questions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/7000175425338275230'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/7000175425338275230'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2012/06/advanced-ejb-interview-questions.html' title='Advanced EJB interview questions'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-5043782110075396214</id><published>2012-06-27T19:59:00.000+05:30</published><updated>2012-06-27T19:59:31.309+05:30</updated><title type='text'>Junior Java programmer interview questions</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;ol style=&quot;border: 0px; font-family: Georgia, serif; font-size: 12px; line-height: 18px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 18px 3em; outline: 0px; padding: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the purpose of finalization?&lt;/b&gt;&amp;nbsp;- The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between the Boolean &amp;amp; operator and the &amp;amp;&amp;amp; operator?&lt;/b&gt;- If an expression involving the Boolean &amp;amp; operator is evaluated, both operands are evaluated. Then the &amp;amp; operator is applied to the operand. When an expression involving the &amp;amp;&amp;amp; operator is evaluated, the first operand is evaluated. If the first operand returns a value of true then the second operand is evaluated. The &amp;amp;&amp;amp; operator is then applied to the first and second operands. If the first operand evaluates to false, the evaluation of the second operand is skipped.&lt;/li&gt;
&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span id=&quot;more-146&quot; style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How many times may an object’s finalize() method be invoked by the garbage collector?&lt;/b&gt;&amp;nbsp;- An object’s finalize() method may only be invoked once by the garbage collector.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the purpose of the finally clause of a try-catch-finally statement?&lt;/b&gt;&amp;nbsp;- The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown or caught.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the argument type of a program’s main() method?&lt;/b&gt;&amp;nbsp;- A program’s main() method takes an argument of the String[] type.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Which Java operator is right associative?&lt;/b&gt;&amp;nbsp;- The = operator is right associative.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can a double value be cast to a byte?&lt;/b&gt;&amp;nbsp;- Yes, a double value can be cast to a byte.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between a break statement and a continue statement?&lt;/b&gt;- A break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What must a class do to implement an interface?&lt;/b&gt;&amp;nbsp;- It must provide all of the methods in the interface and identify the interface in its implements clause.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the advantage of the event-delegation model over the earlier event-inheritance model?&lt;/b&gt;&amp;nbsp;- The event-delegation model has two advantages over the event-inheritance model. First, it enables event handling to be handled by objects other than the ones that generate the events (or their containers). This allows a clean separation between a component’s design and its use. The other advantage of the event-delegation model is that it performs much better in applications where many events are generated. This performance improvement is due to the fact that the event-delegation model does not have to repeatedly process unhandled events, as is the case of the event-inheritance model.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How are commas used in the intialization and iteration parts of a for statement?&lt;/b&gt;&amp;nbsp;- Commas are used to separate multiple statements within the initialization and iteration parts of a for statement.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is an abstract method?&lt;/b&gt;&amp;nbsp;- An abstract method is a method whose implementation is deferred to a subclass.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What value does read() return when it has reached the end of a file?&lt;/b&gt;&amp;nbsp;- The read() method returns -1 when it has reached the end of a file.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can a Byte object be cast to a double value?&lt;/b&gt;&amp;nbsp;- No, an object cannot be cast to a primitive value.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between a static and a non-static inner class?&lt;/b&gt;&amp;nbsp;- A non-static inner class may have object instances that are associated with instances of the class’s outer class. A static inner class does not have any object instances.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;If a variable is declared as private, where may the variable be accessed?&lt;/b&gt;&amp;nbsp;- A private variable may only be accessed within the class in which it is declared.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is an object’s lock and which object’s have locks?&lt;/b&gt;&amp;nbsp;- An object’s lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object’s lock. All objects and classes have locks. A class’s lock is acquired on the class’s Class object.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the % operator?&lt;/b&gt;&amp;nbsp;- It is referred to as the modulo or remainder operator. It returns the remainder of dividing the first operand by the second operand.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;When can an object reference be cast to an interface reference?&lt;/b&gt;&amp;nbsp;- An object reference be cast to an interface reference when the object implements the referenced interface.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Which class is extended by all other classes?&lt;/b&gt;&amp;nbsp;- The Object class is extended by all other classes.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can an object be garbage collected while it is still reachable?&lt;/b&gt;&amp;nbsp;- A reachable object cannot be garbage collected. Only unreachable objects may be garbage collected.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Is the ternary operator written x : y ? z or x ? y : z ?&lt;/b&gt;&amp;nbsp;- It is written x ? y : z.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How is rounding performed under integer division?&lt;/b&gt;&amp;nbsp;- The fractional part of the result is truncated. This is known as rounding toward zero.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?&lt;/b&gt;&amp;nbsp;- The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What classes of exceptions may be caught by a catch clause?&lt;/b&gt;&amp;nbsp;- A catch clause can catch any exception that may be assigned to the Throwable type. This includes the Error and Exception types.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;If a class is declared without any access modifiers, where may the class be accessed?&lt;/b&gt;&amp;nbsp;- A class that is declared without any access modifiers is said to have package access. This means that the class can only be accessed by other classes and interfaces that are defined within the same package.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Does a class inherit the constructors of its superclass?&lt;/b&gt;&amp;nbsp;- A class does not inherit constructors from any of its superclasses.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the purpose of the System class?&lt;/b&gt;&amp;nbsp;- The purpose of the System class is to provide access to system resources.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Name the eight primitive Java types.&lt;/b&gt;&amp;nbsp;- The eight primitive types are byte, char, short, int, long, float, double, and boolean.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Which class should you use to obtain design information about an object?&lt;/b&gt;&amp;nbsp;- The Class class is used to obtain information about an object’s design.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/5043782110075396214/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2012/06/junior-java-programmer-interview.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/5043782110075396214'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/5043782110075396214'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2012/06/junior-java-programmer-interview.html' title='Junior Java programmer interview questions'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-7212147343096026219</id><published>2012-06-27T19:54:00.000+05:30</published><updated>2012-06-27T19:54:07.948+05:30</updated><title type='text'>Core Java interview questions</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;ol style=&quot;border: 0px; font-family: Georgia, serif; font-size: 12px; line-height: 18px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 18px 3em; outline: 0px; padding: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can there be an abstract class with no abstract methods in it?&lt;/b&gt;&amp;nbsp;- Yes&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can an Interface be final?&lt;/b&gt;&amp;nbsp;- No&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can an Interface have an inner class?&lt;/b&gt;&amp;nbsp;- Yes.&lt;span id=&quot;more-145&quot; style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;pre style=&quot;background-color: transparent; border: 0px; font-family: Monaco, monospace; font-size: 11px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt; public interface abc
 {
  static int i=0; void dd();
  class a1
  {
   a1()
   {
    int j;
    System.out.println(&quot;inside&quot;);
   };
   public static void main(String a1[])
   {
    System.out.println(&quot;in interfia&quot;);
   }
  }
 }
&lt;/pre&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can we define private and protected modifiers for variables in interfaces?&lt;/b&gt;&amp;nbsp;- No&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is Externalizable?&lt;/b&gt;&amp;nbsp;- Externalizable is an Interface that extends Serializable Interface. And sends data into Streams in Compressed Format. It has two methods, writeExternal(ObjectOuput out) and readExternal(ObjectInput in)&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What modifiers are allowed for methods in an Interface?&lt;/b&gt;&amp;nbsp;- Only public and abstract modifiers are allowed for methods in interfaces.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is a local, member and a class variable?&lt;/b&gt;&amp;nbsp;- Variables declared within a method are “local” variables. Variables declared within the class i.e not within any methods are “member” variables (global variables). Variables declared within the class i.e not within any methods and are defined as “static” are class variables&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What are the different identifier states of a Thread?&lt;/b&gt;&amp;nbsp;- The different identifiers of a Thread are: R - Running or runnable thread, S - Suspended thread, CW - Thread waiting on a condition variable, MW - Thread waiting on a monitor lock, MS - Thread suspended waiting on a monitor lock&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What are some alternatives to inheritance?&lt;/b&gt;&amp;nbsp;- Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn’t force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Why isn’t there operator overloading?&lt;/b&gt;&amp;nbsp;- Because C++ has proven by example that operator overloading makes code almost impossible to maintain. In fact there very nearly wasn’t even method overloading in Java, but it was thought that this was too useful for some very basic methods like print(). Note that some of the classes like DataOutputStream have unoverloaded methods like writeInt() and writeByte().&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What does it mean that a method or field is “static”?&lt;/b&gt;&amp;nbsp;- Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class. Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That’s how library methods like System.out.println() work. out is a static field in the java.lang.System class.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How do I convert a numeric IP address like 192.18.97.39 into a hostname like java.sun.com?&lt;/b&gt;&lt;pre style=&quot;background-color: transparent; border: 0px; font-family: Monaco, monospace; font-size: 11px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt; String hostname = InetAddress.getByName(&quot;192.18.97.39&quot;).getHostName();
&lt;/pre&gt;
&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Difference between JRE/JVM/JDK?&lt;/b&gt;&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Why do threads block on I/O?&lt;/b&gt;&amp;nbsp;- Threads block on i/o (that is enters the waiting state) so that other threads may execute while the I/O operation is performed.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is synchronization and why is it important?&lt;/b&gt;&amp;nbsp;- With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to significant errors.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Is null a keyword?&lt;/b&gt;&amp;nbsp;- The null value is not a keyword.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Which characters may be used as the second character of an identifier,but not as the first character of an identifier?&lt;/b&gt;&amp;nbsp;- The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What modifiers may be used with an inner class that is a member of an outer class?&lt;/b&gt;&amp;nbsp;- A (non-local) inner class may be declared as public, protected, private, static, final, or abstract.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?&lt;/b&gt;&amp;nbsp;- Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What are wrapped classes?&lt;/b&gt;&amp;nbsp;- Wrapped classes are classes that allow primitive types to be accessed as objects.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What restrictions are placed on the location of a package statement within a source code file?&lt;/b&gt;&amp;nbsp;- A package statement must appear as the first line in a source code file (excluding blank lines and comments).&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the difference between preemptive scheduling and time slicing?&lt;/b&gt;&amp;nbsp;- Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is a native method?&lt;/b&gt;&amp;nbsp;- A native method is a method that is implemented in a language other than Java.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What are order of precedence and associativity, and how are they used?&lt;/b&gt;&amp;nbsp;- Order of precedence determines the order in which operators are evaluated in expressions. Associatity determines whether an expression is evaluated left-to-right or right-to-left&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the catch or declare rule for method declarations?&lt;/b&gt;&amp;nbsp;- If a checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can an anonymous class be declared as implementing an interface and extending a class?&lt;/b&gt;&amp;nbsp;- An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the range of the char type?&lt;/b&gt;&amp;nbsp;- The range of the char type is 0 to 2^16 - 1.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/7212147343096026219/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2012/06/core-java-interview-questions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/7212147343096026219'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/7212147343096026219'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2012/06/core-java-interview-questions.html' title='Core Java interview questions'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-3776835880873710166</id><published>2012-06-27T19:47:00.000+05:30</published><updated>2012-06-27T19:47:43.863+05:30</updated><title type='text'>Java interview questions and answers</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;ol style=&quot;background-color: white; border: 0px; font-family: Georgia, serif; font-size: 12px; line-height: 18px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 18px 3em; outline: 0px; padding: 0px; text-align: -webkit-auto; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is garbage collection? What is the process that is responsible for doing that in java?&lt;/b&gt;&amp;nbsp;- Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this process&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What kind of thread is the Garbage collector thread?&lt;/b&gt;&amp;nbsp;- It is a daemon thread.&lt;/li&gt;
&lt;div style=&quot;background-color: transparent; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span id=&quot;more-144&quot; style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is a daemon thread?&lt;/b&gt;&amp;nbsp;- These are the threads which can run without user intervention. The JVM can exit when there are daemon thread by killing them abruptly.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How will you invoke any external process in Java?&lt;/b&gt;&amp;nbsp;- Runtime.getRuntime().exec(….)&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the finalize method do?&lt;/b&gt;&amp;nbsp;- Before the invalid objects get garbage collected, the JVM give the user a chance to clean up some resources before it got garbage collected.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is mutable object and immutable object?&lt;/b&gt;&amp;nbsp;- 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, …)&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the basic difference between string and stringbuffer object?&lt;/b&gt;&amp;nbsp;- String is an immutable object. StringBuffer is a mutable object.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the purpose of Void class?&lt;/b&gt;&amp;nbsp;- The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is reflection?&lt;/b&gt;&amp;nbsp;- 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.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the base class for Error and Exception?&lt;/b&gt;&amp;nbsp;- Throwable&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the byte range?&lt;/b&gt;&amp;nbsp;-128 to 127&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the implementation of destroy method in java.. is it native or java code?&lt;/b&gt;&amp;nbsp;- This method is not implemented.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is a package?&lt;/b&gt;&amp;nbsp;- To group set of classes into a single unit is known as packaging. Packages provides wide namespace ability.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What are the approaches that you will follow for making a program very efficient?&lt;/b&gt;&amp;nbsp;- 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.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is a DatabaseMetaData?&lt;/b&gt;&amp;nbsp;- Comprehensive information about the database as a whole.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is Locale?&lt;/b&gt;&amp;nbsp;- A Locale object represents a specific geographical, political, or cultural region&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How will you load a specific locale?&lt;/b&gt;&amp;nbsp;- Using ResourceBundle.getBundle(…);&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is JIT and its use?&lt;/b&gt;&amp;nbsp;- 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.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Is JVM a compiler or an interpreter?&lt;/b&gt;&amp;nbsp;- Interpreter&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;When you think about optimization, what is the best way to findout the time/memory consuming process?&lt;/b&gt;&amp;nbsp;- Using profiler&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the purpose of assert keyword used in JDK1.4.x?&lt;/b&gt;&amp;nbsp;- 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.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;How will you get the platform dependent values like line separator, path separator, etc., ?&lt;/b&gt;&amp;nbsp;- Using Sytem.getProperty(…) (line.separator, path.separator, …)&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is skeleton and stub? what is the purpose of those?&lt;/b&gt;&amp;nbsp;- 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.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the final keyword denotes?&lt;/b&gt;&amp;nbsp;- 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.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the significance of ListIterator?&lt;/b&gt;&amp;nbsp;- You can iterate back and forth.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is the major difference between LinkedList and ArrayList?&lt;/b&gt;&amp;nbsp;- LinkedList are meant for sequential accessing. ArrayList are meant for random accessing.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is nested class?&lt;/b&gt;&amp;nbsp;- If all the methods of a inner class is static then it is a nested class.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is inner class?&lt;/b&gt;&amp;nbsp;- If the methods of the inner class can only be accessed via the instance of the inner class, then it is called inner class.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is composition?&lt;/b&gt;&amp;nbsp;- Holding the reference of the other class within some other class is known as composition.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is aggregation?&lt;/b&gt;&amp;nbsp;- 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.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What are the methods in Object?&lt;/b&gt;&amp;nbsp;- clone, equals, wait, finalize, getClass, hashCode, notify, notifyAll, toString&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Can you instantiate the Math class?&lt;/b&gt;&amp;nbsp;- You can’t instantiate the math class. All the methods in this class are static. And the constructor is not public.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is singleton?&lt;/b&gt;&amp;nbsp;- 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., public class Singleton { private static final Singleton s = new Singleton(); private Singleton() { } public static Singleton getInstance() { return s; } // all non static methods … }&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is DriverManager?&lt;/b&gt;&amp;nbsp;- The basic service to manage set of JDBC drivers.&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;What is Class.forName() does and how it is useful?&lt;/b&gt;&amp;nbsp;- It loads the class into the ClassLoader. It returns the Class. Using that you can get the instance ( “class-instance”.newInstance() ).&amp;nbsp;&lt;/li&gt;
&lt;li style=&quot;background-color: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;b style=&quot;border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Inq&lt;/b&gt;&amp;nbsp;adds a question: Expain the reason for each keyword of&lt;br /&gt;&lt;blockquote style=&quot;border: 0px; font-size: 13px; font-style: italic; margin: 0px 3em; outline: 0px; padding: 0px; quotes: none; vertical-align: baseline;&quot;&gt;
public static void main(String args[])&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/3776835880873710166/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2012/06/java-interview-questions-and-answers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/3776835880873710166'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/3776835880873710166'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2012/06/java-interview-questions-and-answers.html' title='Java interview questions and answers'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-8642446603905069135</id><published>2012-05-02T17:31:00.000+05:30</published><updated>2012-05-13T01:27:06.478+05:30</updated><title type='text'>How search engine works</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;b&gt;How Search Engines Work:&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;
The term &quot;search engine&quot; is often used generically to describe both crawler-based search engines and human-powered directories. These two types of search engines gather their listings in radically different ways.

&lt;b&gt;Crawler-Based Search Engines&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.000webhost.com/&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;Web hosting&quot; border=&quot;0&quot; height=&quot;60&quot; src=&quot;http://www.000webhost.com/images/banners/468x60/banner6.gif&quot; width=&quot;468&quot; /&gt;&lt;/a&gt;
Crawler-based search engines, such as Google, create their listings automatically. They &quot;crawl&quot; or &quot;spider&quot; the web, then people search through what they have found.

If you change your web pages, crawler-based search engines eventually find these changes, and that can affect how you are listed. Page titles, body copy and other elements all play a role.&lt;br /&gt;
&lt;b&gt;
Human-Powered Directories&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
A human-powered directory, such as the Open Directory, depends on humans for its listings. You submit a short description to the directory for your entire site, or editors write one for sites they review. A search looks for matches only in the descriptions submitted.

Changing your web pages has no effect on your listing. Things that are useful for improving a listing with a search engine have nothing to do with improving a listing in a directory. The only exception is that a good site, with good content, might be more likely to get reviewed for free than a poor site.&lt;br /&gt;
&lt;b&gt;&quot;Hybrid Search Engines&quot; Or Mixed Results&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
In the web&#39;s early days, it used to be that a search engine either presented crawler-based results or human-powered listings. Today, it extremely common for both types of results to be presented. Usually, a hybrid search engine will favor one type of listings over another. For example, MSN Search is more likely to present human-powered listings from LookSmart. However, it does also present crawler-based results (as provided by Inktomi), especially for more obscure queries.
Search Engine Watch Members have access to in-depth information and get extra benefits.
Learn more about becoming a Member.&lt;br /&gt;
&lt;b&gt;The Parts Of A Crawler-Based Search Engine&lt;/b&gt;
&lt;br /&gt;
Crawler-based search engines have three major elements. First is the spider, also called the crawler. The spider visits a web page, reads it, and then follows links to other pages within the site. This is what it means when someone refers to a site being &quot;spidered&quot; or &quot;crawled.&quot; The spider returns to the site on a regular basis, such as every month or two, to look for changes.

Everything the spider finds goes into the second part of the search engine, the index. The index, sometimes called the catalog, is like a giant book containing a copy of every web page that the spider finds. If a web page changes, then this book is updated with new information.

Sometimes it can take a while for new pages or changes that the spider finds to be added to the index. Thus, a web page may have been &quot;spidered&quot; but not yet &quot;indexed.&quot; Until it is indexed -- added to the index -- it is not available to those searching with the search engine.

Search engine software is the third part of a search engine. This is the program that sifts through the millions of pages recorded in the index to find matches to a search and rank them in order of what it believes is most relevant. You can learn more about how search engine software ranks web pages on the aptly-named How Search Engines Rank Web Pages page.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Major Search Engines: The Same, But Different&lt;/b&gt;
&lt;br /&gt;
All crawler-based search engines have the basic parts described above, but there are differences in how these parts are tuned. That is why the same search on different search engines often produces different results. Some of the significant differences between the major crawler-based search engines are summarized on the Search Engine Features Page. Information on this page has been drawn from the help pages of each search engine, along with knowledge gained from articles, reviews, books, independent research, tips from others and additional information received directly from the various search engines.


&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/8642446603905069135/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2012/05/how-search-engine-works.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/8642446603905069135'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/8642446603905069135'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2012/05/how-search-engine-works.html' title='How search engine works'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-5370944901492743272</id><published>2011-12-17T15:25:00.001+05:30</published><updated>2012-05-13T01:28:47.362+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="swing 3D effect"/><title type='text'>swing 3D effect on lable</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;a href=&quot;http://www.000webhost.com/&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;Web hosting&quot; border=&quot;0&quot; height=&quot;60&quot; src=&quot;http://www.000webhost.com/images/banners/468x60/banner6.gif&quot; width=&quot;468&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
/*&lt;br /&gt;
&amp;nbsp;* To change this template, choose Tools | Templates&lt;br /&gt;
&amp;nbsp;* and open the template in the editor.&lt;br /&gt;
&amp;nbsp;*/&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
&amp;nbsp;*&lt;br /&gt;
&amp;nbsp;* @author akhtar&lt;br /&gt;
&amp;nbsp;*/&lt;br /&gt;
&lt;br /&gt;
import java.awt.Color;&lt;br /&gt;
import java.awt.Dimension;&lt;br /&gt;
import java.awt.FontMetrics;&lt;br /&gt;
import java.awt.Graphics;&lt;br /&gt;
import java.awt.Graphics2D;&lt;br /&gt;
import java.awt.RenderingHints;&lt;br /&gt;
import java.awt.font.LineMetrics;&lt;br /&gt;
&lt;br /&gt;
import javax.swing.JFrame;&lt;br /&gt;
import javax.swing.JLabel;&lt;br /&gt;
&lt;br /&gt;
public class RichJLabel extends JLabel {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; private int tracking;&lt;br /&gt;
&amp;nbsp; public RichJLabel(String text, int tracking) {&lt;br /&gt;
&amp;nbsp; super(text);&lt;br /&gt;
&amp;nbsp; this.tracking = tracking;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; private int left_x, left_y, right_x, right_y;&lt;br /&gt;
&amp;nbsp; private Color left_color, right_color;&lt;br /&gt;
&amp;nbsp; public void setLeftShadow(int x, int y, Color color) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; left_x = x;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; left_y = y;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; left_color = color;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public void setRightShadow(int x, int y, Color color) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; right_x = x;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; right_y = y;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; right_color = color;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public Dimension getPreferredSize() {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; String text = getText();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FontMetrics fm = this.getFontMetrics(getFont());&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int w = fm.stringWidth(text);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; w += (text.length() - 1) * tracking;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; w += left_x + right_x;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int h = fm.getHeight();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; h += left_y + right_y;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return new Dimension(w, h);&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public void paintComponent(Graphics g) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RenderingHints.VALUE_TEXT_ANTIALIAS_ON);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; char[] chars = getText().toCharArray();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FontMetrics fm = this.getFontMetrics(getFont());&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int h = fm.getAscent();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; LineMetrics lm = fm.getLineMetrics(getText(), g);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; g.setFont(getFont());&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int x = 0;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int i = 0; i &amp;lt; chars.length; i++) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; char ch = chars[i];&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int w = fm.charWidth(ch) + tracking;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.setColor(left_color);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.drawString(&quot;&quot; + chars[i], x - left_x, h - left_y);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.setColor(right_color);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.drawString(&quot;&quot; + chars[i], x + right_x, h + right_y);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.setColor(getForeground());&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; g.drawString(&quot;&quot; + chars[i], x, h);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x += w;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public static void main(String[] args) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; RichJLabel label = new RichJLabel(&quot;Akhtar Nawaz&quot;, 0);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; label.setLeftShadow(1, 1, Color.white);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; label.setRightShadow(1, 1, Color.white);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; label.setForeground(Color.blue);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; label.setFont(label.getFont().deriveFont(140f));&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; label.setLeftShadow(5,5,Color.white);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; label.setRightShadow(-3,-3, new Color(0xccccff));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; label.setForeground(new Color(0x8888ff));&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; label.setFont(label.getFont().deriveFont(140f));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; JFrame frame = new JFrame(&quot;RichJLabel hack&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; frame.getContentPane().add(label);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; frame.pack();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; frame.setVisible(true);&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; public static void p(String str) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(str);&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
}&lt;/div&gt;
&lt;a href=&quot;http://affiliates.admagnet.net/z/10181/CD10260/&amp;amp;dp=10189&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://affiliates.admagnet.net/42/10260/10181/&amp;amp;dp=10189&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/5370944901492743272/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/swing-3d-effect-on-lable.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/5370944901492743272'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/5370944901492743272'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/swing-3d-effect-on-lable.html' title='swing 3D effect on lable'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-8051098151334123588</id><published>2011-12-15T15:20:00.001+05:30</published><updated>2012-05-17T11:55:32.474+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Socket Communications"/><title type='text'>Socket Communications</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div id=&quot;PageContent&quot;&gt;
&lt;h1&gt;


What Is a Socket?&lt;/h1&gt;
&lt;h1&gt;

&amp;nbsp;&lt;a href=&quot;http://www.getacoder.com/affiliates/ref.php?u=ANTServices&quot; style=&quot;font-weight: normal; text-decoration: underline;&quot;&gt;http://www.getacoder.com/affiliates/ref.php?u=ANTServices&lt;/a&gt; &lt;/h1&gt;
&lt;br /&gt;
Normally, a server runs on a specific computer and has a socket that 
is bound to a specific port number. The server just waits, listening to 
the socket for a client to make a connection request.&lt;br /&gt;
On the client-side: The client knows the hostname of the machine on 
which the server is running and the port number on which the server is 
listening. To make a connection request, the client tries to rendezvous 
with the server on the server&#39;s machine and port. The client also needs 
to identify itself to the server so it binds to a local port number that
 it will use during this connection. This is usually assigned by the 
system.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;img align=&quot;bottom&quot; alt=&quot;A client&#39;s connection request&quot; height=&quot;73&quot; src=&quot;http://docs.oracle.com/javase/tutorial/figures/networking/5connect.gif&quot; width=&quot;301&quot; /&gt;&lt;/center&gt;If
 everything goes well, the server accepts the connection. Upon 
acceptance, the server gets a new socket bound to the same local port 
and also has its remote endpoint set to the address and port of the 
client. It needs a new socket so that it can continue to listen to the 
original socket for connection requests while tending to the needs of 
the connected client.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;img align=&quot;bottom&quot; alt=&quot;The connection is made&quot; height=&quot;88&quot; src=&quot;http://docs.oracle.com/javase/tutorial/figures/networking/6connect.gif&quot; width=&quot;301&quot; /&gt;&lt;/center&gt;On
 the client side, if the connection is accepted, a socket is 
successfully created and the client can use the socket to communicate 
with the server.&lt;br /&gt;
The client and server can now communicate by writing to or reading from their sockets.&lt;br /&gt;
&lt;div class=&quot;note&quot;&gt;
&lt;hr /&gt;
&lt;b&gt;Definition:&lt;/b&gt;&amp;nbsp;A &lt;i&gt;socket&lt;/i&gt; 
is one endpoint of a two-way communication link between two programs 
running on the network. A socket is bound to a port number so that the 
TCP layer can identify the application that data is destined to be sent.&lt;br /&gt;
&lt;hr /&gt;
&lt;/div&gt;
An endpoint is a combination of an IP address and a port number. 
Every TCP connection can be uniquely identified by its two endpoints. 
That way you can have multiple connections between your host and the 
server.&lt;br /&gt;
The &lt;code&gt;java.net&lt;/code&gt; package in the Java platform provides a class, &lt;code&gt;Socket&lt;/code&gt;, that implements one side of a two-way connection between your Java program and another program on the network. The &lt;code&gt;Socket&lt;/code&gt;
 class sits on top of a platform-dependent implementation, hiding the 
details of any particular system from your Java program. By using the &lt;code&gt;java.net.Socket&lt;/code&gt; class instead of relying on native code, your Java programs can communicate over the network in a platform-independent fashion.&lt;br /&gt;
Additionally, &lt;code&gt;java.net&lt;/code&gt; includes the &lt;code&gt;ServerSocket&lt;/code&gt;
 class, which implements a socket that servers can use to listen for and
 accept connections to clients. This lesson shows you how to use the &lt;code&gt;Socket&lt;/code&gt; and &lt;code&gt;ServerSocket&lt;/code&gt; classes.&lt;br /&gt;
If you are trying to connect to the Web, the &lt;code&gt;URL&lt;/code&gt; class and related classes (&lt;code&gt;URLConnection&lt;/code&gt;, &lt;code&gt;URLEncoder&lt;/code&gt;)
 are probably more appropriate than the socket classes. In fact, URLs 
are a relatively high-level connection to the Web and use sockets as 
part of the underlying implementation. See &lt;a href=&quot;http://docs.oracle.com/javase/tutorial/networking/urls/index.html&quot;&gt;Working with URLs&lt;/a&gt; for information about connecting to the Web via URLs.&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;PageTitle&quot;&gt;
&lt;h1&gt;


Reading from and Writing to a Socket&lt;/h1&gt;
&lt;/div&gt;
&lt;div id=&quot;PageContent&quot;&gt;
Let&#39;s look at a simple example that illustrates how a program can establish a connection to a server program using the &lt;code&gt;Socket&lt;/code&gt; class and then, how the client can send data to and receive data from the server through the socket.&lt;br /&gt;
The example program implements a client, &lt;code&gt;EchoClient&lt;/code&gt;, 
that connects to the Echo server. The Echo server simply receives data 
from its client and echoes it back. The Echo server is a well-known 
service that clients can rendezvous with on port 7.&lt;br /&gt;
&lt;code&gt;EchoClient&lt;/code&gt; creates a socket thereby getting a connection
 to the Echo server. It reads input from the user on the standard input 
stream, and then forwards that text to the Echo server by writing the 
text to the socket. The server echoes the input back through the socket 
to the client. The client program reads and displays the data passed 
back to it from the server:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;import java.io.*;
import java.net.*;

public class EchoClient {
    public static void main(String[] args) throws IOException {

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            echoSocket = new Socket(&quot;taranis&quot;, 7);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println(&quot;Don&#39;t know about host: taranis.&quot;);
            System.exit(1);
        } catch (IOException e) {
            System.err.println(&quot;Couldn&#39;t get I/O for &quot;
                               + &quot;the connection to: taranis.&quot;);
            System.exit(1);
        }

 BufferedReader stdIn = new BufferedReader(
                                   new InputStreamReader(System.in));
 String userInput;

 while ((userInput = stdIn.readLine()) != null) {
     out.println(userInput);
     System.out.println(&quot;echo: &quot; + in.readLine());
 }

 out.close();
 in.close();
 stdIn.close();
 echoSocket.close();
    }
}&lt;/pre&gt;
&lt;/div&gt;
Note that &lt;code&gt;EchoClient&lt;/code&gt; both writes to and reads from its socket, thereby sending data to and receiving data from the Echo server.&lt;br /&gt;
Let&#39;s walk through the program and investigate the interesting parts. The three statements in the &lt;code&gt;try&lt;/code&gt; block of the &lt;code&gt;main&lt;/code&gt; method are critical. These lines establish the socket connection between the client and the server and open a &lt;code&gt;PrintWriter&lt;/code&gt; and a &lt;code&gt;BufferedReader&lt;/code&gt; on the socket:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;echoSocket = new Socket(&quot;taranis&quot;, 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
                             echoSocket.getInputStream()));&lt;/pre&gt;
&lt;/div&gt;
The first statement in this sequence creates a new &lt;code&gt;Socket&lt;/code&gt; object and names it &lt;code&gt;echoSocket&lt;/code&gt;. The &lt;code&gt;Socket&lt;/code&gt;
 constructor used here requires the name of the machine and the port 
number to which you want to connect. The example program uses the host 
name &lt;code&gt;taranis&lt;/code&gt;. This is the name of a hypothetical machine on
 our local network. When you type in and run this program on your 
machine, change the host name to the name of a machine on your network. 
Make sure that the name you use is the fully qualified IP name of the 
machine to which you want to connect. The second argument is the port 
number. Port number 7 is the port on which the Echo server listens.&lt;br /&gt;
The second statement gets the socket&#39;s output stream and opens a &lt;code&gt;PrintWriter&lt;/code&gt; on it. Similarly, the third statement gets the socket&#39;s input stream and opens a &lt;code&gt;BufferedReader&lt;/code&gt; on it. The example uses readers and writers so that it can write Unicode characters over the socket.&lt;br /&gt;
To send data through the socket to the server, &lt;code&gt;EchoClient&lt;/code&gt; simply needs to write to the &lt;code&gt;PrintWriter&lt;/code&gt;. To get the server&#39;s response, &lt;code&gt;EchoClient&lt;/code&gt; reads from the &lt;code&gt;BufferedReader&lt;/code&gt;. The rest of the program achieves this. If you are not yet familiar with the Java platform&#39;s I/O classes, you may wish to read 
&lt;a class=&quot;TutorialLink&quot; href=&quot;http://docs.oracle.com/javase/tutorial/essential/io/index.html&quot; target=&quot;_top&quot;&gt;Basic I/O&lt;/a&gt;.&lt;br /&gt;
The next interesting part of the program is the &lt;code&gt;while&lt;/code&gt; 
loop. The loop reads a line at a time from the standard input stream and
 immediately sends it to the server by writing it to the &lt;code&gt;PrintWriter&lt;/code&gt; connected to the socket:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;String userInput;

while ((userInput = stdIn.readLine()) != null) {
    out.println(userInput);
    System.out.println(&quot;echo: &quot; + in.readLine());
}&lt;/pre&gt;
&lt;/div&gt;
The last statement in the &lt;code&gt;while&lt;/code&gt; loop reads a line of information from the &lt;code&gt;BufferedReader&lt;/code&gt; connected to the socket. The &lt;code&gt;readLine&lt;/code&gt; method waits until the server echoes the information back to &lt;code&gt;EchoClient&lt;/code&gt;. When &lt;code&gt;readline&lt;/code&gt; returns, &lt;code&gt;EchoClient&lt;/code&gt; prints the information to the standard output.&lt;br /&gt;
The &lt;code&gt;while&lt;/code&gt; loop continues until the user types an end-of-input character. That is, &lt;code&gt;EchoClient&lt;/code&gt;
 reads input from the user, sends it to the Echo server, gets a response
 from the server, and displays it, until it reaches the end-of-input. 
The &lt;code&gt;while&lt;/code&gt; loop then terminates and the program continues, executing the next four lines of code:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;out.close();
in.close();
stdIn.close();
echoSocket.close();&lt;/pre&gt;
&lt;/div&gt;
These lines of code fall into the category of housekeeping. A 
well-behaved program always cleans up after itself, and this program is 
well-behaved. These statements close the readers and writers connected 
to the socket and to the standard input stream, and close the socket 
connection to the server. The order here is important. You should close 
any streams connected to a socket before you close the socket itself.&lt;br /&gt;
This client program is straightforward and simple because the Echo 
server implements a simple protocol. The client sends text to the 
server, and the server echoes it back. When your client programs are 
talking to a more complicated server such as an HTTP server, your client
 program will also be more complicated. However, the basics are much the
 same as they are in this program:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Open a socket.&lt;/li&gt;
&lt;li&gt;Open an input stream and output stream to the socket.&lt;/li&gt;
&lt;li&gt;Read from and write to the stream according to the server&#39;s protocol.&lt;/li&gt;
&lt;li&gt;Close the streams.&lt;/li&gt;
&lt;li&gt;Close the socket.&lt;/li&gt;
&lt;/ol&gt;
Only step 3 differs from client to client, depending on the server. The other steps remain largely the same.&lt;/div&gt;
&lt;br /&gt;
&lt;div id=&quot;PageTitle&quot;&gt;
&lt;h1&gt;


Writing the Server Side of a Socket&lt;/h1&gt;
&lt;/div&gt;
This section shows you how to write a server and the client that goes
 with it. The server in the client/server pair serves up Knock Knock 
jokes. Knock Knock jokes are favored by children and are usually 
vehicles for bad puns. They go like this:&lt;br /&gt;
&lt;b&gt;Server&lt;/b&gt;: &quot;Knock knock!&quot;&lt;br /&gt;
&lt;b&gt;Client&lt;/b&gt;: &quot;Who&#39;s there?&quot;&lt;br /&gt;
&lt;b&gt;Server&lt;/b&gt;: &quot;Dexter.&quot;&lt;br /&gt;
&lt;b&gt;Client&lt;/b&gt;: &quot;Dexter who?&quot;&lt;br /&gt;
&lt;b&gt;Server&lt;/b&gt;: &quot;Dexter halls with boughs of holly.&quot;&lt;br /&gt;
&lt;b&gt;Client&lt;/b&gt;: &quot;Groan.&quot;&lt;br /&gt;
The example consists of two independently running Java programs: the 
client program and the server program. The client program is implemented
 by a single class, &lt;code&gt;KnockKnockClient&lt;/code&gt;, and is very similar to the 
&lt;a class=&quot;SourceLink&quot; href=&quot;http://docs.oracle.com/javase/tutorial/networking/sockets/examples/EchoClient.java&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;EchoClient&lt;/code&gt;&lt;/a&gt; example from the previous section. The server program is implemented by two classes: &lt;code&gt;KnockKnockServer&lt;/code&gt; and &lt;code&gt;KnockKnockProtocol&lt;/code&gt;, &lt;code&gt;KnockKnockServer&lt;/code&gt; contains the &lt;code&gt;main&lt;/code&gt;
 method for the server program and performs the work of listening to the
 port, establishing connections, and reading from and writing to the 
socket. &lt;code&gt;KnockKnockProtocol&lt;/code&gt; serves up the jokes. It keeps 
track of the current joke, the current state (sent knock knock, sent 
clue, and so on), and returns the various text pieces of the joke 
depending on the current state. This object implements the protocol-the 
language that the client and server have agreed to use to communicate.&lt;br /&gt;
The following section looks in detail at each class in both the client and the server and then shows you how to run them.&lt;br /&gt;
&lt;h2&gt;


The Knock Knock Server&lt;/h2&gt;
This section walks through the code that implements the Knock Knock server program. Here is the complete source for the 
&lt;a class=&quot;SourceLink&quot; href=&quot;http://docs.oracle.com/javase/tutorial/networking/sockets/examples/KnockKnockServer.java&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;KnockKnockServer&lt;/code&gt;&lt;/a&gt; class.&lt;br /&gt;
The server program begins by creating a new &lt;code&gt;ServerSocket&lt;/code&gt;
 object to listen on a specific port (see the statement in bold in the 
following code segment). When writing a server, choose a port that is 
not already dedicated to some other service. &lt;code&gt;KnockKnockServer&lt;/code&gt;
 listens on port 4444 because 4 happens to be my favorite number and 
port 4444 is not being used for anything else in my environment:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;try {
    &lt;b&gt;serverSocket = new ServerSocket(4444);&lt;/b&gt;
} catch (IOException e) {
    System.out.println(&quot;Could not listen on port: 4444&quot;);
    System.exit(-1);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;code&gt;ServerSocket&lt;/code&gt; is a &lt;code&gt;java.net&lt;/code&gt; class that 
provides a system-independent implementation of the server side of a 
client/server socket connection. The constructor for &lt;code&gt;ServerSocket&lt;/code&gt; throws an exception if it can&#39;t listen on the specified port (for example, the port is already being used). In this case, the &lt;code&gt;KnockKnockServer&lt;/code&gt; has no choice but to exit.&lt;br /&gt;
If the server successfully binds to its port, then the &lt;code&gt;ServerSocket&lt;/code&gt; object is successfully created and the server continues to the next step--accepting a connection from a client (shown in bold):&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;Socket clientSocket = null;
try {
    &lt;b&gt;clientSocket = serverSocket.accept();&lt;/b&gt;
} catch (IOException e) {
    System.out.println(&quot;Accept failed: 4444&quot;);
    System.exit(-1);
}&lt;/pre&gt;
&lt;/div&gt;
The &lt;code&gt;accept&lt;/code&gt; method waits until a client starts up and 
requests a connection on the host and port of this server (in this 
example, the server is running on the hypothetical machine taranis on 
port 4444). When a connection is requested and successfully established,
 the accept method returns a new &lt;code&gt;Socket&lt;/code&gt; object which is 
bound to the same local port and has its remote address and remote port 
set to that of the client. The server can communicate with the client 
over this new &lt;code&gt;Socket&lt;/code&gt; and continue to listen for client connection requests on the original &lt;code&gt;ServerSocket&lt;/code&gt;
 This particular version of the program doesn&#39;t listen for more client 
connection requests. However, a modified version of the program is 
provided in &lt;a href=&quot;http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html#later&quot;&gt;Supporting Multiple Clients&lt;/a&gt;.&lt;br /&gt;
After the server successfully establishes a connection with a client, it communicates with the client using this code:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;PrintWriter out = new PrintWriter(
                      clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                            clientSocket.getInputStream()));
String inputLine, outputLine;

&lt;b&gt;// initiate conversation with client
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);&lt;/b&gt;

while ((inputLine = in.readLine()) != null) {   
    outputLine = kkp.processInput(inputLine);
    out.println(outputLine);
    if (outputLine.equals(&quot;Bye.&quot;))
        break;
}&lt;/pre&gt;
&lt;/div&gt;
This code:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Gets the socket&#39;s input and output stream and opens readers and writers on them.&lt;/li&gt;
&lt;li&gt;Initiates communication with the client by writing to the socket (shown in bold).&lt;/li&gt;
&lt;li&gt;Communicates with the client by reading from and writing to the socket (the &lt;code&gt;while&lt;/code&gt; loop).&lt;/li&gt;
&lt;/ol&gt;
Step 1 is already familiar. Step 2 is shown in bold and is worth a 
few comments. The bold statements in the code segment above initiate the
 conversation with the client. The code creates a &lt;code&gt;KnockKnockProtocol&lt;/code&gt; object-the object that keeps track of the current joke, the current state within the joke, and so on.&lt;br /&gt;
After the &lt;code&gt;KnockKnockProtocol&lt;/code&gt; is created, the code calls &lt;code&gt;KnockKnockProtocol&lt;/code&gt;&#39;s &lt;code&gt;processInput&lt;/code&gt;
 method to get the first message that the server sends to the client. 
For this example, the first thing that the server says is &quot;Knock! 
Knock!&quot; Next, the server writes the information to the &lt;code&gt;PrintWriter&lt;/code&gt; connected to the client socket, thereby sending the message to the client.&lt;br /&gt;
Step 3 is encoded in the &lt;code&gt;while&lt;/code&gt; loop. As long as the 
client and server still have something to say to each other, the server 
reads from and writes to the socket, sending messages back and forth 
between the client and the server.&lt;br /&gt;
The server initiated the conversation with a &quot;Knock! Knock!&quot; so 
afterwards the server must wait for the client to say &quot;Who&#39;s there?&quot; As a
 result, the while loop iterates on a read from the input stream. The 
readLine method waits until the client responds by writing something to 
its output stream (the server&#39;s input stream). When the client responds,
 the server passes the client&#39;s response to the &lt;code&gt;KnockKnockProtocol&lt;/code&gt; object and asks the &lt;code&gt;KnockKnockProtocol&lt;/code&gt;
 object for a suitable reply. The server immediately sends the reply to 
the client via the output stream connected to the socket, using a call 
to println. If the server&#39;s response generated from the &lt;code&gt;KnockKnockServer&lt;/code&gt; object is &quot;Bye.&quot; this indicates that the client doesn&#39;t want any more jokes and the loop quits.&lt;br /&gt;
The &lt;code&gt;KnockKnockServer&lt;/code&gt; class is a well-behaved server, so the last several lines of this section of &lt;code&gt;KnockKnockServer&lt;/code&gt; clean up by closing all of the input and output streams, the client socket, and the server socket:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;out.close();
in.close();
clientSocket.close();
serverSocket.close();&lt;/pre&gt;
&lt;/div&gt;
&lt;h2&gt;


The Knock Knock Protocol&lt;/h2&gt;
The 
&lt;a class=&quot;SourceLink&quot; href=&quot;http://docs.oracle.com/javase/tutorial/networking/sockets/examples/KnockKnockProtocol.java&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;KnockKnockProtocol&lt;/code&gt;&lt;/a&gt;
 class implements the protocol that the client and server use to 
communicate. This class keeps track of where the client and the server 
are in their conversation and serves up the server&#39;s response to the 
client&#39;s statements. The &lt;code&gt;KnockKnockServer&lt;/code&gt; object contains 
the text of all the jokes and makes sure that the client gives the 
proper response to the server&#39;s statements. It wouldn&#39;t do to have the 
client say &quot;Dexter who?&quot; when the server says &quot;Knock! Knock!&quot;&lt;br /&gt;
All client/server pairs must have some protocol by which they speak 
to each other; otherwise, the data that passes back and forth would be 
meaningless. The protocol that your own clients and servers use depends 
entirely on the communication required by them to accomplish the task.&lt;br /&gt;
&lt;h2&gt;


The Knock Knock Client&lt;/h2&gt;
The 
&lt;a class=&quot;SourceLink&quot; href=&quot;http://docs.oracle.com/javase/tutorial/networking/sockets/examples/KnockKnockClient.java&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;KnockKnockClient&lt;/code&gt;&lt;/a&gt; class implements the client program that speaks to the &lt;code&gt;KnockKnockServer&lt;/code&gt;. &lt;code&gt;KnockKnockClient&lt;/code&gt; is based on the &lt;code&gt;EchoClient&lt;/code&gt; program in the previous section, &lt;a href=&quot;http://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html&quot;&gt;Reading from and Writing to a Socket&lt;/a&gt;
 and should be somewhat familiar to you. But we&#39;ll go over the program 
anyway and look at what&#39;s happening in the client in the context of 
what&#39;s going on in the server.&lt;br /&gt;
When you start the client program, the server should already be 
running and listening to the port, waiting for a client to request a 
connection. So, the first thing the client program does is to open a 
socket that is connected to the server running on the hostname and port 
specified:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;&lt;b&gt;kkSocket = new Socket(&quot;taranis&quot;, 4444);&lt;/b&gt;
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
                            kkSocket.getInputStream()));&lt;/pre&gt;
&lt;/div&gt;
When creating its socket, &lt;code&gt;KnockKnockClient&lt;/code&gt; uses the host name &lt;code&gt;taranis&lt;/code&gt;,
 the name of a hypothetical machine on our network. When you type in and
 run this program, change the host name to the name of a machine on your
 network. This is the machine on which you will run the &lt;code&gt;KnockKnockServer&lt;/code&gt;.&lt;br /&gt;
The &lt;code&gt;KnockKnockClient&lt;/code&gt; program also specifies the port number 4444 when creating its socket. This is a &lt;i&gt;remote port number&lt;/i&gt;--the number of a port on the server machine--and is the port to which &lt;code&gt;KnockKnockServer&lt;/code&gt; is listening. The client&#39;s socket is bound to any available &lt;i&gt;local port&lt;/i&gt;--a
 port on the client machine. Remember that the server gets a new socket 
as well. That socket is bound to local port number 4444 on its machine. 
The server&#39;s socket and the client&#39;s socket are connected.&lt;br /&gt;
Next comes the while loop that implements the communication between 
the client and the server. The server speaks first, so the client must 
listen first. The client does this by reading from the input stream 
attached to the socket. If the server does speak, it says &quot;Bye.&quot; and the
 client exits the loop. Otherwise, the client displays the text to the 
standard output and then reads the response from the user, who types 
into the standard input. After the user types a carriage return, the 
client sends the text to the server through the output stream attached 
to the socket.&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;while ((fromServer = in.readLine()) != null) {
    System.out.println(&quot;Server: &quot; + fromServer);
    if (fromServer.equals(&quot;Bye.&quot;))
        break;
    fromUser = stdIn.readLine();
    if (fromUser != null) {
        System.out.println(&quot;Client: &quot; + fromUser);
        out.println(fromUser);
    }
}&lt;/pre&gt;
&lt;/div&gt;
The communication ends when the server asks if the client wishes to 
hear another joke, the client says no, and the server says &quot;Bye.&quot;&lt;br /&gt;
In the interest of good housekeeping, the client closes its input and output streams and the socket:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;out.close();
in.close();
stdIn.close();
kkSocket.close();&lt;/pre&gt;
&lt;/div&gt;
&lt;h2&gt;


Running the Programs&lt;/h2&gt;
You must start the server program first. To do this, run the server 
program using the Java interpreter, just as you would any other Java 
application. Remember to run the server on the machine that the client 
program specifies when it creates the socket.&lt;br /&gt;
Next, run the client program. Note that you can run the client on any
 machine on your network; it does not have to run on the same machine as
 the server.&lt;br /&gt;
If you are too quick, you might start the client before the server 
has a chance to initialize itself and begin listening on the port. If 
this happens, you will see a stack trace from the client. If this 
happens, just restart the client.&lt;br /&gt;
If you forget to change the host name in the source code for the &lt;code&gt;KnockKnockClient&lt;/code&gt; program, you will see the following error message:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;Don&#39;t know about host: taranis&lt;/pre&gt;
&lt;/div&gt;
To fix this, modify the &lt;code&gt;KnockKnockClient&lt;/code&gt; program and provide a valid host name for your network. Recompile the client program and try again.&lt;br /&gt;
If you try to start a second client while the first client is 
connected to the server, the second client just hangs. The next section,
 &lt;a href=&quot;http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html#later&quot;&gt;Supporting Multiple Clients&lt;/a&gt;, talks about supporting multiple clients.&lt;br /&gt;
When you successfully get a connection between the client and server, you will see the following text displayed on your screen:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;Server: Knock! Knock!&lt;/pre&gt;
&lt;/div&gt;
Now, you must respond with:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;&lt;b&gt;Who&#39;s there?&lt;/b&gt;&lt;/pre&gt;
&lt;/div&gt;
The client echoes what you type and sends the text to the server. The
 server responds with the first line of one of the many Knock Knock 
jokes in its repertoire. Now your screen should contain this (the text 
you typed is in bold):&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;Server: Knock! Knock!
&lt;b&gt;Who&#39;s there?&lt;/b&gt;
Client: Who&#39;s there?
Server: Turnip&lt;/pre&gt;
&lt;/div&gt;
Now, you respond with:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;Turnip who?&quot;&lt;/pre&gt;
&lt;/div&gt;
Again, the client echoes what you type and sends the text to the 
server. The server responds with the punch line. Now your screen should 
contain this:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;Server: Knock! Knock!
&lt;b&gt;Who&#39;s there?&lt;/b&gt;
Client: Who&#39;s there?
Server: Turnip
&lt;b&gt;Turnip who?&lt;/b&gt;
Client: Turnip who?
Server: Turnip the heat, it&#39;s cold in here! Want another? (y/n)&lt;/pre&gt;
&lt;/div&gt;
If you want to hear another joke, type &lt;b&gt;y&lt;/b&gt;; if not, type &lt;b&gt;n&lt;/b&gt;. If you type &lt;b&gt;y&lt;/b&gt;, the server begins again with &quot;Knock! Knock!&quot; If you type &lt;b&gt;n&lt;/b&gt;, the server says &quot;Bye.&quot; thus causing both the client and the server to exit.&lt;br /&gt;
If at any point you make a typing mistake, the &lt;code&gt;KnockKnockServer&lt;/code&gt; object catches it and the server responds with a message similar to this:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;Server: You&#39;re supposed to say &quot;Who&#39;s there?&quot;!&lt;/pre&gt;
&lt;/div&gt;
The server then starts the joke over again:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;Server: Try again. Knock! Knock!&lt;/pre&gt;
&lt;/div&gt;
Note that the &lt;code&gt;KnockKnockProtocol&lt;/code&gt; object is particular about spelling and punctuation but not about capitalization.&lt;br /&gt;
&lt;h2&gt;


&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=9044200624497812337&quot; id=&quot;later&quot; name=&quot;later&quot;&gt;Supporting Multiple Clients&lt;/a&gt;&lt;/h2&gt;
To keep the &lt;code&gt;KnockKnockServer&lt;/code&gt; example simple, we designed
 it to listen for and handle a single connection request. However, 
multiple client requests can come into the same port and, consequently, 
into the same &lt;code&gt;ServerSocket&lt;/code&gt;. Client connection requests are 
queued at the port, so the server must accept the connections 
sequentially. However, the server can service them simultaneously 
through the use of threads - one thread per each client connection.&lt;br /&gt;
The basic flow of logic in such a server is this:&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;
&lt;pre&gt;while (true) {
    accept a connection ;
    create a thread to deal with the client ;
end while&lt;/pre&gt;
&lt;/div&gt;
The thread reads from and writes to the client connection as necessary.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;


What are Sockets and Threads?&lt;/h3&gt;
A
 socket is a software endpoint that establishes bidirectional 
communication between a server program and one or more client programs. 
The socket associates the server program with a specific hardware port 
on the machine where it runs so any client program anywhere in the 
network with a socket associated with that same port can communicate 
with the server program.&lt;br /&gt;
&lt;img align=&quot;left&quot; alt=&quot;&quot; height=&quot;151&quot; src=&quot;http://www.oracle.com/ocom/groups/public/@otn/documents/digitalasset/148791.gif&quot; width=&quot;221&quot; /&gt;
 A server program typically provides resources to a network of client 
programs. Client programs send requests to the server program, and the 
server program responds to the request.
          &lt;br /&gt;
One way to handle 
requests from more than one client is to make the server program 
multi-threaded. A multi-threaded server creates a thread for each 
communication it accepts from a client. A thread is a sequence of 
instructions that run independently of the program and of any other 
threads.&lt;br /&gt;
Using threads, a 
multi-threaded server program can accept a connection from a client, 
start a thread for that communication, and continue listening for 
requests from other clients.  
            &lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=9044200624497812337&quot; id=&quot;about&quot; name=&quot;about&quot;&gt;&lt;/a&gt;
          &lt;br /&gt;
&lt;h3&gt;


About the Examples&lt;/h3&gt;
The examples for this lesson consist of two versions of the client and server program pair adapted from the  
            &lt;a href=&quot;http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/Code/FileIO.java&quot;&gt;FileIO.java&lt;/a&gt; application presented in  
            &lt;a href=&quot;http://www.oracle.com/technetwork/java/data-142896.html&quot;&gt;Part 1, Lesson 6: File Access and Permissions&lt;/a&gt;.
          &lt;br /&gt;
Example 1 sets up a
 client and server communication between one server program and one 
client program. The server program is not multi-threaded and cannot 
handle requests from more than one client.&lt;br /&gt;
Example 2 converts the server program to a multi-threaded version so it can handle requests from more than one client.&lt;br /&gt;
&lt;h4&gt;


Example 1: Client-Side Behavior&lt;/h4&gt;
The  
            &lt;a href=&quot;http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/Code/SocketClient.java&quot;&gt;client program&lt;/a&gt; presents a simple user interface and prompts for text input. When you click the  
            &lt;code&gt;Click Me&lt;/code&gt; button, the text is sent to the server
 program. The client program expects an echo from the server and prints 
the echo it receives on its standard output.
          &lt;br /&gt;
&lt;img alt=&quot;&quot; height=&quot;128&quot; src=&quot;http://www.oracle.com/ocom/groups/public/@otn/documents/digitalasset/146416.gif&quot; width=&quot;222&quot; /&gt;          &lt;br /&gt;
&lt;h4&gt;


Example 1: Server-Side Behavior&lt;/h4&gt;
The  
            &lt;a href=&quot;http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/Code/SocketServer.java&quot;&gt;server program&lt;/a&gt; presents a simple user interface, and when you click the  
            &lt;code&gt;Click Me&lt;/code&gt; button, the text received from the client is displayed. The server echoes the text it receives whether or not you click the  
            &lt;code&gt;Click Me&lt;/code&gt; button.
          &lt;br /&gt;
&lt;img alt=&quot;&quot; height=&quot;125&quot; src=&quot;http://www.oracle.com/ocom/groups/public/@otn/documents/digitalasset/146865.gif&quot; width=&quot;224&quot; /&gt;          &lt;br /&gt;
&lt;h4&gt;


Example 1: Compile and Run&lt;/h4&gt;
To
 run the example programs, start the server program first. If you do 
not, the client program cannot establish the socket connection. Here are
 the compiler and interpreter commands to compile and run the example.  
            
          &lt;br /&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;10&quot; cellspacing=&quot;0&quot; class=&quot;grey4&quot;&gt;
             &lt;tbody&gt;
&lt;tr&gt;
               &lt;td&gt;&lt;pre&gt;  javac SocketServer.java
  javac SocketClient.java

  java SocketServer
  java SocketClient&lt;/pre&gt;
&lt;/td&gt;
            &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span class=&quot;sp20&quot;&gt;&lt;br /&gt;&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=9044200624497812337&quot; id=&quot;server&quot; name=&quot;server&quot;&gt;&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;

&lt;span class=&quot;sp20&quot;&gt;
Example 1: Server-Side Program&lt;/span&gt;&lt;/h3&gt;
&lt;span class=&quot;sp20&quot;&gt;
The  
            &lt;a href=&quot;http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/Code/SocketServer.java&quot;&gt;server program&lt;/a&gt; establishes a socket connection on Port 4321 in its  
            &lt;code&gt;listenSocket&lt;/code&gt; method. It reads data sent to it and sends that same data back to the server in its  
            &lt;code&gt;actionPerformed&lt;/code&gt; method.
          &lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;h4&gt;
&lt;span class=&quot;sp20&quot;&gt;

listenSocket Method&lt;/span&gt;&lt;/h4&gt;
&lt;span class=&quot;sp20&quot;&gt;
The  
            &lt;code&gt;listenSocket&lt;/code&gt; method creates a  
            &lt;code&gt;ServerSocket&lt;/code&gt; object with the port number on 
which the server program is going to listen for client communications. 
The port number must be an available port, which means the number cannot
 be reserved or already in use. For example, Unix systems reserve ports 1
 through 1023 for administrative functions leaving port numbers greater 
than 1024 available for use.  
            
          &lt;br /&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;10&quot; cellspacing=&quot;0&quot; class=&quot;grey4&quot;&gt;
             &lt;tbody&gt;
&lt;tr&gt;
               &lt;td&gt;&lt;pre&gt;public void listenSocket(){
  try{
    server = new ServerSocket(4321); 
  } catch (IOException e) {
    System.out.println(&quot;Could not listen on port 4321&quot;);
    System.exit(-1);
  }&lt;/pre&gt;
&lt;/td&gt;
            &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span class=&quot;sp20&quot;&gt;&lt;br /&gt;&lt;code&gt;listenSocket&lt;/code&gt;&lt;code&gt;Socket&lt;/code&gt;&lt;code&gt;server.accept&lt;/code&gt;&lt;code&gt;Socket&lt;/code&gt;&lt;table border=&quot;0&quot; cellpadding=&quot;10&quot; cellspacing=&quot;0&quot; class=&quot;grey4&quot;&gt;
             &lt;tbody&gt;
&lt;tr&gt;
               &lt;td&gt;&lt;pre&gt;  try{
    client = server.accept();
  } catch (IOException e) {
    System.out.println(&quot;Accept failed: 4321&quot;);
    System.exit(-1);
  }&lt;/pre&gt;
&lt;/td&gt;
            &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span class=&quot;sp20&quot;&gt;&lt;br /&gt;&lt;code&gt;listenSocket&lt;/code&gt;&lt;code&gt;BufferedReader&lt;/code&gt;&lt;code&gt;client&lt;/code&gt;&lt;code&gt;PrintWriter&lt;/code&gt;&lt;table border=&quot;0&quot; cellpadding=&quot;10&quot; cellspacing=&quot;0&quot; class=&quot;grey4&quot;&gt;
             &lt;tbody&gt;
&lt;tr&gt;
               &lt;td&gt;&lt;pre&gt;  try{
   in = new BufferedReader(new InputStreamReader(
                           client.getInputStream()));
   out = new PrintWriter(client.getOutputStream(), 
                         true);
  } catch (IOException e) {
    System.out.println(&quot;Read failed&quot;);
    System.exit(-1);
  }
}&lt;/pre&gt;
&lt;/td&gt;
            &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span class=&quot;sp20&quot;&gt;&lt;br /&gt;&lt;code&gt;listenSocket&lt;/code&gt;&lt;table border=&quot;0&quot; cellpadding=&quot;10&quot; cellspacing=&quot;0&quot; class=&quot;grey4&quot;&gt;
             &lt;tbody&gt;
&lt;tr&gt;
               &lt;td&gt;&lt;pre&gt;    while(true){
      try{
        line = in.readLine();
//Send data back to client
        out.println(line);
      } catch (IOException e) {
        System.out.println(&quot;Read failed&quot;);
        System.exit(-1);
      }
    }&lt;/pre&gt;
&lt;/td&gt;
            &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span class=&quot;sp20&quot;&gt;&lt;br /&gt;&lt;h4&gt;


actionPerformed Method&lt;/h4&gt;
The  
            &lt;code&gt;actionPerformed&lt;/code&gt; method is called by the Java platform for action events such as button clicks. This  
            &lt;code&gt;actionPerformed&lt;/code&gt; method uses the text stored in the  
            &lt;code&gt;line&lt;/code&gt; object to initialize the  
            &lt;code&gt;textArea&lt;/code&gt; object so the retrieved text can be displayed to the end user.  
            
          &lt;br /&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;10&quot; cellspacing=&quot;0&quot; class=&quot;grey4&quot;&gt;
             &lt;tbody&gt;
&lt;tr&gt;
               &lt;td&gt;&lt;pre&gt;public void actionPerformed(ActionEvent event) {
   Object source = event.getSource();

   if(source == button){
       textArea.setText(line);
   }
}&lt;/pre&gt;
&lt;/td&gt;
            &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span class=&quot;sp20&quot;&gt;&lt;br /&gt;&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=9044200624497812337&quot; id=&quot;client&quot; name=&quot;client&quot;&gt;&lt;/a&gt;&lt;h3&gt;


Example 1: Client-Side Program&lt;/h3&gt;
The  
            &lt;a href=&quot;http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/Code/SocketClient.java&quot;&gt;client program&lt;/a&gt; establishes a connection to the server program on a particular host and port number in its  
            &lt;code&gt;listenSocket&lt;/code&gt; method, and sends the data entered by the end user to the server program in its  
            &lt;code&gt;actionPerformed&lt;/code&gt; method. The  
            &lt;code&gt;actionPerformed&lt;/code&gt; method also receives the data back from the server and prints it to the command line.
          &lt;br /&gt;
&lt;h4&gt;


listenSocket Method&lt;/h4&gt;
The  
            &lt;code&gt;listenSocket&lt;/code&gt; method first creates a  
            &lt;code&gt;Socket&lt;/code&gt; object with the computer name (
            &lt;code&gt;kq6py&lt;/code&gt;) and port number (4321) where the server program is listening for client connection requests. Next, it creates a  
            &lt;code&gt;PrintWriter&lt;/code&gt; object to send data over the socket connection to the server program. It also creates a  
            &lt;code&gt;BufferedReader&lt;/code&gt; object to read the text sent by the server back to the client.  
            
          &lt;br /&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;10&quot; cellspacing=&quot;0&quot; class=&quot;grey4&quot;&gt;
             &lt;tbody&gt;
&lt;tr&gt;
               &lt;td&gt;&lt;pre&gt;public void listenSocket(){
//Create socket connection
   try{
     socket = new Socket(&quot;kq6py&quot;, 4321);
     out = new PrintWriter(socket.getOutputStream(), 
                 true);
     in = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
   } catch (UnknownHostException e) {
     System.out.println(&quot;Unknown host: kq6py&quot;);
     System.exit(1);
   } catch  (IOException e) {
     System.out.println(&quot;No I/O&quot;);
     System.exit(1);
   }
}&lt;/pre&gt;
&lt;/td&gt;
            &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span class=&quot;sp20&quot;&gt;&lt;br /&gt;&lt;h4&gt;


actionPerformed Method&lt;/h4&gt;
The  
            &lt;code&gt;actionPerformed&lt;/code&gt; method is called by the Java platform for action events such as button clicks. This  
            &lt;code&gt;actionPerformed&lt;/code&gt; method code gets the text in the  
            &lt;code&gt;Textfield&lt;/code&gt; object and passes it to the  
            &lt;code&gt;PrintWriter&lt;/code&gt; object, which then sends it over the socket connection to the server program.
          &lt;br /&gt;
The  
            &lt;code&gt;actionPerformed&lt;/code&gt; method then makes the  
            &lt;code&gt;Textfield&lt;/code&gt; object blank so it is ready for more 
end user input. Lastly, it receives the text sent back to it by the 
server and prints the text out.  
            
          &lt;br /&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;10&quot; cellspacing=&quot;0&quot; class=&quot;grey4&quot;&gt;
             &lt;tbody&gt;
&lt;tr&gt;
               &lt;td&gt;&lt;pre&gt;public void actionPerformed(ActionEvent event){
   Object source = event.getSource();

   if(source == button){
//Send data over socket
      String text = textField.getText();
      out.println(text);
      textField.setText(new String(&quot;&quot;));
      out.println(text);
   }
//Receive text from server
   try{
     String line = in.readLine();
     System.out.println(&quot;Text received: &quot; + line);
   } catch (IOException e){
     System.out.println(&quot;Read failed&quot;);
     System.exit(1);
   }
}  &lt;/pre&gt;
&lt;/td&gt;
            &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span class=&quot;sp20&quot;&gt;&lt;br /&gt;&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=9044200624497812337&quot; id=&quot;multi&quot; name=&quot;multi&quot;&gt;&lt;/a&gt;&lt;h3&gt;


Example 2: Multithreaded Server Example&lt;/h3&gt;
The
 example in its current state works between the server program and one 
client program only. To allow multiple client connections, the server 
program has to be converted to a  
            &lt;a href=&quot;http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/Code/SocketThrdServer.java&quot;&gt;multithreaded server&lt;/a&gt; program.
          &lt;br /&gt;
&lt;table&gt;
             &lt;tbody&gt;
&lt;tr&gt;
               &lt;td valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;img alt=&quot;&quot; height=&quot;101&quot; src=&quot;http://www.oracle.com/ocom/groups/public/@otn/documents/digitalasset/145391.gif&quot; width=&quot;212&quot; /&gt;                
                 &lt;br /&gt;
&lt;div align=&quot;center&quot;&gt;
First Client
                &lt;/div&gt;
&lt;img alt=&quot;&quot; height=&quot;101&quot; src=&quot;http://www.oracle.com/ocom/groups/public/@otn/documents/digitalasset/148446.gif&quot; width=&quot;212&quot; /&gt;                
                 &lt;br /&gt;
&lt;div align=&quot;center&quot;&gt;
Second Client
                &lt;/div&gt;
&lt;img alt=&quot;&quot; height=&quot;101&quot; src=&quot;http://www.oracle.com/ocom/groups/public/@otn/documents/digitalasset/145418.gif&quot; width=&quot;212&quot; /&gt;                
                 &lt;br /&gt;
&lt;div align=&quot;center&quot;&gt;
Third Client
                &lt;/div&gt;
&lt;/td&gt;
               &lt;td valign=&quot;top&quot;&gt;&lt;img alt=&quot;&quot; height=&quot;81&quot; src=&quot;http://www.oracle.com/ocom/groups/public/@otn/documents/digitalasset/146376.gif&quot; width=&quot;223&quot; /&gt;              &lt;/td&gt;
            &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
In this example the  
            &lt;code&gt;listenSocket&lt;/code&gt; method loops on the  
            &lt;code&gt;server.accept&lt;/code&gt; call waiting for client connections and creates an instance of the  
            &lt;code&gt;ClientWorker&lt;/code&gt; class for each client connection it accepts. The  
            &lt;code&gt;textArea&lt;/code&gt; component that displays the text received from the client connection is passed to the  
            &lt;code&gt;ClientWorker&lt;/code&gt; instance with the accepted client connection.  
            
          &lt;br /&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;10&quot; cellspacing=&quot;0&quot; class=&quot;grey4&quot;&gt;
             &lt;tbody&gt;
&lt;tr&gt;
               &lt;td&gt;&lt;pre&gt;public void listenSocket(){
  try{
    server = new ServerSocket(4444);
  } catch (IOException e) {
    System.out.println(&quot;Could not listen on port 4444&quot;);
    System.exit(-1);
  }
  while(true){
    ClientWorker w;
    try{
//server.accept returns a client connection
      w = new ClientWorker(server.accept(), textArea);
      Thread t = new Thread(w);
      t.start();
    } catch (IOException e) {
      System.out.println(&quot;Accept failed: 4444&quot;);
      System.exit(-1);
    }
  }
}&lt;/pre&gt;
&lt;/td&gt;
            &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span class=&quot;sp20&quot;&gt;&lt;br /&gt;The important changes in this version of the server program over the non-threaded server program are the  
            &lt;code&gt;line&lt;/code&gt; and  
            &lt;code&gt;client&lt;/code&gt; variables are no longer instance variables of the server class, but are handled inside the  
            &lt;code&gt;ClientWorker&lt;/code&gt; class.
          &lt;br /&gt;
The  
            &lt;code&gt;ClientWorker&lt;/code&gt; class implements the  
            &lt;code&gt;Runnable&lt;/code&gt; interface, which has one method,  
            &lt;code&gt;run&lt;/code&gt;. The  
            &lt;code&gt;run&lt;/code&gt; method executes independently in each thread. If three clients request connections, three  
            &lt;code&gt;ClientWorker&lt;/code&gt; instances are created, a thread is started for each  
            &lt;code&gt;ClientWorker&lt;/code&gt; instance, and the  
            &lt;code&gt;run&lt;/code&gt; method executes for each thread.
          &lt;br /&gt;
In this example, the  
            &lt;code&gt;run&lt;/code&gt; method creates the input buffer and output 
writer, loops on the input stream waiting for input from the client, 
sends the data it receives back to the client, and sets the text in the 
text area.  
            
          &lt;br /&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;10&quot; cellspacing=&quot;0&quot; class=&quot;grey4&quot;&gt;
             &lt;tbody&gt;
&lt;tr&gt;
               &lt;td&gt;&lt;pre&gt;class ClientWorker implements Runnable {
  private Socket client;
  private JTextArea textArea;

//Constructor
  ClientWorker(Socket client, JTextArea textArea) {
    this.client = client;
    this.textArea = textArea;
  }

  public void run(){
    String line;
    BufferedReader in = null;
    PrintWriter out = null;
    try{
      in = new BufferedReader(new 
        InputStreamReader(client.getInputStream()));
      out = new 
        PrintWriter(client.getOutputStream(), true);
    } catch (IOException e) {
      System.out.println(&quot;in or out failed&quot;);
      System.exit(-1);
    }

    while(true){
      try{
        line = in.readLine();
//Send data back to client
        out.println(line);
//Append data to text area
        textArea.append(line);
       }catch (IOException e) {
        System.out.println(&quot;Read failed&quot;);
        System.exit(-1);
       }
    }
  }
}&lt;/pre&gt;
&lt;/td&gt;
            &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span class=&quot;sp20&quot;&gt;&lt;br /&gt;&lt;code&gt;JTextArea.append&lt;/code&gt;&lt;code&gt;JTextArea.append&lt;/code&gt;&lt;code&gt;textArea.append(line)&lt;/code&gt;&lt;code&gt;synchronized&lt;/code&gt;&lt;code&gt;run&lt;/code&gt;&lt;code&gt;textArea.append(line)&lt;/code&gt;&lt;code&gt;appendText(line)&lt;/code&gt;&lt;pre&gt;  public synchronized void appendText(line){
    textArea.append(line);
  }&lt;/pre&gt;
&lt;code&gt;synchronized&lt;/code&gt;&lt;code&gt;textArea&lt;/code&gt;&lt;code&gt;textArea&lt;/code&gt;The  
            &lt;code&gt;finalize()&lt;/code&gt; method is called by the Java virtual
 machine (JVM)* before the program exits to give the program a chance to
 clean up and release resources. Multi-threaded programs should close 
all  
            &lt;code&gt;Files&lt;/code&gt; and  
            &lt;code&gt;Sockets&lt;/code&gt; they use before exiting so they do not face resource starvation. The call to  
            &lt;code&gt;server.close()&lt;/code&gt; in the  
            &lt;code&gt;finalize()&lt;/code&gt; method closes the  
            &lt;code&gt;Socket&lt;/code&gt; connection used by each thread in this program.  
            
          &lt;br /&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;10&quot; cellspacing=&quot;0&quot; class=&quot;grey4&quot;&gt;
             &lt;tbody&gt;
&lt;tr&gt;
               &lt;td&gt;&lt;pre&gt;  protected void finalize(){
//Objects created in run method are finalized when
//program terminates and thread exits
     try{
        server.close();
    } catch (IOException e) {
        System.out.println(&quot;Could not close socket&quot;);
        System.exit(-1);
    }
  }&lt;/pre&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;a href=&quot;http://affiliates.admagnet.net/z/10181/CD10260/&amp;amp;dp=10189&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://affiliates.admagnet.net/42/10260/10181/&amp;amp;dp=10189&quot; /&gt;&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/8051098151334123588/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/socket-communications.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/8051098151334123588'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/8051098151334123588'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/socket-communications.html' title='Socket Communications'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-8241770756897953790</id><published>2011-12-14T18:06:00.001+05:30</published><updated>2012-04-16T21:15:15.560+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 211-224"/><title type='text'>SCJP Questions 211-224</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 211&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. class Snoochy {&lt;br /&gt;
12. Boochybooch;&lt;br /&gt;
13. public Snoochy() { booch = new Boochy(this); }&lt;br /&gt;
14. }&lt;br /&gt;
15.&lt;br /&gt;
16. class Boochy {&lt;br /&gt;
17. Snoochy snooch;&lt;br /&gt;
18. public Boochy(Snoochy s) { snooch = s; }&lt;br /&gt;
19. }&lt;br /&gt;
And the statements:&lt;br /&gt;
21. public static void main(String[] args) {&lt;br /&gt;
22. Snoochy snoog = new Snoochy();&lt;br /&gt;
23. snoog = null;&lt;br /&gt;
24. // more code here&lt;br /&gt;
25. }&lt;br /&gt;
Which statement is true about the objects referenced by snoog,&lt;br /&gt;
snooch, and booch immediately after line 23 executes?&lt;br /&gt;
A. None of these objects are eligible for garbage collection.&lt;br /&gt;
B. Only the object referenced by booch is eligible for garbage&lt;br /&gt;
collection.&lt;br /&gt;
C. Only the object referenced by snoog is eligible for garbage&lt;br /&gt;
collection.&lt;br /&gt;
D. Only the object referenced by snooch is eligible for garbage&lt;br /&gt;
collection.&lt;br /&gt;
E. The objects referenced by snooch and booch are eligible for garbage&lt;br /&gt;
collection.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: E&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 212&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class GC {&lt;br /&gt;
2. private Object o;&lt;br /&gt;
3. private void doSomethingElse(Object obj) { o = obj; }&lt;br /&gt;
4. public void doSomething() {&lt;br /&gt;
5. Object o = new Object();&lt;br /&gt;
6. doSomethingElse(o);&lt;br /&gt;
7. o = new Object();&lt;br /&gt;
8. doSomethingElse(null);&lt;br /&gt;
9.o=null;&lt;br /&gt;
10. }&lt;br /&gt;
11. }&lt;br /&gt;
When the doSomething method is called, after which line does the&lt;br /&gt;
Object created in line 5 become available for garbage collection?&lt;br /&gt;
A. Line 5&lt;br /&gt;
B. Line 6&lt;br /&gt;
C. Line 7&lt;br /&gt;
D. Line 8&lt;br /&gt;
E. Line 9&lt;br /&gt;
F. Line 10&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 213&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Which two are true? (Choose two.)&lt;br /&gt;
A. A finalizer may NOT be invoked explicitly.&lt;br /&gt;
B. The finalize method declared in class Object takes no action.&lt;br /&gt;
C. super.finalize() is called implicitly by any overriding finalize method.&lt;br /&gt;
D. The finalize method for a given object will be called no more than&lt;br /&gt;
once by the garbage collector.&lt;br /&gt;
E. The order in which finalize will be called on two objects is based on&lt;br /&gt;
the order in which the two objects became finalizable.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: BD&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 214&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
A class games.cards.Poker is correctly defined in the jar file Poker.jar.&lt;br /&gt;
A user wants to execute the main method of Poker on a UNIX system&lt;br /&gt;
using the command:&lt;br /&gt;
java games.cards.Poker&lt;br /&gt;
What allows the user to do this?&lt;br /&gt;
A. put Poker.jar in directory /stuff/java, and set the CLASSPATH to&lt;br /&gt;
include /stuff/java&lt;br /&gt;
B. put Poker.jar in directory /stuff/java, and set the CLASSPATH to&lt;br /&gt;
include /stuff/java/*.jar&lt;br /&gt;
C. Put Poker.jar in directory /stuff/java, and set the CLASSPATH to&lt;br /&gt;
include /stuff/java/Poker.jar&lt;br /&gt;
D. put Poker.jar in directory /stuff/java/games/cards, and set the&lt;br /&gt;
CLASSPATH to include /stuff/java&lt;br /&gt;
E. put Poker.jar in directory /stuff/java/games/cards, and set the&lt;br /&gt;
CLASSPATH to include /stuffijava/*.jar&lt;br /&gt;
F. put Poker.jar in directory /stuff/java/games/cards, and set the&lt;br /&gt;
CLASSPATH to include /stuff/java/Poker.jar&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 215&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
Given the fully-qualified class names:&lt;br /&gt;
com.foo.bar.Dog&lt;br /&gt;
com.foo.bar.blatz.Book&lt;br /&gt;
com.bar.Car&lt;br /&gt;
com.bar.blatz.Sun&lt;br /&gt;
Which graph represents the correct directory structure for a JAR file&lt;br /&gt;
from which those classes can be used by the compiler and JYM?&lt;br /&gt;
A. Jar A&lt;br /&gt;
B. Jar B&lt;br /&gt;
C. Jar C&lt;br /&gt;
D. Jar D&lt;br /&gt;
E. Jar E&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 216&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
A developer is creating a class Book that needs to access class Paper.&lt;br /&gt;
The Paper class is deployed in a JAR named myLib.jar. Which three,&lt;br /&gt;
taken independently, will allow the developer to use the Paper class&lt;br /&gt;
while compiling the Book class? (Choose three.)&lt;br /&gt;
A. The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar.&lt;br /&gt;
B. The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar.&lt;br /&gt;
C. The JAR file is located at /foo/myLib.jar and a classpath&lt;br /&gt;
environment variable is set that includes /foo/myLib.jar/Paper.class.&lt;br /&gt;
D. The JAR file is located at /foo/myLib.jar and a classpath&lt;br /&gt;
environment variable is set that includes /foo/myLib.jar.&lt;br /&gt;
E. The JAR file is located at /foo/myLib.jar and the Book class is&lt;br /&gt;
compiled using javac -cp /foo/myLib.jar/Paper Book.java.&lt;br /&gt;
F. The JAR file is located at /foo/myLib.jar and the Book class is&lt;br /&gt;
compiled using javac -d /foo/myLib.jar Book.java.&lt;br /&gt;
G. The JAR file is located at /foo/myLib.jar and the Book class is&lt;br /&gt;
compiled using javac -classpath /foo/myLib.jar Book.java.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: BDG&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 217&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. package com.company.application;&lt;br /&gt;
2.&lt;br /&gt;
3. public class MainClass {&lt;br /&gt;
4. public static void main(String[] args) { }&lt;br /&gt;
5. }&lt;br /&gt;
And MainClass exists in the /apps/com/company/application directory.&lt;br /&gt;
Assume the CLASSPATH environment variable is set to “.“ (current&lt;br /&gt;
directory). Which two java commands entered at the command line&lt;br /&gt;
will run MainClass? (Choose two.)&lt;br /&gt;
A. java MainClass if run from the /apps directory&lt;br /&gt;
B. java com.company.application.MainClass if run from the /apps&lt;br /&gt;
directory&lt;br /&gt;
C. java -classpath /apps com.company.application.MainClass if run&lt;br /&gt;
from any directory&lt;br /&gt;
D. java -classpath . MainClass if run from the&lt;br /&gt;
/apps/com/company/application directory&lt;br /&gt;
E. java -classpath /apps/com/company/application:. MainClass if run&lt;br /&gt;
from the /apps directory&lt;br /&gt;
F. java com.company.application.MainClass if run from the&lt;br /&gt;
/apps/com/company/application directory&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: BC&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 218&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
A UNIX user named Bob wants to replace his chess program with a&lt;br /&gt;
new one, but he is hot sure where the old one is installed. Bob is&lt;br /&gt;
currently able to run a Java chess program starting from his home&lt;br /&gt;
directory /home/bob using the command:&lt;br /&gt;
java -classpath /test:/home/bob/downloads/* .jar games.Chess&lt;br /&gt;
Bob’s CLASSPATH is set (at login time) to:&lt;br /&gt;
/usr/lib:/home/bob/classes:/opt/java/lib:/opt/java/lib/* .jar&lt;br /&gt;
What is a possible location for the Chess.class file?&lt;br /&gt;
A. /test/Chess.class&lt;br /&gt;
B. /home/bob/Chess.class&lt;br /&gt;
C. /test/games/Chess.class&lt;br /&gt;
D. /usr/lib/games/Chess.class&lt;br /&gt;
E. /home/bob/games/Chess.class&lt;br /&gt;
F. inside jarfile /opt/java/lib/Games.jar (with a correct manifest)&lt;br /&gt;
G. inside jarfile /home/bob/downloads/Games.jar (with a correct&lt;br /&gt;
manifest)&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 219&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public static void test(String str) {&lt;br /&gt;
12. if(str == null | str.lellgth() == 0) {&lt;br /&gt;
13. System.out.println(”String is empty”);&lt;br /&gt;
14. } else {&lt;br /&gt;
15. System.out.println(”String is not empty”);&lt;br /&gt;
16. }&lt;br /&gt;
17. }&lt;br /&gt;
And the invocation:&lt;br /&gt;
31. test(llull);&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Au exception is thrown at runtime.&lt;br /&gt;
B. “String is empty” is printed to output.&lt;br /&gt;
C. Compilation fails because of au error in line 12.&lt;br /&gt;
D. “String is not empty” is printed to output.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 220&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public static void test(String str) {&lt;br /&gt;
12. int check = 4;&lt;br /&gt;
13. if (check = str.length()) {&lt;br /&gt;
14. System.out.print(str.charAt(check -= 1) +“, “);&lt;br /&gt;
15. } else {&lt;br /&gt;
16. System.out.print(str.charAt(0) + “, “);&lt;br /&gt;
17. }&lt;br /&gt;
18. }&lt;br /&gt;
and the invocation:&lt;br /&gt;
21. test(”four”);&lt;br /&gt;
22. test(”tee”);&lt;br /&gt;
23. test(”to”);&lt;br /&gt;
What is the result?&lt;br /&gt;
A. r, t, t,&lt;br /&gt;
B. r, e, o,&lt;br /&gt;
C. Compilation fails.&lt;br /&gt;
D. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 221&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
10. public class MyClass {&lt;br /&gt;
11.&lt;br /&gt;
12. public Integer startingI;&lt;br /&gt;
13. public void methodA() {&lt;br /&gt;
14. Integer i = new Integer(25);&lt;br /&gt;
15. startingI = i;&lt;br /&gt;
16. methodB(i);&lt;br /&gt;
17. }&lt;br /&gt;
18. private void methodB(Integer i2) {&lt;br /&gt;
19. i2 = i2.intValue();&lt;br /&gt;
20.&lt;br /&gt;
21. }&lt;br /&gt;
22. }&lt;br /&gt;
If methodA is invoked, which two are true at line 20? (Choose two.)&lt;br /&gt;
A. i2 == startingI returns true.&lt;br /&gt;
B. i2 == startingI returns false.&lt;br /&gt;
C. i2.equals(startingI) returns true.&lt;br /&gt;
D. i2.equals(startingI) returns false.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: BC&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 222&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
222. Given:&lt;br /&gt;
11. class Cup { }&lt;br /&gt;
12. class PoisonCup extends Cup { }&lt;br /&gt;
21. public void takeCup(Cup c) {&lt;br /&gt;
22. if(c instanceof PoisonCup) {&lt;br /&gt;
23. System.out.println(”Inconceivable!”);&lt;br /&gt;
24. } else if(c instanceof Cup) {&lt;br /&gt;
25. System.out.println(”Dizzying intellect!”);&lt;br /&gt;
26. } else {&lt;br /&gt;
27. System.exit(0);&lt;br /&gt;
28. }&lt;br /&gt;
29. }&lt;br /&gt;
And the execution of the statements:&lt;br /&gt;
Cup cup = new PoisonCup();&lt;br /&gt;
takeCup(cup);&lt;br /&gt;
What is the output?&lt;br /&gt;
A. Inconceivable!&lt;br /&gt;
B. Dizzying intellect!&lt;br /&gt;
C. The code runs with no output.&lt;br /&gt;
D. An exception is thrown at runtime.&lt;br /&gt;
E. Compilation fails because of an error in line 22.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 223&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. String[] elements = { “for”, “tea”, “too” };&lt;br /&gt;
12. String first = (elements.length &amp;gt; 0)? elements[0] null;&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. The variable first is set to null.&lt;br /&gt;
D. The variable first is set to elements[0].&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 224&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
42. public class ClassA {&lt;br /&gt;
43. public int getValue() {&lt;br /&gt;
44.int value=0;&lt;br /&gt;
45. boolean setting = true;&lt;br /&gt;
46. String title=”Hello”;&lt;br /&gt;
47. if (value || (setting &amp;amp;&amp;amp; title == “Hello”)) { return 1; }&lt;br /&gt;
48. if (value == 1 &amp;amp; title.equals(”Hello”)) { return 2; }&lt;br /&gt;
49. }&lt;br /&gt;
50. }&lt;br /&gt;
And:&lt;br /&gt;
70. ClassA a = new ClassA();&lt;br /&gt;
71. a.getValue();&lt;br /&gt;
What is the result?&lt;br /&gt;
A. 1&lt;br /&gt;
B. 2&lt;br /&gt;
C. Compilation fails.&lt;br /&gt;
D. The code runs with no output.&lt;br /&gt;
E. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/8241770756897953790/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-211-224.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/8241770756897953790'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/8241770756897953790'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-211-224.html' title='SCJP Questions 211-224'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-2541892388498275990</id><published>2011-12-14T18:05:00.001+05:30</published><updated>2011-12-14T18:05:49.888+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 201-210"/><title type='text'>SCJP Questions 201-210</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 200&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given the command line java Pass2 and:&lt;br /&gt;
15. public class Pass2 {&lt;br /&gt;
16. public void main(String [] args) {&lt;br /&gt;
17.int x=6;&lt;br /&gt;
18. Pass2 p = new Pass2();&lt;br /&gt;
19. p.doStuff(x);&lt;br /&gt;
20. System.out.print(” main x = “+ x);&lt;br /&gt;
21. }&lt;br /&gt;
22.&lt;br /&gt;
23. void doStuff(int x) {&lt;br /&gt;
24. System.out.print(” doStuffx = “+ x++);&lt;br /&gt;
25. }&lt;br /&gt;
26. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. doStuffx = 6 main x = 6&lt;br /&gt;
D. doStuffx = 6 main x = 7&lt;br /&gt;
E. doStuffx = 7 main x = 6&lt;br /&gt;
F. doStuffx = 7 main x = 7&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 201&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
15. public class Yippee {&lt;br /&gt;
16. public static void main(String [] args) {&lt;br /&gt;
17. for(int x = 1; x &amp;lt; args.length; x++) {&lt;br /&gt;
18. System.out.print(args[x] +“ “);&lt;br /&gt;
19. }&lt;br /&gt;
20. }&lt;br /&gt;
21. }&lt;br /&gt;
and two separate command line invocations:&lt;br /&gt;
java Yippee&lt;br /&gt;
java Yippee 1234&lt;br /&gt;
What is the result?&lt;br /&gt;
A. No output is produced.&lt;br /&gt;
123&lt;br /&gt;
B. No output is produced.&lt;br /&gt;
234&lt;br /&gt;
C. No output is produced.&lt;br /&gt;
1234&lt;br /&gt;
D. An exception is thrown at runtime.&lt;br /&gt;
123&lt;br /&gt;
E. An exception is thrown at runtime.&lt;br /&gt;
234&lt;br /&gt;
F. An exception is thrown at rijntime.&lt;br /&gt;
1234&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 202&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
12. public class Yippee2 {&lt;br /&gt;
13.&lt;br /&gt;
14. static public void main(String [] yahoo) {&lt;br /&gt;
15. for(int x= 1; x&amp;lt;yahoo.length; x++) {&lt;br /&gt;
16. System.out.print(yahoo[x] + “ “);&lt;br /&gt;
17. }&lt;br /&gt;
18. }&lt;br /&gt;
19. }&lt;br /&gt;
and the command line invocation:&lt;br /&gt;
java Yippee2 a b c&lt;br /&gt;
What is the result?&lt;br /&gt;
A.a b&lt;br /&gt;
B.b c&lt;br /&gt;
C.a b c&lt;br /&gt;
D. Compilation fails.&lt;br /&gt;
E. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 203&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public class Commander {&lt;br /&gt;
12. public static void main(String[] args) {&lt;br /&gt;
13. String myProp = /* insert code here */&lt;br /&gt;
14. System.out.println(myProp);&lt;br /&gt;
15. }&lt;br /&gt;
16. }&lt;br /&gt;
and the command line:&lt;br /&gt;
java -Dprop.custom=gobstopper Commander&lt;br /&gt;
Which two, placed on line 13, will produce the output gobstopper?&lt;br /&gt;
(Choose two.)&lt;br /&gt;
A. System.load(”prop.custom”);&lt;br /&gt;
B. System.getenv(”prop.custom”);&lt;br /&gt;
C. System.property(”prop.custom”);&lt;br /&gt;
D. System.getProperty(”prop.custom”);&lt;br /&gt;
E. System.getProperties().getProperty(”prop.custom”);&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: DE&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 204&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
11. class Payload {&lt;br /&gt;
12. private int weight;&lt;br /&gt;
13. public Payload(int wt) { weight = wt; }&lt;br /&gt;
13. public void setWeight(mt w) { weight = w; }&lt;br /&gt;
15. public String toString { return Integer.toString(weight); }&lt;br /&gt;
16. }&lt;br /&gt;
17.&lt;br /&gt;
18. public class TestPayload {&lt;br /&gt;
19. static void changePayload(Payload p) {&lt;br /&gt;
20. /* insert code here */&lt;br /&gt;
21. }&lt;br /&gt;
22.&lt;br /&gt;
23. public static void main(String[] args) {&lt;br /&gt;
24. Payload p = new Payload();&lt;br /&gt;
25. p.setWeight(1024);&lt;br /&gt;
26. changePayload(p);&lt;br /&gt;
27. System.out.println(”The value of p is “+ p);&lt;br /&gt;
28. }&lt;br /&gt;
29. }&lt;br /&gt;
Which statement, placed at line 20, causes the code to print “The&lt;br /&gt;
value of p is 420.”?&lt;br /&gt;
A. p.setWeight(420);&lt;br /&gt;
B. p.changePayload(420);&lt;br /&gt;
C. p = new Payload(420);&lt;br /&gt;
D. Payload.setWeight(420);&lt;br /&gt;
E. p = Payload.setWeight(420);&lt;br /&gt;
F. p = new Payload();&lt;br /&gt;
p.setWeight(420);&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 205&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
1. public class Item {&lt;br /&gt;
2. private String desc;&lt;br /&gt;
3. public String getDescription() { return desc; }&lt;br /&gt;
4. public void setDescription(String d) { desc = d; }&lt;br /&gt;
5.&lt;br /&gt;
6. public static void modifyDesc(Item item, String desc) {&lt;br /&gt;
7. item = new Item();&lt;br /&gt;
8. item.setDescription(desc);&lt;br /&gt;
9. }&lt;br /&gt;
10. public static void main(String[] args) {&lt;br /&gt;
11. Item it = new Item();&lt;br /&gt;
12. it.setDescription(”Gobstopper”);&lt;br /&gt;
13. Item it2 = new Item();&lt;br /&gt;
14. it2.setDescription(”Fizzylifting”);&lt;br /&gt;
15. modifyDesc(it, “Scrumdiddlyumptious”);&lt;br /&gt;
16. System.out.println(it.getDescription());&lt;br /&gt;
17. System.out.println(it2.getDescription());&lt;br /&gt;
18. }&lt;br /&gt;
19. }&lt;br /&gt;
What is the outcome of the code?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. Gobstopper&lt;br /&gt;
Fizzylifting&lt;br /&gt;
C. Gobstopper&lt;br /&gt;
Scrumdiddlyumptious&lt;br /&gt;
D. Scrumdiddlyumptious&lt;br /&gt;
Fizzylifltng&lt;br /&gt;
E. Scrumdiddlyumptious&lt;br /&gt;
Scrumdiddlyumptious&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 206&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public class ItemTest {&lt;br /&gt;
12. private final mt id;&lt;br /&gt;
13. public ItemTest(int id) { this.id = id; }&lt;br /&gt;
14. public void updateId(int newId) { id = newId; }&lt;br /&gt;
15.&lt;br /&gt;
16. public static void main(String[] args) {&lt;br /&gt;
17. ItemTest fa = new ItemTest(42);&lt;br /&gt;
18. fa.updateId(69);&lt;br /&gt;
19. System.out.println(fa.id);&lt;br /&gt;
20. }&lt;br /&gt;
21. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. The attribute id in the Item object remains unchanged.&lt;br /&gt;
D. The attribute id in the Item object is modified to the new value.&lt;br /&gt;
E. A new Item object is created with the preferred value in the id&lt;br /&gt;
attribute.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 207&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
10. class Inner {&lt;br /&gt;
11. private int x;&lt;br /&gt;
12. public void setX( int x) { this.x = x; }&lt;br /&gt;
13. public int getX() { return x; }&lt;br /&gt;
14. }&lt;br /&gt;
15.&lt;br /&gt;
16. class Outer {&lt;br /&gt;
17. private Inner y;&lt;br /&gt;
18. public void setY( Inner y) { this.y = y; }&lt;br /&gt;
19. public Inner getY() { return y; }&lt;br /&gt;
20. }&lt;br /&gt;
21.&lt;br /&gt;
22. public class Gamma {&lt;br /&gt;
23. public static void main( String[] args) {&lt;br /&gt;
24. Outer o = new Outer();&lt;br /&gt;
25. Inner i = new Inner();&lt;br /&gt;
26.int n=10;&lt;br /&gt;
27. i.setX(n);&lt;br /&gt;
28. o.setY(i);&lt;br /&gt;
29. // insert code here&lt;br /&gt;
30. System.out.println( o.getY().getX());&lt;br /&gt;
31. }&lt;br /&gt;
32. }&lt;br /&gt;
Which three code fragments, added individually at line 29, produce the&lt;br /&gt;
output 100? (Choose three.)&lt;br /&gt;
A. n = 100;&lt;br /&gt;
B. i.setX( 100);&lt;br /&gt;
C. o.getY().setX( 100);&lt;br /&gt;
D. i = new Inner(); i.setX( 100);&lt;br /&gt;
E. o.setY( i); i = new Inner(); i.setX( 100);&lt;br /&gt;
F. i = new Inner(); i.setX( 100); o.setY( i);&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: BCF&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 208&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
10. class Foo {&lt;br /&gt;
11. private int x;&lt;br /&gt;
12.publicFoo(intx) {this.x=x; }&lt;br /&gt;
13. public void setX( int x) { this.x = x; }&lt;br /&gt;
14. public int getX() { return x; }&lt;br /&gt;
15. }&lt;br /&gt;
16.&lt;br /&gt;
17. public class Gamma {&lt;br /&gt;
18.&lt;br /&gt;
19. static Foo fooBar( Foo foo) {&lt;br /&gt;
20. foo = new Foo( 100);&lt;br /&gt;
21. return foo;&lt;br /&gt;
22. }&lt;br /&gt;
23.&lt;br /&gt;
24. public static void main( String[] args) {&lt;br /&gt;
25. Foo foo = new Foo( 300);&lt;br /&gt;
26. System.out.print( foo.getX() + “-“);&lt;br /&gt;
27.&lt;br /&gt;
28. Foo fooFoo = fooBar( foo);&lt;br /&gt;
29. System.out.print( foo.getX() + “-“);&lt;br /&gt;
30. System.out.print( fooFoo.getX() + “-“);&lt;br /&gt;
31.&lt;br /&gt;
32. foo = fooBar( fooFoo);&lt;br /&gt;
33. System.out.print( foo.getX() + “-“);&lt;br /&gt;
34. System.out.prmt( fooFoo.getX());&lt;br /&gt;
35. }&lt;br /&gt;
36. }&lt;br /&gt;
What is the output of this program?&lt;br /&gt;
A. 300-100-100-100-100&lt;br /&gt;
B. 300-300-100-100-100&lt;br /&gt;
C. 300-300-300-100-100&lt;br /&gt;
D. 300-300-300-300-100&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 209&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public void genNumbers() {&lt;br /&gt;
12. ArrayList numbers = new ArrayList();&lt;br /&gt;
13. for (int i=0; i&amp;lt;10; i++) {&lt;br /&gt;
14. int value = i * ((int) Math.random());&lt;br /&gt;
15. Integer intObj = new Integer(value);&lt;br /&gt;
16. numbers.add(intObj);&lt;br /&gt;
17. }&lt;br /&gt;
18. System.out.println(numbers);&lt;br /&gt;
19. }&lt;br /&gt;
Which line of code marks the earliest point that an object referenced&lt;br /&gt;
by intObj becomes a candidate for garbage collection?&lt;br /&gt;
A. Line 16&lt;br /&gt;
B. Line 17&lt;br /&gt;
C. Line 18&lt;br /&gt;
D. Line 19&lt;br /&gt;
E. The object is NOT a candidate for garbage collection.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 210&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. rbo = new ReallyBigObject();&lt;br /&gt;
12. // more code here&lt;br /&gt;
13. rbo = null;&lt;br /&gt;
14. /* insert code here */&lt;br /&gt;
Which statement should be placed at line 14 to suggest that the virtual&lt;br /&gt;
machine expend effort toward recycling the memory used by the&lt;br /&gt;
object rbo?&lt;br /&gt;
A. System.gc();&lt;br /&gt;
B. Runtime.gc();&lt;br /&gt;
C. System.freeMemory();&lt;br /&gt;
D. Runtime.getRuntime().growHeap();&lt;br /&gt;
E. Runtime.getRuntime().freeMemory();&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/2541892388498275990/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-201-210.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/2541892388498275990'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/2541892388498275990'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-201-210.html' title='SCJP Questions 201-210'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-6198351053422744921</id><published>2011-12-14T18:01:00.001+05:30</published><updated>2011-12-14T18:01:41.411+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 191-200"/><title type='text'>SCJP Questions 191-200</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 191&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given classes defined in two different files:&lt;br /&gt;
1. package util;&lt;br /&gt;
2. public class BitUtils {&lt;br /&gt;
3. public static void process(byte[]) { /* more code here */ }&lt;br /&gt;
4. }&lt;br /&gt;
1. package app;&lt;br /&gt;
2. public class SomeApp {&lt;br /&gt;
3. public static void main(String[] args) {&lt;br /&gt;
4. byte[] bytes = new byte[256];&lt;br /&gt;
5. // insert code here&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
What is required at line 5 in class SomeApp to use the process method&lt;br /&gt;
of BitUtils?&lt;br /&gt;
A. process(bytes);&lt;br /&gt;
B. BitUtils.process(bytes);&lt;br /&gt;
C. util.BitUtils.process(bytes);&lt;br /&gt;
D. SomeApp cannot use methods in BitUtils.&lt;br /&gt;
E. import util.BitUtils.*; process(bytes);&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question : 192&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given classes defined in two different files:&lt;br /&gt;
1. package util;&lt;br /&gt;
2. public class BitUtils {&lt;br /&gt;
3. private static void process(byte[] b) { }&lt;br /&gt;
4. }&lt;br /&gt;
1. package app;&lt;br /&gt;
2. public class SomeApp {&lt;br /&gt;
3. public static void main(String[] args) {&lt;br /&gt;
4. byte[] bytes = new byte[256];&lt;br /&gt;
5. // insert code here&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
What is required at line 5 in class SomeApp to use the process method&lt;br /&gt;
of BitUtils?&lt;br /&gt;
A. process(bytes);&lt;br /&gt;
B. BitUtils.process(bytes);&lt;br /&gt;
C. app.BitUtils.process(bytes);&lt;br /&gt;
D. util.BitUtils.process(bytes);&lt;br /&gt;
E. import util.BitUtils. *; process(bytes);&lt;br /&gt;
F. SomeApp cannot use the process method in BitUtils.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: F&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 193&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given classes defined in two different files:&lt;br /&gt;
1. package packageA;&lt;br /&gt;
2. public class Message {&lt;br /&gt;
3. String getText() { return “text”; }&lt;br /&gt;
4. }&lt;br /&gt;
and:&lt;br /&gt;
1. package packageB;&lt;br /&gt;
2. public class XMLMessage extends packageA.Message {&lt;br /&gt;
3. String getText() { return “&amp;lt;msg&amp;gt;text&amp;lt;/msg&amp;gt;”; }&lt;br /&gt;
4. public static void main(String[] args) {&lt;br /&gt;
5. System.out.println(new XMLMessage().getText());&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
What is the result of executing XMLMessage.main?&lt;br /&gt;
A. text&lt;br /&gt;
B. &amp;lt;msg&amp;gt;text&amp;lt;/msg&amp;gt;&lt;br /&gt;
C. An exception is thrown at runtime.&lt;br /&gt;
D. Compilation fails because of an error in line 2 of XMLMessage.&lt;br /&gt;
E. Compilation fails because of an error in line 3 of XMLMessage.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: E&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 194&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given a file GrizzlyBear.java:&lt;br /&gt;
1. package animals.mammals;&lt;br /&gt;
2.&lt;br /&gt;
3. public class GrizzlyBear extends Bear {&lt;br /&gt;
4. void hunt() {&lt;br /&gt;
5. Salmon s = findSalmon();&lt;br /&gt;
6. s.consume();&lt;br /&gt;
7. }&lt;br /&gt;
8. }&lt;br /&gt;
and another file, Salmon.java:&lt;br /&gt;
1. package animals.fish;&lt;br /&gt;
2.&lt;br /&gt;
3. public class Salmon extends Fish {&lt;br /&gt;
4. void consume() { /* do stuff */ }&lt;br /&gt;
5. }&lt;br /&gt;
Assume both classes are defined in the correct directories for theft&lt;br /&gt;
packages, and that the Mammal class correctly defines the&lt;br /&gt;
findSalmon() method. Which two changes allow this code to compile&lt;br /&gt;
correctly? (Choose two.)&lt;br /&gt;
A. add public to the start of line 4 in Salmon.java&lt;br /&gt;
B. add public to the start of line 4 in GrizzlyBear.java&lt;br /&gt;
C. add import animals.mammals.*; at line 2 in Salmon.java&lt;br /&gt;
D. add import animals.fish.*; at line 2 in GrizzlyBear.java&lt;br /&gt;
E. add import animals.fish.Salmon.*; at line 2 in GrizzlyBear.java&lt;br /&gt;
F. add import animals.mammals.GrizzlyBear.*;at line 2 in Salmon.java&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: AD&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 195&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given a class Repetition:&lt;br /&gt;
1. package utils;&lt;br /&gt;
2.&lt;br /&gt;
3. public class Repetition {&lt;br /&gt;
4. public static String twice(String s) { return s + s; }&lt;br /&gt;
5. }&lt;br /&gt;
and given another class Demo:&lt;br /&gt;
1. // insert code here&lt;br /&gt;
2.&lt;br /&gt;
3. public class Demo {&lt;br /&gt;
4. public static void main(String[] args) {&lt;br /&gt;
5. System.out.println(twice(”pizza”));&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
Which code should be inserted at line 1 of Demo.java to compile and&lt;br /&gt;
run Demo to print “pizzapizza”?&lt;br /&gt;
A. import utils.*;&lt;br /&gt;
B. static import utils.*;&lt;br /&gt;
C. import utils.Repetition.*;&lt;br /&gt;
D. static import utils.Repetition. *;&lt;br /&gt;
E. import utils.Repetition.twice();&lt;br /&gt;
F. import static utils.Repetition.twice;&lt;br /&gt;
G. static import utils.Repetition.twice;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: F&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 196&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. interface DeclareStuff{&lt;br /&gt;
12. public static final int EASY = 3;&lt;br /&gt;
13. void doStuff(int t); }&lt;br /&gt;
14. public class TestDeclare implements DeclareStuff {&lt;br /&gt;
15. public static void main(String [] args) {&lt;br /&gt;
16. int x=5;&lt;br /&gt;
17. new TestDeclare().doStuff(++x);&lt;br /&gt;
18. }&lt;br /&gt;
19. void doStuff(int s) {&lt;br /&gt;
20. s += EASY + ++s;&lt;br /&gt;
21. System.out.println(”s “ + s);&lt;br /&gt;
22. }&lt;br /&gt;
23. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. s 14&lt;br /&gt;
B. s 16&lt;br /&gt;
C. s 10&lt;br /&gt;
D. Compilation fails.&lt;br /&gt;
E. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 197&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. interface DoStuff2 {&lt;br /&gt;
2. float getRange(int low, int high); }&lt;br /&gt;
3.&lt;br /&gt;
4. interface DoMore {&lt;br /&gt;
5. float getAvg(int a, int b, int c); }&lt;br /&gt;
6.&lt;br /&gt;
7. abstract class DoAbstract implements DoStuff2, DoMore { }&lt;br /&gt;
8.&lt;br /&gt;
9. class DoStuff implements DoStuff2 {&lt;br /&gt;
10. public float getRange(int x, int y) { return 3.14f; } }&lt;br /&gt;
11.&lt;br /&gt;
12. interface DoAll extends DoMore {&lt;br /&gt;
13. float getAvg(int a, int b, int c, int d); }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. The file will compile without error.&lt;br /&gt;
B. Compilation fails. Only line 7 contains an error.&lt;br /&gt;
C. Compilation fails. Only line 12 contains an error.&lt;br /&gt;
D. Compilation fails. Only line 13 contains an error.&lt;br /&gt;
E. Compilation fails. Only lines 7 and 12 contain errors.&lt;br /&gt;
F. Compilation fails. Only lines 7 and 13 contain errors.&lt;br /&gt;
G. Compilation fails. Lines 7, 12, and 13 contain errors.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 198&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public class Counter {&lt;br /&gt;
12. public static void main(String[] args) {&lt;br /&gt;
13. int numArgs = /* insert code here */;&lt;br /&gt;
14. }&lt;br /&gt;
15. }&lt;br /&gt;
and the command line:&lt;br /&gt;
java Counter one fred 42&lt;br /&gt;
Which code, inserted at line 13, captures the number of arguments&lt;br /&gt;
passed into the program?&lt;br /&gt;
A. args.count&lt;br /&gt;
B. args.length&lt;br /&gt;
C. args.count()&lt;br /&gt;
D. args.length()&lt;br /&gt;
E. args.getLength()&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 199&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given a correctly compiled class whose source code is:&lt;br /&gt;
1. package com.sun.sjcp;&lt;br /&gt;
2. public class Commander {&lt;br /&gt;
3. public static void main(String[] args) {&lt;br /&gt;
4. // more code here&lt;br /&gt;
5. }&lt;br /&gt;
6. }&lt;br /&gt;
Assume that the class file is located in /foo/com/sun/sjcp/, the current&lt;br /&gt;
directory is /foo/, and that the classpath contains “.“ (current&lt;br /&gt;
directory).&lt;br /&gt;
Which command line correctly runs Commander?&lt;br /&gt;
A. java Commander&lt;br /&gt;
B. java com. sim. sjcp.Commander&lt;br /&gt;
C. java com/sun/sjcp/Commander&lt;br /&gt;
D. java -cp com.sun.sjcp Commander&lt;br /&gt;
E. java -cp com/sun/sjcp Commander&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 200&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given the command line java Pass2 and:&lt;br /&gt;
15. public class Pass2 {&lt;br /&gt;
16. public void main(String [] args) {&lt;br /&gt;
17.int x=6;&lt;br /&gt;
18. Pass2 p = new Pass2();&lt;br /&gt;
19. p.doStuff(x);&lt;br /&gt;
20. System.out.print(” main x = “+ x);&lt;br /&gt;
21. }&lt;br /&gt;
22.&lt;br /&gt;
23. void doStuff(int x) {&lt;br /&gt;
24. System.out.print(” doStuffx = “+ x++);&lt;br /&gt;
25. }&lt;br /&gt;
26. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. doStuffx = 6 main x = 6&lt;br /&gt;
D. doStuffx = 6 main x = 7&lt;br /&gt;
E. doStuffx = 7 main x = 6&lt;br /&gt;
F. doStuffx = 7 main x = 7&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/6198351053422744921/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-191-200.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/6198351053422744921'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/6198351053422744921'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-191-200.html' title='SCJP Questions 191-200'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-8740372376917370682</id><published>2011-12-14T17:59:00.001+05:30</published><updated>2011-12-14T17:59:01.391+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 181-190"/><title type='text'>SCJP Questions 181-190</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 181&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class Test {&lt;br /&gt;
2. public &amp;lt;T extends Comparable&amp;gt; T findLarger(T x, T y) {&lt;br /&gt;
3. if(x.compareTo(y) &amp;gt; 0) {&lt;br /&gt;
4. return x;&lt;br /&gt;
5. } else {&lt;br /&gt;
6. return y;&lt;br /&gt;
7. }&lt;br /&gt;
8. }&lt;br /&gt;
9. }&lt;br /&gt;
and:&lt;br /&gt;
22. Test t = new Test();&lt;br /&gt;
23. // insert code here&lt;br /&gt;
Which two will compile without errors when inserted at line 23?&lt;br /&gt;
(Choose two.)&lt;br /&gt;
A. Object x = t.findLarger(123, “456”);&lt;br /&gt;
B. int x = t.findLarger(123, new Double(456));&lt;br /&gt;
C. int x = t.findLarger(123, new Integer(456));&lt;br /&gt;
D. int x = (int) t.findLarger(new Double(123), new Double(456));&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: AC&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 182&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. // insert code here&lt;br /&gt;
12. private N min, max;&lt;br /&gt;
13. public N getMin() { return min; }&lt;br /&gt;
14. public N getMax() { return max; }&lt;br /&gt;
15. public void add(N added) {&lt;br /&gt;
16. if (min == null || added.doubleValue() &amp;lt;min.doubleValue())&lt;br /&gt;
17. min = added;&lt;br /&gt;
18. if (max == null ||added.doubleValue() &amp;gt; max.doubleValue())&lt;br /&gt;
19. max = added;&lt;br /&gt;
20. }&lt;br /&gt;
21. }&lt;br /&gt;
Which two, inserted at line 11, will allow the code to compile? (Choose&lt;br /&gt;
two.)&lt;br /&gt;
A. public class MinMax&amp;lt;?&amp;gt; {&lt;br /&gt;
B. public class MinMax&amp;lt;? extends Number&amp;gt; {&lt;br /&gt;
C. public class MinMax&amp;lt;N extends Object&amp;gt; {&lt;br /&gt;
D. public class MinMax&amp;lt;N extends Number&amp;gt; {&lt;br /&gt;
E. public class MinMax&amp;lt;? extends Object&amp;gt; {&lt;br /&gt;
F. public class MinMax&amp;lt;N extends Integer&amp;gt; {&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: DF&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 183&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
A programmer must create a generic class MinMax and the type&lt;br /&gt;
parameter of MinMax must implement Comparable. Which&lt;br /&gt;
implementation of MinMax will compile?&lt;br /&gt;
A. class MinMax&amp;lt;E extends Comparable&amp;lt;E&amp;gt;&amp;gt; {&lt;br /&gt;
E min=null;&lt;br /&gt;
E max=null;&lt;br /&gt;
public MinMax() { }&lt;br /&gt;
public void put(E value) { /* store min or max */ }&lt;br /&gt;
}&lt;br /&gt;
B. class MinMax&amp;lt;E implements Comparable&amp;lt;E&amp;gt;&amp;gt; {&lt;br /&gt;
E min=null;&lt;br /&gt;
E max=null;&lt;br /&gt;
public MinMax() { }&lt;br /&gt;
public void put(E value) { /* store min or max */ }&lt;br /&gt;
}&lt;br /&gt;
C. class MinMax&amp;lt;E extends Comparable&amp;lt;E&amp;gt;&amp;gt; {&lt;br /&gt;
&amp;lt;E&amp;gt; E min = null;&lt;br /&gt;
&amp;lt;E&amp;gt; E max = null;&lt;br /&gt;
public MinMax() { }&lt;br /&gt;
public &amp;lt;E&amp;gt; void put(E value) { /* store min or max */ }&lt;br /&gt;
}&lt;br /&gt;
D. class MinMax&amp;lt;E implements Comparable&amp;lt;E&amp;gt;&amp;gt; {&lt;br /&gt;
&amp;lt;E&amp;gt; E min = null;&lt;br /&gt;
&amp;lt;E&amp;gt; E max = null;&lt;br /&gt;
public MinMax() { }&lt;br /&gt;
public &amp;lt;E&amp;gt; void put(E value) { /* store min or max */ }&lt;br /&gt;
}&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 184&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class Drink implements Comparable {&lt;br /&gt;
2. public String name;&lt;br /&gt;
3. public int compareTo(Object o) {&lt;br /&gt;
4. return 0;&lt;br /&gt;
5. }&lt;br /&gt;
6. }&lt;br /&gt;
and:&lt;br /&gt;
20. Drink one = new Drink();&lt;br /&gt;
21. Drink two = new Drink();&lt;br /&gt;
22. one.name= “Coffee”;&lt;br /&gt;
23. two.name= “Tea”;&lt;br /&gt;
23. TreeSet set = new TreeSet();&lt;br /&gt;
24. set.add(one);&lt;br /&gt;
25. set.add(two);&lt;br /&gt;
A programmer iterates over the TreeSet and prints the name of each&lt;br /&gt;
Drink object.&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Tea&lt;br /&gt;
B. Coffee&lt;br /&gt;
C. Coffee&lt;br /&gt;
Tea&lt;br /&gt;
D. Compilation fails.&lt;br /&gt;
E. The code runs with no output.&lt;br /&gt;
F. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 185&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. List list = // more code here&lt;br /&gt;
12. Collections.sort(list, new MyComparator());&lt;br /&gt;
Which code will sort this list in the opposite order of the sort in line&lt;br /&gt;
12?&lt;br /&gt;
A. Collections.reverseSort(list, new MyComparator());&lt;br /&gt;
B. Collections.sort(list, new MyComparator());&lt;br /&gt;
list.reverse();&lt;br /&gt;
C. Collections.sort(list, new InverseComparator(&lt;br /&gt;
new MyComparator()));&lt;br /&gt;
D. Collections.sort(list, Collections.reverseOrder(&lt;br /&gt;
new MyComparator()));&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 186&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
int[] myArray=newint[] {1, 2,3,4, 5};&lt;br /&gt;
What allows you to create a list from this array?&lt;br /&gt;
A. List myList = myArray.asList();&lt;br /&gt;
B. List myList = Arrays.asList(myArray);&lt;br /&gt;
C. List myList = new ArrayList(myArray);&lt;br /&gt;
D. List myList = Collections.fromArray(myArray);&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 187&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
13. public static void search(List&amp;lt;String&amp;gt; list) {&lt;br /&gt;
14. list.clear();&lt;br /&gt;
15. list.add(”b”);&lt;br /&gt;
16. list.add(”a”);&lt;br /&gt;
17. list.add(”c”);&lt;br /&gt;
18. System.out.println(Collections.binarySearch(list, “a”));&lt;br /&gt;
19. }&lt;br /&gt;
What is the result of calling search with a valid List implementation?&lt;br /&gt;
A. 0&lt;br /&gt;
B. 1&lt;br /&gt;
C. 2&lt;br /&gt;
D. a&lt;br /&gt;
E. b&lt;br /&gt;
F. c&lt;br /&gt;
G. The result is undefined.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: G&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 188&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. import java.util.*;&lt;br /&gt;
2.&lt;br /&gt;
3. public class LetterASort {&lt;br /&gt;
4. public static void main(String[] args) {&lt;br /&gt;
5. ArrayList&amp;lt;String&amp;gt; strings = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
6. strings.add(’aAaA”);&lt;br /&gt;
7. strings.add(”AaA”);&lt;br /&gt;
8. strings.add(’aAa”);&lt;br /&gt;
9. strings.add(”AAaa”);&lt;br /&gt;
10. Collections.sort(strings);&lt;br /&gt;
11. for (String s: strings) { System.out.print(s + “ “); }&lt;br /&gt;
12. }&lt;br /&gt;
13. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. aAaA aAa AAaa AaA&lt;br /&gt;
C. AAaa AaA aAa aAaA&lt;br /&gt;
D. AaA AAaa aAaA aAa&lt;br /&gt;
E. aAa AaA aAaA AAaa&lt;br /&gt;
F. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 189&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
ArrayList a = new ArrayList();&lt;br /&gt;
containing the values {“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”}&lt;br /&gt;
Which code will return 2?&lt;br /&gt;
A. Collections. sort(a, a.reverse());&lt;br /&gt;
int result = Collections.binarySearch(a, “6”);&lt;br /&gt;
B. Comparator c = Collections.reverseOrder();&lt;br /&gt;
Collections.sort(a, c);&lt;br /&gt;
int result = Collections.binarySearch(a, “6”);&lt;br /&gt;
C. Comparator c = Collections.reverseOrder();&lt;br /&gt;
Collections.sort(a, c);&lt;br /&gt;
int result = Collections.binarySearch(a, “6”,c);&lt;br /&gt;
D. Comparator c = Collections.reverseOrder(a);&lt;br /&gt;
Collections.sort(a, c);&lt;br /&gt;
int result = Collections.binarySearch(a, “6”,c);&lt;br /&gt;
E. Comparator c = new InverseComparator(new Comparator());&lt;br /&gt;
Collections.sort(a);&lt;br /&gt;
int result = Collections.binarySearch(a, “6”,c);&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 190&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
34. HashMap props = new HashMap();&lt;br /&gt;
35. props.put(”key45”, “some value”);&lt;br /&gt;
36. props.put(”key12”, “some other value”);&lt;br /&gt;
37. props.put(”key39”, “yet another value”);&lt;br /&gt;
38. Set s = props.keySet();&lt;br /&gt;
39. // insert code here&lt;br /&gt;
What, inserted at line 39, will sort the keys in the props HashMap?&lt;br /&gt;
A. Arrays.sort(s);&lt;br /&gt;
B. s = new TreeSet(s);&lt;br /&gt;
C. Collections.sort(s);&lt;br /&gt;
D. s = new SortedSet(s);&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/8740372376917370682/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-181-190.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/8740372376917370682'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/8740372376917370682'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-181-190.html' title='SCJP Questions 181-190'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-7879601320473011179</id><published>2011-12-14T17:57:00.001+05:30</published><updated>2011-12-14T17:57:34.812+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 171-180"/><title type='text'>SCJP Questions 171-180</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 171&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class Person {&lt;br /&gt;
2. private String name;&lt;br /&gt;
3. public Person(String name) { this.name = name; }&lt;br /&gt;
4. public boolean equals(Person p) {&lt;br /&gt;
5. return p.name.equals(this.name);&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
Which is true?&lt;br /&gt;
A. The equals method does NOT properly override the Object.equals&lt;br /&gt;
method.&lt;br /&gt;
B. Compilation fails because the private attribute p.name cannot be&lt;br /&gt;
accessed in line 5.&lt;br /&gt;
C. To work correctly with hash-based data structures, this class must&lt;br /&gt;
also implement the hashCode method.&lt;br /&gt;
D. When adding Person objects to a java.util.Set collection, the equals&lt;br /&gt;
method in line 4 will prevent duplicates.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 172&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Which two statements are true about the hashCode method? (Choose&lt;br /&gt;
two.)&lt;br /&gt;
A. The hashCode method for a given class can be used to test for&lt;br /&gt;
object equality and object inequality for that class.&lt;br /&gt;
B. The hashCode method is used by the java.util.SortedSet collection&lt;br /&gt;
class to order the elements within that set.&lt;br /&gt;
C. The hashCode method for a given class can be used to test for&lt;br /&gt;
object inequality, but NOT object equality, for that class.&lt;br /&gt;
D. The only important characteristic of the values returned by a&lt;br /&gt;
hashCode method is that the distribution of values must follow a&lt;br /&gt;
Gaussian distribution.&lt;br /&gt;
E. The hashCode method is used by the java.util.HashSet collection&lt;br /&gt;
class to group the elements within that set into hash buckets for&lt;br /&gt;
swift retrieval.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: CE&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 173&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
enum Example { ONE, TWO, THREE }&lt;br /&gt;
Which is true?&lt;br /&gt;
A. The expressions (ONE == ONE) and ONE.equals(ONE) are both&lt;br /&gt;
guaranteed to be true.&lt;br /&gt;
B. The expression (ONE &amp;lt; TWO) is guaranteed to be true and&lt;br /&gt;
ONE.compareTo(TWO) is guaranteed to be less than one.&lt;br /&gt;
C. The Example values cannot be used in a raw java.util.HashMap;&lt;br /&gt;
instead, the programmer must use a java.util.EnumMap.&lt;br /&gt;
D. The Example values can be used in a java.util.SortedSet, but the&lt;br /&gt;
set will NOT be sorted because enumerated types do NOT implement&lt;br /&gt;
java.lang.Comparable.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 174&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
1. import java.util.*;&lt;br /&gt;
2. class KeyMaster {&lt;br /&gt;
3. public int i;&lt;br /&gt;
4. public KeyMaster(int i) { this.i = i; }&lt;br /&gt;
5. public boolean equals(Object o) { return i == ((KeyMaster)o).i; }&lt;br /&gt;
6. public int hashCode() { return i; }&lt;br /&gt;
7. }&lt;br /&gt;
8. public class MapIt {&lt;br /&gt;
9. public static void main(String[] args) {&lt;br /&gt;
10. Set&amp;lt;KeyMaster&amp;gt; set = new HashSet&amp;lt;KeyMaster&amp;gt;();&lt;br /&gt;
11. KeyMaster k1 = new KeyMaster(1);&lt;br /&gt;
12. KeyMaster k2 = new KeyMaster(2);&lt;br /&gt;
13. set.add(k1); set.add(k1);&lt;br /&gt;
14. set.add(k2); set.add(k2);&lt;br /&gt;
15. System.out.print(set.size() + “:”);&lt;br /&gt;
16. k2.i = 1;&lt;br /&gt;
17. System.out.print(set.size() + “:”);&lt;br /&gt;
18. set.remove(k1);&lt;br /&gt;
19. System.out.print(set.size() + “:”);&lt;br /&gt;
20. set.remove(k2);&lt;br /&gt;
21. System.out.print(set.size());&lt;br /&gt;
22. }&lt;br /&gt;
23. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. 4:4:2:2&lt;br /&gt;
B. 4:4:3:2&lt;br /&gt;
C. 2:2:1:0&lt;br /&gt;
D. 2:2:0:0&lt;br /&gt;
E. 2:1:0:0&lt;br /&gt;
F. 2:2:1:1&lt;br /&gt;
G. 4:3:2:1&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: F&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 175&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. import java.util.*;&lt;br /&gt;
2. public class Test {&lt;br /&gt;
3. public static void main(String[] args) {&lt;br /&gt;
4. List&amp;lt;String&amp;gt; strings = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
5. // insert code here&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
Which four, inserted at line 5, will allow compilation to succeed?&lt;br /&gt;
(Choose four.)&lt;br /&gt;
A. String s = strings.get(0);&lt;br /&gt;
B. Iterator i1 = strings.iterator();&lt;br /&gt;
C. String[] array1 = strings.toArray();&lt;br /&gt;
D. Iterator&amp;lt;String&amp;gt; i2 = strings.iterator();&lt;br /&gt;
E. String[] array2 = strings.toArray(new String[1]);&lt;br /&gt;
F. Iterator&amp;lt;String&amp;gt; i3 = strings.iterator&amp;lt;String&amp;gt;();&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: ABDE&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 176&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. import java.util.*;&lt;br /&gt;
2. public class Old {&lt;br /&gt;
3. public static Object get()(List list) {&lt;br /&gt;
4. return list.get(0);&lt;br /&gt;
5. }&lt;br /&gt;
6. }&lt;br /&gt;
Which three will compile successfully? (Choose three.)&lt;br /&gt;
A. Object o = Old.get0(new LinkedList());&lt;br /&gt;
B. Object o = Old.get0(new LinkedList&amp;lt;?&amp;gt;());&lt;br /&gt;
C. String s = Old.getfl(new LinkedList&amp;lt;String&amp;gt;());&lt;br /&gt;
D. Object o = Old.get0(new LinkedList&amp;lt;Object&amp;gt;());&lt;br /&gt;
E. String s = (String)Old.get0(new LinkedList&amp;lt;String&amp;gt;());&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: ADE&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 177&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public static void append(List list) { list.add(”0042”); }&lt;br /&gt;
12. public static void main(String[] args) {&lt;br /&gt;
13. List&amp;lt;Integer&amp;gt; intList = new ArrayList&amp;lt;Integer&amp;gt;();&lt;br /&gt;
14. append(intList);&lt;br /&gt;
15. System.out.println(intList.get(0));&lt;br /&gt;
16. }&lt;br /&gt;
‘What is the result?&lt;br /&gt;
A. 42&lt;br /&gt;
B. 0042&lt;br /&gt;
C. An exception is thrown at runtime.&lt;br /&gt;
D. Compilation fails because of an error in line 13.&lt;br /&gt;
E. Compilation fails because of an error in line 14.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 178&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given a pre-generics implementation of a method:&lt;br /&gt;
11. public static int sum(List list) {&lt;br /&gt;
12. int sum = 0;&lt;br /&gt;
13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {&lt;br /&gt;
14. int i = ((Integer)iter.next()).intValue();&lt;br /&gt;
15. sum += i;&lt;br /&gt;
16. }&lt;br /&gt;
17. return sum;&lt;br /&gt;
18. }&lt;br /&gt;
Which three changes must be made to the method sum to use&lt;br /&gt;
generics? (Choose three.)&lt;br /&gt;
A. remove line 14&lt;br /&gt;
B. replace line 14 with “int i = iter.next();”&lt;br /&gt;
C. replace line 13 with “for (int i : intList) {“&lt;br /&gt;
D. replace line 13 with “for (Iterator iter : intList) {“&lt;br /&gt;
E. replace the method declaration with “sum(List&amp;lt;int&amp;gt; intList)”&lt;br /&gt;
F. replace the method declaration with “sum(List&amp;lt;Integer&amp;gt; intList)”&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: ACF&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 179&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
classA {}&lt;br /&gt;
class B extends A {}&lt;br /&gt;
class C extends A {}&lt;br /&gt;
class D extends B {}&lt;br /&gt;
Which three statements are true? (Choose three.)&lt;br /&gt;
A. The type List&amp;lt;A&amp;gt; is assignable to List.&lt;br /&gt;
B. The type List&amp;lt;B&amp;gt; is assignable to List&amp;lt;A&amp;gt;.&lt;br /&gt;
C. The type List&amp;lt;Object&amp;gt; is assignable to List&amp;lt;?&amp;gt;.&lt;br /&gt;
D. The type List&amp;lt;D&amp;gt; is assignable to List&amp;lt;? extends B&amp;gt;.&lt;br /&gt;
E. The type List&amp;lt;? extends A&amp;gt; is assignable to List&amp;lt;A&amp;gt;.&lt;br /&gt;
F. The type List&amp;lt;Object&amp;gt; is assignable to any List reference.&lt;br /&gt;
G. The type List&amp;lt;? extends B&amp;gt; is assignable to List&amp;lt;? extends A&amp;gt;.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: CDG&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 180&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public void addStrings(List list) {&lt;br /&gt;
12. list.add(”foo”);&lt;br /&gt;
13. list.add(”bar”);&lt;br /&gt;
14. }&lt;br /&gt;
What must you change in this method to compile without warnings?&lt;br /&gt;
A. add this code after line 11:&lt;br /&gt;
list = (List&amp;lt;String&amp;gt;) list;&lt;br /&gt;
B. change lines 12 and 13 to:&lt;br /&gt;
list.add&amp;lt;String&amp;gt;(”foo”);&lt;br /&gt;
list.add&amp;lt;String&amp;gt;(”bar”);&lt;br /&gt;
C. change the method signature on line 11 to:&lt;br /&gt;
public void addStrings(List&amp;lt;? extends String&amp;gt; list) {&lt;br /&gt;
D. change the method signature on line 11 to:&lt;br /&gt;
public void addStrings(List&amp;lt;? super String&amp;gt; list) {&lt;br /&gt;
E. No changes are necessary. This method compiles without warnings.&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;strong&gt;Answer: D&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/7879601320473011179/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-171-180.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/7879601320473011179'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/7879601320473011179'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-171-180.html' title='SCJP Questions 171-180'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-8817640404466076432</id><published>2011-12-14T17:53:00.001+05:30</published><updated>2011-12-14T17:53:54.826+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 161-170"/><title type='text'>SCJP Questions 161-170</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 161&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
10. interface Jumper { public void jump(); }&lt;br /&gt;
......&lt;br /&gt;
20. class Animal {}&lt;br /&gt;
......&lt;br /&gt;
30. class Dog extends Animal {&lt;br /&gt;
31. Tail tail;&lt;br /&gt;
32. }&lt;br /&gt;
......&lt;br /&gt;
40. class Beagle extends Dog implements Jumper {&lt;br /&gt;
41. public void jump() { }&lt;br /&gt;
42. }&lt;br /&gt;
.......&lt;br /&gt;
50. class Cat implements Jumper {&lt;br /&gt;
51. public void jump() { }&lt;br /&gt;
52. }&lt;br /&gt;
Which three are true? (Choose three.)&lt;br /&gt;
A. Cat is-a Animal&lt;br /&gt;
B. Cat is-a Jumper&lt;br /&gt;
C. Dog is-a Animal&lt;br /&gt;
D. Dog is-a Jumper&lt;br /&gt;
E. Cat has-a Animal&lt;br /&gt;
F. Beagle has-a Tail&lt;br /&gt;
G. Beagle has-a Jumper&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: BCF&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 162&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. import java.util.*;&lt;br /&gt;
2. public class Example {&lt;br /&gt;
3. public static void main(String[] args) {&lt;br /&gt;
4. // insert code here&lt;br /&gt;
5. set.add(new integer(2));&lt;br /&gt;
6. set.add(new integer(l));&lt;br /&gt;
7. System.out.println(set);&lt;br /&gt;
8. }&lt;br /&gt;
9. }&lt;br /&gt;
Which code, inserted at line 4, guarantees that this program will&lt;br /&gt;
output [1, 2]?&lt;br /&gt;
A. Set set = new TreeSet();&lt;br /&gt;
B. Set set = new HashSet();&lt;br /&gt;
C. Set set = new SortedSet();&lt;br /&gt;
D. List set = new SortedList();&lt;br /&gt;
E. Set set = new LinkedHashSet();&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 163&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. import java.util.*;&lt;br /&gt;
2. public class PQ {&lt;br /&gt;
3. public static void main(String[] args) {&lt;br /&gt;
4. PriorityQueue&amp;lt;String&amp;gt; pq = new PriorityQueue&amp;lt;String&amp;gt;();&lt;br /&gt;
5. pq.add(”carrot”);&lt;br /&gt;
6. pq.add(”apple”);&lt;br /&gt;
7. pq.add(”banana”);&lt;br /&gt;
8. System.out.println(pq.poll() +”:” + pq.peek());&lt;br /&gt;
9. }&lt;br /&gt;
10. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. apple:apple&lt;br /&gt;
B. carrot:apple&lt;br /&gt;
C. apple:banana&lt;br /&gt;
D. banana:apple&lt;br /&gt;
E. carrot:carrot&lt;br /&gt;
F. carrot:banana&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 164&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. import java.util.*;&lt;br /&gt;
2. public class WrappedString {&lt;br /&gt;
3. private String s;&lt;br /&gt;
4. public WrappedString(String s) { this.s = s; }&lt;br /&gt;
5. public static void main(String[] args) {&lt;br /&gt;
6. HashSet&amp;lt;Object&amp;gt; hs = new HashSet&amp;lt;Object&amp;gt;();&lt;br /&gt;
7. WrappedString ws1 = new WrappedString(”aardvark”);&lt;br /&gt;
8. WrappedString ws2 = new WrappedString(”aardvark”);&lt;br /&gt;
9. String s1 = new String(”aardvark”);&lt;br /&gt;
10. String s2 = new String(”aardvark”);&lt;br /&gt;
11. hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2);&lt;br /&gt;
12. System.out.println(hs.size()); } }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. 0&lt;br /&gt;
B. 1&lt;br /&gt;
C. 2&lt;br /&gt;
D. 3&lt;br /&gt;
E. 4&lt;br /&gt;
F. Compilation fails.&lt;br /&gt;
G. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 165&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
1. import java.util.*;&lt;br /&gt;
2. public class TestSet {&lt;br /&gt;
3. enum Example { ONE, TWO, THREE }&lt;br /&gt;
4. public static void main(String[] args) {&lt;br /&gt;
5. Collection coll = new ArrayList();&lt;br /&gt;
6. coll.add(Example.THREE);&lt;br /&gt;
7. coll.add(Example.THREE);&lt;br /&gt;
8. coll.add(Example.THREE);&lt;br /&gt;
9. coll.add(Example.TWO);&lt;br /&gt;
10. coll.add(Example.TWO);&lt;br /&gt;
11. coll.add(Example.ONE);&lt;br /&gt;
12. Set set = new HashSet(coll);&lt;br /&gt;
13. }&lt;br /&gt;
14. }&lt;br /&gt;
Which statement is true about the set variable on line 12?&lt;br /&gt;
A. The set variable contains all six elements from the coll collection,&lt;br /&gt;
and the order is guaranteed to be preserved.&lt;br /&gt;
B. The set variable contains only three elements from the coll&lt;br /&gt;
collection, and the order is guaranteed to be preserved.&lt;br /&gt;
C. The set variable contains all six elements from the coil collection,&lt;br /&gt;
but the order is NOT guaranteed to be preserved.&lt;br /&gt;
D. The set variable contains only three elements from the coil&lt;br /&gt;
collection, but the order is NOT guaranteed to be preserved.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 166&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class Score implements Comparable&amp;lt;Score&amp;gt; {&lt;br /&gt;
2. private int wins, losses;&lt;br /&gt;
3. public Score(int w, int 1) { wins = w; losses = 1; }&lt;br /&gt;
4. public int getWins() { return wins; }&lt;br /&gt;
5. public int getLosses() { return losses; }&lt;br /&gt;
6. public String toString() {&lt;br /&gt;
7. return “&amp;lt;“ + wins + “,“ + losses + “&amp;gt;”;&lt;br /&gt;
8. }&lt;br /&gt;
9. // insert code here&lt;br /&gt;
10. }&lt;br /&gt;
Which method will complete this class?&lt;br /&gt;
A. public int compareTo(Object o) {/*mode code here*/}&lt;br /&gt;
B. public int compareTo(Score other) {/*more code here*/}&lt;br /&gt;
C. public int compare(Score s1,Score s2){/*more code here*/}&lt;br /&gt;
D. public int compare(Object o1,Object o2){/*more code here*/}&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 167&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
A programmer has an algorithm that requires a java.util.List that&lt;br /&gt;
provides an efficient implementation of add(0,object), but does&lt;br /&gt;
NOT need to support quick random access. What supports these&lt;br /&gt;
requirements?&lt;br /&gt;
A. java.util.Queue&lt;br /&gt;
B. java.util.ArrayList&lt;br /&gt;
C. java.util.LinearList&lt;br /&gt;
D. java.util.LinkedList&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 168&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public class Person {&lt;br /&gt;
12. private String name, comment;&lt;br /&gt;
13. private int age;&lt;br /&gt;
14. public Person(String n, int a, String c) {&lt;br /&gt;
15. name = n; age = a; comment = c;&lt;br /&gt;
16. }&lt;br /&gt;
17. public boolean equals(Object o) {&lt;br /&gt;
18. if(! (o instanceof Person)) return false;&lt;br /&gt;
19, Person p = (Person)o;&lt;br /&gt;
20. return age == p.age &amp;amp;&amp;amp; name.equals(p.name);&lt;br /&gt;
21. }&lt;br /&gt;
22. }&lt;br /&gt;
What is the appropriate definition of the hashCode method in class&lt;br /&gt;
Person?&lt;br /&gt;
A. return super.hashCode();&lt;br /&gt;
B. return name.hashCode() + age * 7;&lt;br /&gt;
C. return name.hashCode() + comment.hashCode() /2;&lt;br /&gt;
D. return name.hashCode() + comment.hashCode() / 2 - age * 3;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 169&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public class Key {&lt;br /&gt;
12. private long id1;&lt;br /&gt;
13. private long 1d2;&lt;br /&gt;
14.&lt;br /&gt;
15. // class Key methods&lt;br /&gt;
16. }&lt;br /&gt;
A programmer is developing a class Key, that will be used as a key in&lt;br /&gt;
a standard java.util.HashMap. Which two methods should be&lt;br /&gt;
overridden to assure that Key works correctly as a key? (Choose two.)&lt;br /&gt;
A. public int hashCode()&lt;br /&gt;
B. public boolean equals(Key k)&lt;br /&gt;
C. public int compareTo(Object o)&lt;br /&gt;
D. public boolean equals(Object o)&lt;br /&gt;
E. public boolean compareTo(Key k)&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: AD&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 170&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public class Person {&lt;br /&gt;
12. private name;&lt;br /&gt;
13. public Person(String name) {&lt;br /&gt;
14. this.name = name;&lt;br /&gt;
15. }&lt;br /&gt;
16. public boolean equals(Object o) {&lt;br /&gt;
17. if( !o instanceof Person ) return false;&lt;br /&gt;
18. Person p = (Person) o;&lt;br /&gt;
19. return p.name.equals(this.name);&lt;br /&gt;
20. }&lt;br /&gt;
21. }&lt;br /&gt;
Which is true?&lt;br /&gt;
A. Compilation fails because the hashCode method is not overridden.&lt;br /&gt;
B. A HashSet could contain multiple Person objects with the same&lt;br /&gt;
name.&lt;br /&gt;
C. All Person objects will have the same hash code because the&lt;br /&gt;
hashCode method is not overridden.&lt;br /&gt;
D. If a HashSet contains more than one Person object with&lt;br /&gt;
name=”Fred”, then removing another Person, also with name=”Fred”,&lt;br /&gt;
will remove them all.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/8817640404466076432/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-161-170.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/8817640404466076432'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/8817640404466076432'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-161-170.html' title='SCJP Questions 161-170'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-3220576832636487408</id><published>2011-12-14T17:52:00.001+05:30</published><updated>2011-12-14T17:52:26.397+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 151-160"/><title type='text'>SCJP Questions 151-160</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 151&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
1. public class SimpleCalc {&lt;br /&gt;
2. public int value;&lt;br /&gt;
3. public void calculate() { value += 7; }&lt;br /&gt;
4. }&lt;br /&gt;
And:&lt;br /&gt;
1. public class MultiCalc extends SimpleCalc {&lt;br /&gt;
2. public void calculate() { value -= 3; }&lt;br /&gt;
3. public void calculate(int multiplier) {&lt;br /&gt;
4. calculate();&lt;br /&gt;
5. super.calculate();&lt;br /&gt;
6. value *=multiplier;&lt;br /&gt;
7. }&lt;br /&gt;
8. public static void main(String[] args) {&lt;br /&gt;
9. MultiCalc calculator = new MultiCalc();&lt;br /&gt;
10. calculator.calculate(2);&lt;br /&gt;
11. System.out.println(”Value is: “+ calculator.value);&lt;br /&gt;
12. }&lt;br /&gt;
13. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Value is: 8&lt;br /&gt;
B. Compilation fails.&lt;br /&gt;
C. Value is: 12&lt;br /&gt;
D. Value is: -12&lt;br /&gt;
E. The code runs with no output.&lt;br /&gt;
F. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 152&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
10. public class Hello {&lt;br /&gt;
11. String title;&lt;br /&gt;
12. int value;&lt;br /&gt;
13. public Hello() {&lt;br /&gt;
14. title += “ World”;&lt;br /&gt;
15. }&lt;br /&gt;
16. public Hello(int value) {&lt;br /&gt;
17. this.value = value;&lt;br /&gt;
18. title = “Hello”;&lt;br /&gt;
19. Hello();&lt;br /&gt;
20. }&lt;br /&gt;
21. }&lt;br /&gt;
and:&lt;br /&gt;
30. Hello c = new Hello(5);&lt;br /&gt;
31. System.out.println(c.title);&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Hello&lt;br /&gt;
B. Hello World&lt;br /&gt;
C. Compilation fails.&lt;br /&gt;
D. Hello World 5&lt;br /&gt;
E. The code runs with no output.&lt;br /&gt;
F. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 153&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
1. public class Car {&lt;br /&gt;
2. private int wheelCount;&lt;br /&gt;
3. private String vin;&lt;br /&gt;
4. public Car(String vin) {&lt;br /&gt;
5. this.vin = vin;&lt;br /&gt;
6. this.wheelCount = 4;&lt;br /&gt;
7. }&lt;br /&gt;
8. public String drive() {&lt;br /&gt;
9. return “zoom-zoom”;&lt;br /&gt;
10. }&lt;br /&gt;
11. public String getInfo() {&lt;br /&gt;
12. return “VIN: “+ vin + “wheels: “+ wheelCount;&lt;br /&gt;
13. }&lt;br /&gt;
14. }&lt;br /&gt;
And:&lt;br /&gt;
1. public class MeGo extends Car {&lt;br /&gt;
2. public MeGo(String vin) {&lt;br /&gt;
3. this.wheelCount = 3;&lt;br /&gt;
4. }&lt;br /&gt;
5. }&lt;br /&gt;
What two must the programmer do to correct the compilation errors?&lt;br /&gt;
(Choose two.)&lt;br /&gt;
A. insert a call to this() in the Car constructor&lt;br /&gt;
B. insert a call to this() in the MeGo constructor&lt;br /&gt;
C. insert a call to super() in the MeGo constructor&lt;br /&gt;
D. insert a call to super(vin) in the MeGo constructor&lt;br /&gt;
E. change the wheelCount variable in Car to protected&lt;br /&gt;
F. change line 3 in the MeGo class to super.wheelCount = 3;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: DE&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 154&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
1. public class Employee {&lt;br /&gt;
2. String name;&lt;br /&gt;
3. double baseSalary;&lt;br /&gt;
4. Employee(String name, double baseSalary) {&lt;br /&gt;
5. this.name = name;&lt;br /&gt;
6. this.baseSalary = baseSalary;&lt;br /&gt;
7. }&lt;br /&gt;
8. }&lt;br /&gt;
And:&lt;br /&gt;
1. public class Salesperson extends Employee {&lt;br /&gt;
2. double commission;&lt;br /&gt;
3. public Salesperson(String name, double baseSalary,&lt;br /&gt;
4. double commission) {&lt;br /&gt;
5. // insert code here&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
Which code, inserted at line 7, completes the Salesperson constructor?&lt;br /&gt;
A. this.commission = commission;&lt;br /&gt;
B. superb();&lt;br /&gt;
commission = commission;&lt;br /&gt;
C. this.commission = commission;&lt;br /&gt;
superb();&lt;br /&gt;
D. super(name, baseSalary);&lt;br /&gt;
this.commission = commission;&lt;br /&gt;
E. super();&lt;br /&gt;
this.commission = commission;&lt;br /&gt;
F. this.commission = commission;&lt;br /&gt;
super(name, baseSalary);&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 155&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class Blip {&lt;br /&gt;
2. protected int blipvert(int x) { return 0; }&lt;br /&gt;
3. }&lt;br /&gt;
4. class Vert extends Blip {&lt;br /&gt;
5. // insert code here&lt;br /&gt;
6. }&lt;br /&gt;
Which five methods, inserted independently at line 5, will compile?&lt;br /&gt;
(Choose five.)&lt;br /&gt;
A. public int blipvert(int x) { return 0; }&lt;br /&gt;
B. private int blipvert(int x) { return 0; }&lt;br /&gt;
C. private int blipvert(long x) { return 0; }&lt;br /&gt;
D. protected long blipvert(int x) { return 0; }&lt;br /&gt;
E. protected int blipvert(long x) { return 0; }&lt;br /&gt;
F. protected long blipvert(long x) { return 0; }&lt;br /&gt;
G. protected long blipvert(int x, int y) { return 0; }&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: ACEFG&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question156&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
10. public class Foo {&lt;br /&gt;
11. public int a;&lt;br /&gt;
12. public Foo() { a = 3; }&lt;br /&gt;
13. public void addFive() { a += 5; }&lt;br /&gt;
14. }&lt;br /&gt;
and:&lt;br /&gt;
20. public class Bar extends Foo {&lt;br /&gt;
21. public int a;&lt;br /&gt;
22. public Bar() { a = 8; }&lt;br /&gt;
23. public void addFive() { this.a +=5; }&lt;br /&gt;
24. }&lt;br /&gt;
invoked with:&lt;br /&gt;
30. Foo foo = new Bar();&lt;br /&gt;
31. foo.addFive();&lt;br /&gt;
32. System.out.println(”Value: “+ foo.a);&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Value: 3&lt;br /&gt;
B. Value: 8&lt;br /&gt;
C. Value: 13&lt;br /&gt;
D. Compilation fails.&lt;br /&gt;
E. The code runs with no output.&lt;br /&gt;
F. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 157&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
10. public class SuperCaic {&lt;br /&gt;
11. protected static int multiply(int a, int b) { return a * b; }&lt;br /&gt;
12. }&lt;br /&gt;
and:&lt;br /&gt;
20. public class SubCalc extends SuperCalc {&lt;br /&gt;
21. public static int multiply(int a, int b) {&lt;br /&gt;
22. int c = super.multiply(a, b);&lt;br /&gt;
23. return c;&lt;br /&gt;
24. }&lt;br /&gt;
25. }&lt;br /&gt;
and:&lt;br /&gt;
30. SubCalc sc = new SubCalc();&lt;br /&gt;
31. System.out.println(sc.multiply(3,4));&lt;br /&gt;
32. System.out.println(SubCalc.multiply(2,2));&lt;br /&gt;
What is the result?&lt;br /&gt;
A. 12&lt;br /&gt;
4&lt;br /&gt;
B. The code runs with no output.&lt;br /&gt;
C. An exception is thrown at runtime.&lt;br /&gt;
D. Compilation fails because of an error in line 21.&lt;br /&gt;
E. Compilation fails because of an error in line 22.&lt;br /&gt;
F. Compilation fails because of an error in line 31.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: E&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 158&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class Team extends java.util.LinkedList {&lt;br /&gt;
2. public void addPlayer(Player p) {&lt;br /&gt;
3. add(p);&lt;br /&gt;
4. }&lt;br /&gt;
5. public void compete(Team opponent) { /* more code here */ }&lt;br /&gt;
6. }&lt;br /&gt;
7. class Player { /* more code here */ }&lt;br /&gt;
Which two are true? (Choose two.)&lt;br /&gt;
A. This code will compile.&lt;br /&gt;
B. This code demonstrates proper design of an is-a relationship.&lt;br /&gt;
C. This code demonstrates proper design of a has-a relationship.&lt;br /&gt;
D. A Java programmer using the Team class could remove Player&lt;br /&gt;
objects from a Team object.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: AD&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 159&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Which four are true? (Choose four.)&lt;br /&gt;
A. Has-a relationships should never be encapsulated.&lt;br /&gt;
B. Has-a relationships should be implemented using inheritance.&lt;br /&gt;
C. Has-a relationships can be implemented using instance variables.&lt;br /&gt;
D. Is-a relationships can be implemented using the extends keyword.&lt;br /&gt;
E. Is-a relationships can be implemented using the implements&lt;br /&gt;
keyword.&lt;br /&gt;
F. The relationship between Movie and Actress is an example of an is-a&lt;br /&gt;
relationship.&lt;br /&gt;
G. An array or a collection can be used to implement a one-to-many&lt;br /&gt;
has-a relationship.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: CDEG&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 160&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Which two are true about has-a and is-a relationships? (Choose two.)&lt;br /&gt;
A. Inheritance represents an is-a relationship.&lt;br /&gt;
B. Inheritance represents a has-a relationship.&lt;br /&gt;
C. Interfaces must be used when creating a has-a relationship.&lt;br /&gt;
D. Instance variables can be used when creating a has-a relationship.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: AD&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/3220576832636487408/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-151-160.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/3220576832636487408'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/3220576832636487408'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-151-160.html' title='SCJP Questions 151-160'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-3772899608016959310</id><published>2011-12-14T17:51:00.001+05:30</published><updated>2011-12-14T17:51:23.612+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 141-150"/><title type='text'>SCJP Questions 141-150</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 141&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
10. abstract class A {&lt;br /&gt;
11. abstract void al();&lt;br /&gt;
12. void a2() { }&lt;br /&gt;
13. }&lt;br /&gt;
14. class B extends A {&lt;br /&gt;
15. void a1() { }&lt;br /&gt;
16. void a2() { }&lt;br /&gt;
17. }&lt;br /&gt;
18. class C extends B { void c1() { } }&lt;br /&gt;
and:&lt;br /&gt;
A x = new B(); C y = new C(); A z = new C();&lt;br /&gt;
Which four are valid examples of polymorphic method calls? (Choose&lt;br /&gt;
four.)&lt;br /&gt;
A. x.a2();&lt;br /&gt;
B. z.a2();&lt;br /&gt;
C. z.c1();&lt;br /&gt;
D. z.a1();&lt;br /&gt;
E. y.c1();&lt;br /&gt;
F. x.a1();&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: ABDF&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 142&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
10. interface A { void x(); }&lt;br /&gt;
11. class B implements A { public void x() { } public voidy() { } }&lt;br /&gt;
12. class C extends B { public void x() {} }&lt;br /&gt;
And:&lt;br /&gt;
20. java.util.List&amp;lt;A&amp;gt; list = new java.util.ArrayList&amp;lt;A&amp;gt;();&lt;br /&gt;
21. list.add(new B());&lt;br /&gt;
22. list.add(new C());&lt;br /&gt;
23. for (A a:list) {&lt;br /&gt;
24. a.x();&lt;br /&gt;
25. a.y();;&lt;br /&gt;
26. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. The code runs with no output.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. Compilation fails because of an error in line 20.&lt;br /&gt;
D. Compilation fails because of an error in line 21.&lt;br /&gt;
E. Compilation fails because of an error in line 23.&lt;br /&gt;
F. Compilation fails because of an error in line 25.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: F&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 143&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. class SuperClass {&lt;br /&gt;
2. public A getA() {&lt;br /&gt;
3. return new A();&lt;br /&gt;
4. }&lt;br /&gt;
5. }&lt;br /&gt;
6. class SubClass extends SuperClass {&lt;br /&gt;
7. public B getA() {&lt;br /&gt;
8. return new B();&lt;br /&gt;
9. }&lt;br /&gt;
10. }&lt;br /&gt;
Which is true?&lt;br /&gt;
A. Compilation will succeed if A extends B.&lt;br /&gt;
B. Compilation will succeed if B extends A.&lt;br /&gt;
C. Compilation will always fail because of an error in line 7.&lt;br /&gt;
D. Compilation will always fail because of an error in line 8.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;strong&gt;Question 144&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. interface A { public void aMethod(); }&lt;br /&gt;
2. interface B { public void bMethod(); }&lt;br /&gt;
3. interface C extends A,B { public void cMethod(); }&lt;br /&gt;
4. class D implements B {&lt;br /&gt;
5. public void bMethod() { }&lt;br /&gt;
6. }&lt;br /&gt;
7. class E extends D implements C {&lt;br /&gt;
8. public void aMethod() { }&lt;br /&gt;
9. public void bMethod() { }&lt;br /&gt;
10. public void cMethod() { }&lt;br /&gt;
11. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails because of an error in line 3.&lt;br /&gt;
B. Compilation fails because of an error in line 7.&lt;br /&gt;
C. Compilation fails because of an error in line 9.&lt;br /&gt;
D. If you define D e = new E(), then e.bMethod() invokes the version&lt;br /&gt;
of bMethod() defined in Line 5.&lt;br /&gt;
E. If you define D e = (D)(new E()), then e.bMethod() invokes the&lt;br /&gt;
version of bMethod() defined in Line 5.&lt;br /&gt;
F. If you define D e = (D)(new E()), then e.bMethod() invokes the&lt;br /&gt;
version of bMethod() defined in Line 9.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: F&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 145&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
10. interface A { public int getValue() }&lt;br /&gt;
11. class B implements A {&lt;br /&gt;
12. public int getValue() { return 1; }&lt;br /&gt;
13. }&lt;br /&gt;
14. class C extends B {&lt;br /&gt;
15. // insert code here&lt;br /&gt;
16. }&lt;br /&gt;
Which three code fragments, inserted individually at line 15, make use&lt;br /&gt;
of polymorphism? (Choose three.)&lt;br /&gt;
A. public void add(C c) { c.getValue(); }&lt;br /&gt;
B. public void add(B b) { b.getValue(); }&lt;br /&gt;
C. public void add(A a) { a.getValue(); }&lt;br /&gt;
D. public void add(A a, B b) { a.getValue(); }&lt;br /&gt;
E. public void add(C c1, C c2) { c1.getValue(); }&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: BCD&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 146&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. class ClassA {&lt;br /&gt;
2. public int numberOfinstances;&lt;br /&gt;
3. protected ClassA(int numberOfinstances) {&lt;br /&gt;
4. this.numberOflnstances = numberOfinstances;&lt;br /&gt;
5. }&lt;br /&gt;
6. }&lt;br /&gt;
7. public class ExtendedA extends ClassA {&lt;br /&gt;
8. private ExtendedA(int numberOfinstances) {&lt;br /&gt;
9. super(numberOflnstances);&lt;br /&gt;
10. }&lt;br /&gt;
11. public static void main(String[] args) {&lt;br /&gt;
12. ExtendedA ext = new ExtendedA(420);&lt;br /&gt;
13. System.out.print(ext.numberOflnstances);&lt;br /&gt;
14. }&lt;br /&gt;
15. }&lt;br /&gt;
Which is true?&lt;br /&gt;
A. 420 is the output.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. All constructors must be declared public.&lt;br /&gt;
D. Constructors CANNOT use the private modifier.&lt;br /&gt;
E. Constructors CANNOT use the protected modifier.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 147&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
147. Given:&lt;br /&gt;
1. public class Base {&lt;br /&gt;
2. public static final String FOO = “foo”;&lt;br /&gt;
3. public static void main(String[] args) {&lt;br /&gt;
4. Base b = new Base();&lt;br /&gt;
5. Sub s = new Sub();&lt;br /&gt;
6. System.out.print(Base.FOO);&lt;br /&gt;
7. System.out.print(Sub.FOO);&lt;br /&gt;
8. System.out.print(b.FOO);&lt;br /&gt;
9. System.out.print(s.FOO);&lt;br /&gt;
10. System.out.print(((Base)s).FOO);&lt;br /&gt;
11. } }&lt;br /&gt;
12. class Sub extends Base {public static final String FOO=bar;}&lt;br /&gt;
What is the result?&lt;br /&gt;
A. foofoofoofoofoo&lt;br /&gt;
B. foobarfoobarbar&lt;br /&gt;
C. foobarfoofoofoo&lt;br /&gt;
D. foobarfoobarfoo&lt;br /&gt;
E. barbarbarbarbar&lt;br /&gt;
F. foofoofoobarbar&lt;br /&gt;
G. foofoofoobarfoo&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 148&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Which three statements are true? (Choose three.)&lt;br /&gt;
A. A final method in class X can be abstract if and only if X is abstract.&lt;br /&gt;
B. A protected method in class X can be overridden by any subclass of&lt;br /&gt;
X.&lt;br /&gt;
C. A private static method can be called only within other static&lt;br /&gt;
methods in class X.&lt;br /&gt;
D. A non-static public final method in class X can be overridden in any&lt;br /&gt;
subclass of X.&lt;br /&gt;
E. A public static method in class X can be called by a subclass of X&lt;br /&gt;
without explicitly referencing the class X.&lt;br /&gt;
F. A method with the same signature as a private final method in class&lt;br /&gt;
X can be implemented in a subclass of X.&lt;br /&gt;
G. A protected method in class X can be overridden by a subclass of A&lt;br /&gt;
only if the subclass is in the same package as X.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: BEF&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 149&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. class Pizza {&lt;br /&gt;
2. java.util.ArrayList toppings;&lt;br /&gt;
3. public final void addTopping(String topping) {&lt;br /&gt;
4. toppings.add(topping);&lt;br /&gt;
5. }&lt;br /&gt;
6. }&lt;br /&gt;
7. public class PepperoniPizza extends Pizza {&lt;br /&gt;
8. public void addTopping(String topping) {&lt;br /&gt;
9. System.out.println(”Cannot add Toppings”);&lt;br /&gt;
10. }&lt;br /&gt;
11. public static void main(String[] args) {&lt;br /&gt;
12. Pizza pizza = new PepperoniPizza();&lt;br /&gt;
13. pizza.addTopping(”Mushrooms”);&lt;br /&gt;
14. }&lt;br /&gt;
15. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. Cannot add Toppings&lt;br /&gt;
C. The code runs with no output.&lt;br /&gt;
D. A NullPointerException is thrown in Line 4.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 150&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. class Super {&lt;br /&gt;
2. private int a;&lt;br /&gt;
3. protected Super(int a) { this.a = a; }&lt;br /&gt;
4. }&lt;br /&gt;
.....&lt;br /&gt;
11. class Sub extends Super {&lt;br /&gt;
12. public Sub(int a) { super(a); }&lt;br /&gt;
13. public Sub() { this.a= 5; }&lt;br /&gt;
14. }&lt;br /&gt;
Which two, independently, will allow Sub to compile? (Choose two.)&lt;br /&gt;
A. Change line 2 to:&lt;br /&gt;
public int a;&lt;br /&gt;
B. Change line 2 to:&lt;br /&gt;
protected int a;&lt;br /&gt;
C. Change line 13 to:&lt;br /&gt;
public Sub() { this(5); }&lt;br /&gt;
D. Change line 13 to:&lt;br /&gt;
public Sub() { super(5); }&lt;br /&gt;
E. Change line 13 to:&lt;br /&gt;
public Sub() { super(a); }&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: CD&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/3772899608016959310/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-141-150.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/3772899608016959310'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/3772899608016959310'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-141-150.html' title='SCJP Questions 141-150'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-6185385624682551547</id><published>2011-12-14T17:50:00.001+05:30</published><updated>2011-12-14T17:50:45.882+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 131-14"/><title type='text'>SCJP Questions 131-14</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 131&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
1. class Computation extends Thread {&lt;br /&gt;
2.&lt;br /&gt;
3. private int num;&lt;br /&gt;
4. private boolean isComplete;&lt;br /&gt;
5. private int result;&lt;br /&gt;
6.&lt;br /&gt;
7. public Computation(int num) { this.num = num; }&lt;br /&gt;
8.&lt;br /&gt;
9. public synchronized void run() {&lt;br /&gt;
10. result = num * 2;&lt;br /&gt;
11. isComplete = true;&lt;br /&gt;
12. notify();&lt;br /&gt;
13. }&lt;br /&gt;
14.&lt;br /&gt;
15. public synchronized int getResult() {&lt;br /&gt;
16. while (!isComplete) {&lt;br /&gt;
17. try {&lt;br /&gt;
18. wait();&lt;br /&gt;
19. } catch (InterruptedException e) { }&lt;br /&gt;
20. }&lt;br /&gt;
21. return result;&lt;br /&gt;
22. }&lt;br /&gt;
23.&lt;br /&gt;
24. public static void main(String[] args) {&lt;br /&gt;
25. Computation[] computations = new Computation [4];&lt;br /&gt;
26. for (int i = 0; i &amp;lt; computations.length; i++) {&lt;br /&gt;
27. computations[i] = new Computation(i);&lt;br /&gt;
28. computations[i] .start();&lt;br /&gt;
29. }&lt;br /&gt;
30. for (Computation c : computations)&lt;br /&gt;
31. System.out.print(c.getResult() +“ “);&lt;br /&gt;
32. }&lt;br /&gt;
33. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. The code will deadlock.&lt;br /&gt;
B. The code may run with no output.&lt;br /&gt;
C. An exception is thrown at runtime.&lt;br /&gt;
D. The code may run with output “0 6”.&lt;br /&gt;
E. The code may run with output “2 0 6 4’.&lt;br /&gt;
F. The code may ruin with output “0 2 4 6”.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: F&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 132&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
7. void waitForSignal() {&lt;br /&gt;
8. Object obj = new Object();&lt;br /&gt;
9. synchronized (Thread.currentThread()) {&lt;br /&gt;
10. obj.wait();&lt;br /&gt;
11. obj.notify();;&lt;br /&gt;
12. }&lt;br /&gt;
13. }&lt;br /&gt;
Which is true?&lt;br /&gt;
A. This code may throw an InterruptedException.&lt;br /&gt;
B. This code may throw an IllegalStateException.&lt;br /&gt;
C. This code may throw a TimeoutException after ten minutes.&lt;br /&gt;
D. This code will not compile unless “obj.wait()” is replaced with&lt;br /&gt;
“((Thread) obj).wait()”.&lt;br /&gt;
E. Reversing the order of obj.wait() and obj.notify() may cause this&lt;br /&gt;
method to complete normally.&lt;br /&gt;
F. A call to notify() or notifyAll() from another thread may cause this&lt;br /&gt;
method to complete normally.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 133&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
foo and bar are public references available to many other threads. foo&lt;br /&gt;
refers to a Thread and bar is an Object. The thread foo is currently&lt;br /&gt;
executing bar.wait(). From another thread, which statement is the&lt;br /&gt;
most reliable way to ensue that foo will stop executing wait()?&lt;br /&gt;
A. foo.notify();&lt;br /&gt;
B. bar.notify();&lt;br /&gt;
C. foo.notifyAll();&lt;br /&gt;
D. Thread.notify();&lt;br /&gt;
E. bar.notiFYAll();&lt;br /&gt;
F. Object.notify();&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: E&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 134&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Which two are true? (Choose two.)&lt;br /&gt;
A. An encapsulated, public class promotes re-use.&lt;br /&gt;
B. Classes that share the same interface are always tightly&lt;br /&gt;
encapsulated.&lt;br /&gt;
C. An encapsulated class allows subclasses to overload methods, but&lt;br /&gt;
does NOT allow overriding methods.&lt;br /&gt;
D. An encapsulated class allows a programmer to change an&lt;br /&gt;
implementation without affecting outside code.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: AD&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 135&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. package test;&lt;br /&gt;
2.&lt;br /&gt;
3. class Target {&lt;br /&gt;
4. public String name = “hello”;&lt;br /&gt;
5. }&lt;br /&gt;
What can directly access and change the value of the variable name?&lt;br /&gt;
A. any class&lt;br /&gt;
B. only the Target class&lt;br /&gt;
C. any class in the test package&lt;br /&gt;
D. any class that extends Target&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 136&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class Target {&lt;br /&gt;
2. private int i = 0;&lt;br /&gt;
3. public int addOne() {&lt;br /&gt;
4. return ++i;&lt;br /&gt;
5. }&lt;br /&gt;
6. }&lt;br /&gt;
And:&lt;br /&gt;
1. public class Client {&lt;br /&gt;
2. public static void main(String[] args) {&lt;br /&gt;
3. System.out.println(new Target().addOne());&lt;br /&gt;
4. }&lt;br /&gt;
5. }&lt;br /&gt;
Which change can you make to Target without affecting Client?&lt;br /&gt;
A. Line 4 of class Target can be changed to return i++;&lt;br /&gt;
B. Line 2 of class Target can be changed to private int i = 1;&lt;br /&gt;
C. Line 3 of class Target can be changed to private int addOne() {&lt;br /&gt;
D. Line 2 of class Target can be changed to private Integer i = 0;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 137&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. package geometry;&lt;br /&gt;
2. public class Hypotenuse {&lt;br /&gt;
3. public InnerTriangle it = new InnerTriangle();&lt;br /&gt;
4. class InnerTriangle {&lt;br /&gt;
5. public int base;&lt;br /&gt;
6. public int height;&lt;br /&gt;
7. }&lt;br /&gt;
8. }&lt;br /&gt;
Which is true about the class of an object that can reference the&lt;br /&gt;
variable base?&lt;br /&gt;
A. It can be any class.&lt;br /&gt;
B. No class has access to base.&lt;br /&gt;
C. The class must belong to the geometry package.&lt;br /&gt;
D. The class must be a subclass of the class Hypotenuse.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 138&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. class ClassA {}&lt;br /&gt;
12. class ClassB extends ClassA {}&lt;br /&gt;
13. class ClassC extends ClassA {}&lt;br /&gt;
and:&lt;br /&gt;
21. ClassA p0 = new ClassA();&lt;br /&gt;
22. ClassB p1 = new ClassB();&lt;br /&gt;
23. ClassC p2 = new ClassC();&lt;br /&gt;
24. ClassA p3 = new ClassB();&lt;br /&gt;
25. ClassA p4 = new ClassC();&lt;br /&gt;
Which three are valid? (Choose three.)&lt;br /&gt;
A. p0 = p1;&lt;br /&gt;
B. p1 =p2;&lt;br /&gt;
C. p2 = p4;&lt;br /&gt;
D. p2 = (ClassC)p1;&lt;br /&gt;
E. p1 = (ClassB)p3;&lt;br /&gt;
F. p2 = (ClassC)p4;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: AEF&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 139&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. class Animal { public String noise() { return “peep”; } }&lt;br /&gt;
12. class Dog extends Animal {&lt;br /&gt;
13. public String noise() { return “bark”; }&lt;br /&gt;
14. }&lt;br /&gt;
15. class Cat extends Animal {&lt;br /&gt;
16. public String noise() { return “meow”; }&lt;br /&gt;
17. }&lt;br /&gt;
.....&lt;br /&gt;
30. Animal animal = new Dog();&lt;br /&gt;
31. Cat cat = (Cat)animal;&lt;br /&gt;
32. System.out.printIn(cat.noise());&lt;br /&gt;
What is the result?&lt;br /&gt;
A. peep&lt;br /&gt;
B. bark&lt;br /&gt;
C. meow&lt;br /&gt;
D. Compilation fails.&lt;br /&gt;
E. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: E&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 140&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. abstract class Vehicle { public int speed() { return 0; } }&lt;br /&gt;
12. class Car extends Vehicle { public int speed() { return 60; } }&lt;br /&gt;
13. class RaceCar extends Car { public int speed() { return 150; }}&lt;br /&gt;
......&lt;br /&gt;
21. RaceCar racer = new RaceCar();&lt;br /&gt;
22. Car car = new RaceCar();&lt;br /&gt;
23. Vehicle vehicle = new RaceCar();&lt;br /&gt;
24. System.out.println(racer.speed() + “, ‘ + car.speed()&lt;br /&gt;
25. + “, “+ vehicle.speed());&lt;br /&gt;
What is the result?&lt;br /&gt;
A. 0, 0,0&lt;br /&gt;
B. 150, 60, 0&lt;br /&gt;
C. Compilation fails.&lt;br /&gt;
D. 150, 150, 150&lt;br /&gt;
E. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/6185385624682551547/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-131-14.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/6185385624682551547'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/6185385624682551547'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-131-14.html' title='SCJP Questions 131-14'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-8875239167962779911</id><published>2011-12-14T17:47:00.001+05:30</published><updated>2011-12-14T17:47:26.650+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 121-130"/><title type='text'>SCJP Questions 121-130</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 121&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class TestOne {&lt;br /&gt;
2. public static void main (String[] args) throws Exception {&lt;br /&gt;
3. Thread.sleep(3000);&lt;br /&gt;
4. System.out.println(”sleep”);&lt;br /&gt;
5. }&lt;br /&gt;
6. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. The code executes normally and prints “sleep”.&lt;br /&gt;
D. The code executes normally, but nothing is printed.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 122&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class TestOne implements Runnable {&lt;br /&gt;
2. public static void main (String[] args) throws Exception {&lt;br /&gt;
3. Thread t = new Thread(new TestOne());&lt;br /&gt;
4. t.start();&lt;br /&gt;
5. System.out.print(”Started”);&lt;br /&gt;
6. t.join();&lt;br /&gt;
7. System.out.print(”Complete”);&lt;br /&gt;
8. }&lt;br /&gt;
9. public void run() {&lt;br /&gt;
10. for (int i= 0; i&amp;lt; 4; i++) {&lt;br /&gt;
11. System.out.print(i);&lt;br /&gt;
12. }&lt;br /&gt;
13. }&lt;br /&gt;
14. }&lt;br /&gt;
What can be a result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. The code executes and prints “StartedComplete”.&lt;br /&gt;
D. The code executes and prints “StartedComplete0123”.&lt;br /&gt;
E. The code executes and prints “Started0l23Complete”.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: E&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 123&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
Given:&lt;br /&gt;
1. public class TwoThreads {&lt;br /&gt;
2&lt;br /&gt;
3. private static Object resource = new Object();&lt;br /&gt;
4.&lt;br /&gt;
5. private static void delay(long n) {&lt;br /&gt;
6. try { Thread.sleep(n); }&lt;br /&gt;
7. catch (Exception e) { System.out.print(”Error “); }&lt;br /&gt;
8. }&lt;br /&gt;
9&lt;br /&gt;
10. public static void main(String[] args) {&lt;br /&gt;
11. System.out.print(”StartMain “);&lt;br /&gt;
12. new Thread1().start();&lt;br /&gt;
13. delay(1000);&lt;br /&gt;
14. Thread t2 = new Thread2();&lt;br /&gt;
15. t2.start();&lt;br /&gt;
16. delay(1000);&lt;br /&gt;
17. t2.interrupt&lt;br /&gt;
18. delay(1000);&lt;br /&gt;
19. System.out.print(”EndMain “);&lt;br /&gt;
20. }&lt;br /&gt;
21.&lt;br /&gt;
22. static class Thread 1 extends Thread {&lt;br /&gt;
23. public void run() {&lt;br /&gt;
24. synchronized (resource) {&lt;br /&gt;
25. System.out.print(”Startl “);&lt;br /&gt;
26. delay(6000);&lt;br /&gt;
27. System.out.print(”End1 “);&lt;br /&gt;
28. }&lt;br /&gt;
29. }&lt;br /&gt;
30. }&lt;br /&gt;
31.&lt;br /&gt;
32. static class Thread2 extends Thread {&lt;br /&gt;
33. public void run() {&lt;br /&gt;
34. synchronized (resource) {&lt;br /&gt;
35. System.out.print(”Start2 “);&lt;br /&gt;
36. delay(2000);&lt;br /&gt;
37. System.out.print(”End2 “);&lt;br /&gt;
38. }&lt;br /&gt;
39. }&lt;br /&gt;
40. }&lt;br /&gt;
41. }&lt;br /&gt;
Assume that sleep(n) executes in exactly m milliseconds, and all other&lt;br /&gt;
code executes in an insignificant amount of time. What is the output if&lt;br /&gt;
the main() method is run?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. Deadlock occurs.&lt;br /&gt;
C. StartMain Start1 Error EndMain End1&lt;br /&gt;
D. StartMain Start1 EndMain End1 Start2 End2&lt;br /&gt;
E. StartMain Start1 Error Start2 EndMain End2 End1&lt;br /&gt;
F. StartMain Start1 Start2 Error End2 EndMain End1&lt;br /&gt;
G. StartMain Start1 EndMain End1 Start2 Error End2&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: G&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 124&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
public class NamedCounter {&lt;br /&gt;
private final String name;&lt;br /&gt;
private int count;&lt;br /&gt;
public NamedCounter(String name) { this.name = name; }&lt;br /&gt;
public String getName() { return name; }&lt;br /&gt;
public void increment() { coount++; }&lt;br /&gt;
public int getCount() { return count; }&lt;br /&gt;
public void reset() { count = 0; }&lt;br /&gt;
}&lt;br /&gt;
Which three changes should be made to adapt this class to be used&lt;br /&gt;
safely by multiple threads? (Choose three.)&lt;br /&gt;
A. declare reset() using the synchronized keyword&lt;br /&gt;
B. declare getName() using the synchronized keyword&lt;br /&gt;
C. declare getCount() using the synchronized keyword&lt;br /&gt;
D. declare the constructor using the synchronized keyword&lt;br /&gt;
E. declare increment() using the synchronized keyword&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: ACE&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 125&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button:&lt;br /&gt;
1. public class Threads 1 {&lt;br /&gt;
2. intx=0;&lt;br /&gt;
3. public class Runner implements Runnable {&lt;br /&gt;
4. public void run() {&lt;br /&gt;
5. int current = 0;&lt;br /&gt;
6. for(int=i=0;i&amp;lt;4;i++){&lt;br /&gt;
7. current = x;&lt;br /&gt;
8. System.out.print(current + “, “);&lt;br /&gt;
9. x = current + 2;&lt;br /&gt;
10. }&lt;br /&gt;
11. }&lt;br /&gt;
12. }&lt;br /&gt;
13.&lt;br /&gt;
14. public static void main(String[] args) {&lt;br /&gt;
15. new Threads1().go();&lt;br /&gt;
16. }&lt;br /&gt;
17.&lt;br /&gt;
18. public void go() {&lt;br /&gt;
19. Runnable r1 = new Runner();&lt;br /&gt;
20. new Thread(r1).start();&lt;br /&gt;
21. new Thread(r1 ).start();&lt;br /&gt;
22. }&lt;br /&gt;
23. }&lt;br /&gt;
Which two are possible results? (Choose two.)&lt;br /&gt;
A. 0, 2, 4, 4, 6, 8, 10, 6,&lt;br /&gt;
B. 0, 2, 4, 6, 8, 10, 2, 4,&lt;br /&gt;
C. 0, 2, 4, 6, 8, 10, 12, 14,&lt;br /&gt;
D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,&lt;br /&gt;
E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: AC&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 126&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
1. import java.util.*;&lt;br /&gt;
2.&lt;br /&gt;
3. public class NameList {&lt;br /&gt;
4. private List names = new ArrayList();&lt;br /&gt;
5. public synchronized void add(String name) { names.add(name); }&lt;br /&gt;
6. public synchronized void printAll() {&lt;br /&gt;
7. for (int i = 0; i &amp;lt;names.size(); i++) {&lt;br /&gt;
8. System.out.print(names.get(i) +“ “);&lt;br /&gt;
9. }&lt;br /&gt;
10. }&lt;br /&gt;
11. public static void main(String[] args) {&lt;br /&gt;
12. final NameList sl = new NameList();&lt;br /&gt;
13.for(int i=0;i&amp;lt;2;i++) {&lt;br /&gt;
14. new Thread() {&lt;br /&gt;
15. public void ruin() {&lt;br /&gt;
16. sl.add(”A”);&lt;br /&gt;
17. sl.add(”B”);&lt;br /&gt;
18. sl.add(”C”);&lt;br /&gt;
19. sl.printAll();&lt;br /&gt;
20. }&lt;br /&gt;
21. }.start();&lt;br /&gt;
22. }&lt;br /&gt;
23. }&lt;br /&gt;
24. }&lt;br /&gt;
Which two statements are true if this class is compiled and run?&lt;br /&gt;
(Choose two.)&lt;br /&gt;
A. An exception may be thrown at runtime.&lt;br /&gt;
B. The code may run with no output, without exiting.&lt;br /&gt;
C. The code may run with no output, exiting normally.&lt;br /&gt;
D. The code may rum with output “A B A B C C “, then exit.&lt;br /&gt;
E. The code may rum with output “A B C A B C A B C “, then exit.&lt;br /&gt;
F. The code may ruin with output “A A A B C A B C C “, then exit.&lt;br /&gt;
G. The code may ruin with output “A B C A A B C A B C “, then exit.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: EG&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 127&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class TestFive {&lt;br /&gt;
2. private int x;&lt;br /&gt;
3. public void foo() {&lt;br /&gt;
4 int current = x;&lt;br /&gt;
5. x = current + 1;&lt;br /&gt;
6. }&lt;br /&gt;
7. public void go() {&lt;br /&gt;
8. for(int i=0;i&amp;lt;5;i++) {&lt;br /&gt;
9. new Thread() {&lt;br /&gt;
10. public void run() {&lt;br /&gt;
11. foo();&lt;br /&gt;
12. System.out.print(x + “, “);&lt;br /&gt;
13. } }.start();&lt;br /&gt;
14. }}}&lt;br /&gt;
Which two changes, taken together, would guarantee the output: 1, 2,&lt;br /&gt;
3, 4, 5, ? (Choose two.)&lt;br /&gt;
A. Move the line 12 print statement into the foo() method.&lt;br /&gt;
B. Change line 7 to public synchronized void go() {.&lt;br /&gt;
C. Change the variable declaration on line 3 to private volatile int x;.&lt;br /&gt;
D. Wrap the code inside the foo() method with a synchronized( this )&lt;br /&gt;
block.&lt;br /&gt;
E. Wrap the for loop code inside the go() method with a synchronized&lt;br /&gt;
block synchronized(this) { // for loop code here }.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: AD&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 128&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Which three will compile and rim without exception? (Choose three.)&lt;br /&gt;
A. private synchronized Object o;&lt;br /&gt;
B. void go() {&lt;br /&gt;
synchronized() { /* code here */ }&lt;br /&gt;
}&lt;br /&gt;
C. public synchronized void go() { /* code here */ }&lt;br /&gt;
D. private synchronized(this) void go() { /* code here */ }&lt;br /&gt;
E. void go() {&lt;br /&gt;
synchronized(Object.class) { /* code here */ }&lt;br /&gt;
}&lt;br /&gt;
F. void go() {&lt;br /&gt;
Object o = new Object();&lt;br /&gt;
synchronized(o) { /* code here */ }&lt;br /&gt;
}&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: CEF&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 129&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class TestSeven extends Thread {&lt;br /&gt;
2. private static int x;&lt;br /&gt;
3. public synchronized void doThings() {&lt;br /&gt;
4. int current = x;&lt;br /&gt;
5. current++;&lt;br /&gt;
6. x = current;&lt;br /&gt;
7. }&lt;br /&gt;
8. public void run() {&lt;br /&gt;
9. doThings();&lt;br /&gt;
10. }&lt;br /&gt;
1 1.}&lt;br /&gt;
Which is true?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. Synchronizing the run() method would make the class thread-safe.&lt;br /&gt;
D. The data in variable “x” are protected from concurrent access&lt;br /&gt;
problems.&lt;br /&gt;
E. Declaring the doThings() method as static would make the class&lt;br /&gt;
thread-safe.&lt;br /&gt;
F. Wrapping the statements within doThings() in a synchronized(new&lt;br /&gt;
Object()) { } block would make the class thread-safe.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: E&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 130&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
10. public class Transfers {&lt;br /&gt;
11. public static void main(String[] args) throws Exception {&lt;br /&gt;
12. Record r1 = new Record();&lt;br /&gt;
13. Record r2 = new Record();&lt;br /&gt;
14. doTransfer(r1, r2, 5);&lt;br /&gt;
15. doTransfer(r2, r1, 2);&lt;br /&gt;
16. doTransfer(r1, r2, 1);&lt;br /&gt;
17. // print the result&lt;br /&gt;
18. System.out.println(”rl = “ + r1.get() +“, r2=” + r2.get());&lt;br /&gt;
19. }&lt;br /&gt;
20. private static void doTransfer(&lt;br /&gt;
21. final Record a, final Record b, final int amount) {&lt;br /&gt;
22. Thread t = new Thread() {&lt;br /&gt;
23. public void run() {&lt;br /&gt;
24. new Clerk().transfer(a, b, amount);&lt;br /&gt;
25. }&lt;br /&gt;
26. };&lt;br /&gt;
27. t.start();&lt;br /&gt;
28. }&lt;br /&gt;
29. }&lt;br /&gt;
30. class Clerk {&lt;br /&gt;
31. public synchronized void transfer(Record a, Record b, int amount){&lt;br /&gt;
32. synchronized (a) {&lt;br /&gt;
33. synchronized (b) {&lt;br /&gt;
34. a.add(-amount);&lt;br /&gt;
35. b.add(amount);&lt;br /&gt;
36. }&lt;br /&gt;
37. }&lt;br /&gt;
38. }&lt;br /&gt;
39. }&lt;br /&gt;
40. class Record {&lt;br /&gt;
41.int num=10;&lt;br /&gt;
42. public int get() { return num; }&lt;br /&gt;
43. public void add(int n) { num = num + n; }&lt;br /&gt;
44. }&lt;br /&gt;
If Transfers.main() is run, which three are true? (Choose three.)&lt;br /&gt;
A. The output may be “r1 = 6, r2 = 14”.&lt;br /&gt;
B. The output may be “r1 = 5, r2 = 15”.&lt;br /&gt;
C. The output may be “r1 = 8, r2 = 12”.&lt;br /&gt;
D. The code may run (and complete) with no output.&lt;br /&gt;
E. The code may deadlock (without completing) with no output.&lt;br /&gt;
F. M IllegalStateException or InterruptedException may be thrown at&lt;br /&gt;
runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: ABE&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/8875239167962779911/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-121-130.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/8875239167962779911'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/8875239167962779911'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-121-130.html' title='SCJP Questions 121-130'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-791332280741929241</id><published>2011-12-14T17:46:00.003+05:30</published><updated>2011-12-14T17:46:45.650+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 111-120"/><title type='text'>SCJP Questions 111-120</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 111&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. String test = “Test A. Test B. Test C.”;&lt;br /&gt;
12. // insert code here&lt;br /&gt;
13. String[] result = test.split(regex);&lt;br /&gt;
Which regular expression inserted at line 12 will correctly split test into&lt;br /&gt;
“Test A,” “Test B,” and “Test C”?&lt;br /&gt;
A. String regex = “”;&lt;br /&gt;
B. String regex = “ “;&lt;br /&gt;
C. String regex = “.*“.&lt;br /&gt;
D. String regex = “\\s”&lt;br /&gt;
E. String regex = “\\.\\s*”;&lt;br /&gt;
F. String regex = “\\w[ \.] +“;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: E&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 112&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
12. System.out.format(”Pi is approximately %d.”, Math.PI);&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. Pi is approximately 3.&lt;br /&gt;
C. Pi is approximately 3.141593.&lt;br /&gt;
D. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 113&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
12. String csv = “Sue,5,true,3”;&lt;br /&gt;
13. Scanner scanner = new Scanner( csv);&lt;br /&gt;
14. scanner.useDelimiter(”,”);&lt;br /&gt;
15. int age = scanner.nextInt();&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. After line 15, the value of age is 5.&lt;br /&gt;
C. After line 15, the value of age is 3.&lt;br /&gt;
D. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 114&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Which two code fragments will execute the method doStuff() in a&lt;br /&gt;
separate thread? (Choose two.)&lt;br /&gt;
A. new Thread() {&lt;br /&gt;
public void run() { doStuff(); }&lt;br /&gt;
}&lt;br /&gt;
B. new Thread() {&lt;br /&gt;
public void start() { doStuff(); }&lt;br /&gt;
}&lt;br /&gt;
C. new Thread() {&lt;br /&gt;
public void start() { doStuff(); }&lt;br /&gt;
} .run();&lt;br /&gt;
D. new Thread() {&lt;br /&gt;
public void run() { doStuff(); }&lt;br /&gt;
} .start();&lt;br /&gt;
E. new Thread(new Runnable() {&lt;br /&gt;
public void run() { doStuff(); }&lt;br /&gt;
} ).run();&lt;br /&gt;
F. new Thread(new Runnable() {&lt;br /&gt;
public void run() { doStuff(); }&lt;br /&gt;
}).start();&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: DF&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 115&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class Threads3 implements Runnable {&lt;br /&gt;
2. public void run() {&lt;br /&gt;
3. System.out.print(”running”);&lt;br /&gt;
4. }&lt;br /&gt;
5. public static void main(String[] args) {&lt;br /&gt;
6. Thread t = new Thread(new Threads3());&lt;br /&gt;
7. t.run();&lt;br /&gt;
8. t.run();&lt;br /&gt;
9. t.start();&lt;br /&gt;
10. }&lt;br /&gt;
11. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. The code executes and prints “running”.&lt;br /&gt;
D. The code executes and prints “runningrunning”.&lt;br /&gt;
E. The code executes and prints “runningrunningrunning”.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: E&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 116&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class Threads4 {&lt;br /&gt;
2. public static void main (String[] args) {&lt;br /&gt;
3. new Threads4().go();&lt;br /&gt;
4. }&lt;br /&gt;
5. public void go() {&lt;br /&gt;
6. Runnable r = new Runnable() {&lt;br /&gt;
7. public void run() {&lt;br /&gt;
8. System.out.print(”foo”);&lt;br /&gt;
9. }&lt;br /&gt;
10. };&lt;br /&gt;
11. Thread t = new Thread(r);&lt;br /&gt;
12. t.start();&lt;br /&gt;
13. t.start();&lt;br /&gt;
14. }&lt;br /&gt;
15. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. The code executes normally and prints ‘foo”.&lt;br /&gt;
D. The code executes normally, but nothing is printed.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 117&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class Threads5 {&lt;br /&gt;
2. public static void main (String[] args) {&lt;br /&gt;
3. new Thread(new Runnable() {&lt;br /&gt;
4. public void run() {&lt;br /&gt;
5. System.out.print(”bar”);&lt;br /&gt;
6. }}).start();&lt;br /&gt;
7. }&lt;br /&gt;
8. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. The code executes normally and prints “bar”.&lt;br /&gt;
D. The code executes normally, but nothing prints.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 118&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. Runnable r = new Runnable() {&lt;br /&gt;
12. public void run() {&lt;br /&gt;
13. System.out.print(”Cat”);&lt;br /&gt;
14. }&lt;br /&gt;
15. };&lt;br /&gt;
16. Threadt=new Thread(r) {&lt;br /&gt;
17. public void run() {&lt;br /&gt;
18. System.out.print(”Dog”);&lt;br /&gt;
19. }&lt;br /&gt;
20. };&lt;br /&gt;
21. t.start();&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Cat&lt;br /&gt;
B. Dog&lt;br /&gt;
C. Compilation fails.&lt;br /&gt;
D. The code runs with no output.&lt;br /&gt;
E. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 119&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
Given:&lt;br /&gt;
10. public class Starter extends Thread {&lt;br /&gt;
11. private int x= 2;&lt;br /&gt;
12. public static void main(String[] args) throws Exception {&lt;br /&gt;
13. new Starter().makeItSo();&lt;br /&gt;
14. }&lt;br /&gt;
15. public Starter() {&lt;br /&gt;
16. x=5;&lt;br /&gt;
17. start();&lt;br /&gt;
18. }&lt;br /&gt;
19. public void makeItSo() throws Exception {&lt;br /&gt;
20. join();&lt;br /&gt;
21. x=x- 1;&lt;br /&gt;
22. System.out.println(x);&lt;br /&gt;
23. }&lt;br /&gt;
24. public void run() { x *= 2; }&lt;br /&gt;
25. }&lt;br /&gt;
What is the output if the main() method is rum?&lt;br /&gt;
A. 4&lt;br /&gt;
B. 5&lt;br /&gt;
C. 8&lt;br /&gt;
D. 9&lt;br /&gt;
E. Compilation fails.&lt;br /&gt;
F. An exception is thrown at runtime.&lt;br /&gt;
G. It is impossible to determine for certain.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 120&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class Threads2 implements Runnable {&lt;br /&gt;
2.&lt;br /&gt;
3. public void nun() {&lt;br /&gt;
4. System.out.println(”run.”);&lt;br /&gt;
5. throw new RuntimeException(”Problem”);&lt;br /&gt;
6. }&lt;br /&gt;
7. public static void main(String[] args) {&lt;br /&gt;
8. Thread t = new Thread(new Threads2());&lt;br /&gt;
9. t.start();&lt;br /&gt;
10. System.out.println(”End of method.”);&lt;br /&gt;
11. }&lt;br /&gt;
12. }&lt;br /&gt;
Which two can be results? (Choose two.)&lt;br /&gt;
A. java.lang.RuntimeException: Problem&lt;br /&gt;
B. run.&lt;br /&gt;
java.lang.RuntimeException: Problem&lt;br /&gt;
C. End of method.&lt;br /&gt;
java.lang.RuntimeException: Problem&lt;br /&gt;
D. End of method.&lt;br /&gt;
run.&lt;br /&gt;
java.lang.RuntimeException: Problem&lt;br /&gt;
E. run.&lt;br /&gt;
java.lang.RuntimeException: Problem&lt;br /&gt;
End of method.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: DE&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/791332280741929241/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-111-120.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/791332280741929241'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/791332280741929241'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-111-120.html' title='SCJP Questions 111-120'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-9042847358945033593</id><published>2011-12-14T17:46:00.001+05:30</published><updated>2011-12-14T17:46:11.043+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 101-110"/><title type='text'>SCJP Questions 101-110</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 100&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Assuming that the serializeBanana() and the deserializeBanana()&lt;br /&gt;
methods will correctly use Java serialization and given:&lt;br /&gt;
13. import java.io.*;&lt;br /&gt;
14. class Food implemertts Serializable {int good = 3;}&lt;br /&gt;
15. class Fruit externds Food {int juice = 5;}&lt;br /&gt;
16. public class Banana extends Fruit {&lt;br /&gt;
17. int yellow = 4;&lt;br /&gt;
18. public static void main(String [] args) {&lt;br /&gt;
19. Banana b = new Banana(); Banana b2 = new Banana();&lt;br /&gt;
20. b.serializeBanana(b); // assume correct serialization&lt;br /&gt;
21. b2 = b.deserializeBanana(); // assume correct&lt;br /&gt;
22. System.out.println(”restore “+b2.yellow+ b2.juice+b2.good);&lt;br /&gt;
24. }&lt;br /&gt;
25. // more Banana methods go here&lt;br /&gt;
50. }&lt;br /&gt;
‘What is the result?&lt;br /&gt;
A. restore 400&lt;br /&gt;
B. restore 403&lt;br /&gt;
C. restore 453&lt;br /&gt;
D. Compilation fails.&lt;br /&gt;
E. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 101&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Assuming that the serializeBanana2() and the deserializeBanana2()&lt;br /&gt;
methods will correctly use Java serialization and given:&lt;br /&gt;
13. import java.io.*;&lt;br /&gt;
14. class Food {Food() { System.out.print(”1”); } }&lt;br /&gt;
15. class Fruit extends Food implements Serializable {&lt;br /&gt;
16. Fruit() { System.out.print(”2”); } }&lt;br /&gt;
17. public class Banana2 extends Fruit { int size = 42;&lt;br /&gt;
18. public static void main(String [] args) {&lt;br /&gt;
19. Banana2 b = new Banana2();&lt;br /&gt;
20. b.serializeBanana2(b); // assume correct serialization&lt;br /&gt;
21. b = b.deserializeBanana2(b); // assume correct&lt;br /&gt;
22. System.out.println(” restored “+ b.size + “ “); }&lt;br /&gt;
23. // more Banana2 methods&lt;br /&gt;
24. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. 1 restored 42&lt;br /&gt;
C. 12 restored 42&lt;br /&gt;
D. 121 restored 42&lt;br /&gt;
E. 1212 restored 42&lt;br /&gt;
F. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 102&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
10. public class Foo implements java.io.Serializable {&lt;br /&gt;
11. private int x;&lt;br /&gt;
12. public int getX() { return x; }&lt;br /&gt;
12.publicFoo(int x){this.x=x; }&lt;br /&gt;
13. private void writeObject( ObjectOutputStream s)&lt;br /&gt;
14. throws IOException {&lt;br /&gt;
15. // insert code here&lt;br /&gt;
16. }&lt;br /&gt;
17. }&lt;br /&gt;
Which code fragment, inserted at line 15, will allow Foo objects to be&lt;br /&gt;
correctly serialized and deserialized?&lt;br /&gt;
A. s.writeInt(x);&lt;br /&gt;
B. s.serialize(x);&lt;br /&gt;
C. s.writeObject(x);&lt;br /&gt;
D. s.defaultWriteObject();&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 103&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
12. NumberFormat nf= NumberFormat.getInstance();&lt;br /&gt;
13. nf.setMaximumFractionDigits(4);&lt;br /&gt;
14. nf.setMinimumFractionDigits(2);&lt;br /&gt;
15. String a = nf.format(3.1415926);&lt;br /&gt;
16. String b = nf.format(2);&lt;br /&gt;
Which two are true about the result if the default locale is Locale.US?&lt;br /&gt;
(Choose two.)&lt;br /&gt;
A. The value of b is 2.&lt;br /&gt;
B. The value of a is 3.14.&lt;br /&gt;
C. The value of b is 2.00.&lt;br /&gt;
D. The value of a is 3.141.&lt;br /&gt;
E. The value of a is 3.1415.&lt;br /&gt;
F. The value of a is 3.1416.&lt;br /&gt;
G. The value of b is 2.0000.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: CF&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 104&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. double input = 314159.26;&lt;br /&gt;
12. NumberFormat nf= NumberFormat.getInstance(Locale.ITALIAN);&lt;br /&gt;
13. String b;&lt;br /&gt;
14. //insert code here&lt;br /&gt;
Which code, inserted at line 14, sets the value of b to 3 14.159,26?&lt;br /&gt;
A. b = nf.parse( input);&lt;br /&gt;
B. b = nf.format( input);&lt;br /&gt;
C. b = nf.equals( input);&lt;br /&gt;
D. b = nf.parseObject( input);&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 105&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
14. DateFormat df;&lt;br /&gt;
15. Date date = new Date();&lt;br /&gt;
16. //insert code here&lt;br /&gt;
17. String s = df.format( date);&lt;br /&gt;
Which two, inserted independently at line 16, allow the code to&lt;br /&gt;
compile? (Choose two.)&lt;br /&gt;
A. df= new DateFormat();&lt;br /&gt;
B. df= Date.getFormatter();&lt;br /&gt;
C. df= date.getFormatter();&lt;br /&gt;
D. df= date.getDateFormatter();&lt;br /&gt;
E. df= Date.getDateFormatter();&lt;br /&gt;
F. df= DateFormat.getInstance();&lt;br /&gt;
G. df = DateFormat.getDateInstance();&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: FG&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 106&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
12. Date date = new Date();&lt;br /&gt;
13. df.setLocale(Locale.ITALY);&lt;br /&gt;
14. String s = df.format(date);&lt;br /&gt;
The variable df is an object of type DateFormat that has been&lt;br /&gt;
initialized in line 11. What is the result if this code is run on December&lt;br /&gt;
14, 2000?&lt;br /&gt;
A. The value of s is 14-dic-2004.&lt;br /&gt;
B. The value of s is Dec 14, 2000.&lt;br /&gt;
C. An exception is thrown at runtime.&lt;br /&gt;
D. Compilation fails because of an error in line 13.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 107&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
33. Date d = new Date(0);&lt;br /&gt;
34. String ds = “December 15, 2004”;&lt;br /&gt;
35. // insert code here&lt;br /&gt;
36. try {&lt;br /&gt;
37. d = df.parse(ds);&lt;br /&gt;
38. }&lt;br /&gt;
39. catch(ParseException e) {&lt;br /&gt;
40. System.out.println(”Unable to parse “+ ds);&lt;br /&gt;
41. }&lt;br /&gt;
42. // insert code here too&lt;br /&gt;
Which will create the appropriate DateFormat object and add a day to&lt;br /&gt;
the Date object?&lt;br /&gt;
A. 35. DateFormat df= DateFormat.getDateFormat();&lt;br /&gt;
42. d.setTime( (60 * 60 * 24) + d.getTime());&lt;br /&gt;
B. 35. DateFormat df= DateFormat.getDateJnstance();&lt;br /&gt;
42. d.setTime( (1000 * 60 * 60 * 24) + d.getTime());&lt;br /&gt;
C. 35. DateFormat df= DateFormat.getDateFormat();&lt;br /&gt;
42. d.setLocalTime( (1000*60*60*24) + d.getLocalTime());&lt;br /&gt;
D. 35. DateFormat df= DateFormat.getDateJnstance();&lt;br /&gt;
42. d.setLocalTime( (60 * 60 * 24) + d.getLocalTime());&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 108&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given a valid DateFormat object named df, and&lt;br /&gt;
16. Date d = new Date(0L);&lt;br /&gt;
17. String ds = “December 15, 2004”;&lt;br /&gt;
18. // insert code here&lt;br /&gt;
What updates d’s value with the date represented by ds?&lt;br /&gt;
A. 18. d = df.parse(ds);&lt;br /&gt;
B. 18. d = df.getDate(ds);&lt;br /&gt;
C. 18. try {&lt;br /&gt;
19. d = df.parse(ds);&lt;br /&gt;
20. } catch(ParseException e) { };&lt;br /&gt;
D. 18. try {&lt;br /&gt;
19. d = df.getDate(ds);&lt;br /&gt;
20. } catch(ParseException e) { };&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 109&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. String test = “This is a test”;&lt;br /&gt;
12. String[] tokens = test.split(”\s”);&lt;br /&gt;
13. System.out.println(tokens.length);&lt;br /&gt;
What is the result?&lt;br /&gt;
A. 0&lt;br /&gt;
B. 1&lt;br /&gt;
C. 4&lt;br /&gt;
D. Compilation fails.&lt;br /&gt;
E. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 110&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. String test= “a1b2c3”;&lt;br /&gt;
12. String[] tokens = test.split(”\\d”);&lt;br /&gt;
13. for(String s: tokens) System.out.print(s +“ “);&lt;br /&gt;
What is the result?&lt;br /&gt;
A. a b c&lt;br /&gt;
B. 1 2 3&lt;br /&gt;
C. a1b2c3&lt;br /&gt;
D. a1 b2 c3&lt;br /&gt;
E. Compilation fails.&lt;br /&gt;
F. The code runs with no output.&lt;br /&gt;
G. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/9042847358945033593/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-101-110.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/9042847358945033593'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/9042847358945033593'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-101-110.html' title='SCJP Questions 101-110'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-7159599344392412712</id><published>2011-12-14T17:45:00.001+05:30</published><updated>2011-12-14T17:45:36.629+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 91-100"/><title type='text'>SCJP Questions 91-100</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 91&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public String makinStrings() {&lt;br /&gt;
12. String s = “Fred”;&lt;br /&gt;
13. s = s + “47”;&lt;br /&gt;
14. s = s.substring(2, 5);&lt;br /&gt;
15. s = s.toUpperCase();&lt;br /&gt;
16. return s.toString();&lt;br /&gt;
17. }&lt;br /&gt;
How many String objects will be created when this method is invoked?&lt;br /&gt;
A. 1&lt;br /&gt;
B. 2&lt;br /&gt;
C. 3&lt;br /&gt;
D. 4&lt;br /&gt;
E. 5&lt;br /&gt;
F. 6&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 92&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class TestString3 {&lt;br /&gt;
2. public static void main(String[] args) {&lt;br /&gt;
3. // insert code here&lt;br /&gt;
5. System.out.println(s);&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
Which two code fragments, inserted independently at line 3, generate&lt;br /&gt;
the output 4247? (Choose two.)&lt;br /&gt;
A. String s = “123456789”;&lt;br /&gt;
s = (s-”123”).replace(1,3,”24”) - “89”;&lt;br /&gt;
B. StringBuffer s = new StringBuffer(”123456789”);&lt;br /&gt;
s.delete(0,3).replace( 1,3, “24”).delete(4,6);&lt;br /&gt;
C. StringBuffer s = new StringBuffer(”123456789”);&lt;br /&gt;
s.substring(3,6).delete( 1 ,3).insert( 1, “24”);&lt;br /&gt;
D. StringBuilder s = new StringBuilder(”123456789”);&lt;br /&gt;
s.substring(3,6).delete( 1 ,2).insert( 1, “24”);&lt;br /&gt;
E. StringBuilder s = new StringBuilder(”123456789”);&lt;br /&gt;
s.delete(0,3).delete( 1 ,3).delete(2,5).insert( 1, “24”);&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: BE&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 93&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public class Yikes {&lt;br /&gt;
12.&lt;br /&gt;
13. public static void go(Long n) {System.out.println(”Long “);}&lt;br /&gt;
14. public static void go(Short n) {System.out.println(”Short “);}&lt;br /&gt;
15. public static void go(int n) {System.out.println(”int “);}&lt;br /&gt;
16. public static void main(String [] args) {&lt;br /&gt;
17. short y= 6;&lt;br /&gt;
18. long z= 7;&lt;br /&gt;
19. go(y);&lt;br /&gt;
20. go(z);&lt;br /&gt;
21. }&lt;br /&gt;
22. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. int Long&lt;br /&gt;
B. Short Long&lt;br /&gt;
C. Compilation fails.&lt;br /&gt;
D. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 94&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
12. public class Wow {&lt;br /&gt;
13. public static void go(short n) {System.out.println(”short”); }&lt;br /&gt;
14. public static void go(Short n) {System.out.println(”SHORT”);}&lt;br /&gt;
15. public static void go(Long n) {System.out.println(” LONG”); }&lt;br /&gt;
16. public static void main(String [] args) {&lt;br /&gt;
17. Short y= 6;&lt;br /&gt;
18.int z=7;&lt;br /&gt;
19. go(y);&lt;br /&gt;
20. go(z);&lt;br /&gt;
21. }&lt;br /&gt;
22. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. short LONG&lt;br /&gt;
B. SHORT LONG&lt;br /&gt;
C. Compilation fails.&lt;br /&gt;
D. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 95&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
10. class MakeFile {&lt;br /&gt;
11. public static void main(String[] args) {&lt;br /&gt;
12. try {&lt;br /&gt;
13. File directory = new File(”d”);&lt;br /&gt;
14. File file = new File(directory,”f”);&lt;br /&gt;
15. if(!file.exists()) {&lt;br /&gt;
16. file.createNewFile();&lt;br /&gt;
17. }&lt;br /&gt;
18. } catch (IOException e) {&lt;br /&gt;
19. e.printStackTrace&lt;br /&gt;
20. }&lt;br /&gt;
21. }&lt;br /&gt;
22. }&lt;br /&gt;
The current directory does NOT contain a directory named “d.”&lt;br /&gt;
Which three are true? (Choose three.)&lt;br /&gt;
A. Line 16 is never executed.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. Line 13 creates a File object named “d.”&lt;br /&gt;
D. Line 14 creates a File object named “f.’&lt;br /&gt;
E. Line 13 creates a directory named “d” in the file system.&lt;br /&gt;
F. Line 16 creates a directory named “d” and a file ‘f’ within it in the&lt;br /&gt;
file system.&lt;br /&gt;
G. Line 14 creates a file named ‘f’ inside of the directory named “d” in&lt;br /&gt;
the file system.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: BCD&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 96&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
When comparing java.io.BufferedWriter to java.io.FileWriter, which&lt;br /&gt;
capability exists as a method in only one of the two?&lt;br /&gt;
A. closing the stream&lt;br /&gt;
B. flushing the stream&lt;br /&gt;
C. writing to the stream&lt;br /&gt;
D. marking a location in the stream&lt;br /&gt;
E. writing a line separator to the stream&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: E&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 97&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
12. import java.io.*;&lt;br /&gt;
13. public class Forest implements Serializable {&lt;br /&gt;
14. private Tree tree = new Tree();&lt;br /&gt;
15. public static void main(String [] args) {&lt;br /&gt;
16. Forest f= new Forest();&lt;br /&gt;
17. try {&lt;br /&gt;
18. FileOutputStream fs = new FileOutputStream(”Forest.ser”);&lt;br /&gt;
19. ObjectOutputStream os = new ObjectOutputStream(fs);&lt;br /&gt;
20. os.writeObject(f); os.close();&lt;br /&gt;
21. } catch (Exception ex) { ex.printStackTrace(); }&lt;br /&gt;
22. } }&lt;br /&gt;
23.&lt;br /&gt;
24. class Tree { }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. An exception is thrown at runtime.&lt;br /&gt;
C. An instance of Forest is serialized.&lt;br /&gt;
D. A instance of Forest and an instance of Tree are both serialized.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 98&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
1. import java.io.*;&lt;br /&gt;
2. public class Foo implements Serializable {&lt;br /&gt;
3. public int x, y;&lt;br /&gt;
4. public Foo( int x, int y) { this.x = x; this.y = y; }&lt;br /&gt;
5.&lt;br /&gt;
6. private void writeObject( ObjectOutputStream s)&lt;br /&gt;
7. throws IOException {&lt;br /&gt;
8. s.writeInt(x); s.writeInt(y)&lt;br /&gt;
9. }&lt;br /&gt;
10.&lt;br /&gt;
11. private void readObject( ObjectInputStream s)&lt;br /&gt;
12. throws IOException, ClassNotFoundException {&lt;br /&gt;
13.&lt;br /&gt;
14. // insert code here&lt;br /&gt;
15.&lt;br /&gt;
16. }&lt;br /&gt;
17. }&lt;br /&gt;
Which code, inserted at line 14, will allow this class to correctly&lt;br /&gt;
serialize and deserialize?&lt;br /&gt;
A. s.defaultReadObject();&lt;br /&gt;
B. this = s.defaultReadObject();&lt;br /&gt;
C. y = s.readInt(); x = s.readInt();&lt;br /&gt;
D. x = s.readInt(); y = s.readInt();&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 99&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Which three concerning the use of the java.io.Serializable interface are&lt;br /&gt;
true? (Choose three.)&lt;br /&gt;
A. Objects from classes that use aggregation cannot be serialized.&lt;br /&gt;
B. Art object serialized on one JVM can be successfully deserialized on&lt;br /&gt;
a different JVM.&lt;br /&gt;
C. The values in fields with the volatile modifier will NOT survive&lt;br /&gt;
serialization and deserialization.&lt;br /&gt;
D. The values in fields with the transient modifier will NOT survive&lt;br /&gt;
serialization and deserialization.&lt;br /&gt;
E. It is legal to serialize an object of a type that has a supertype that&lt;br /&gt;
does NOT implement java.io.Serializable.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: BDE&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 100&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Assuming that the serializeBanana() and the deserializeBanana()&lt;br /&gt;
methods will correctly use Java serialization and given:&lt;br /&gt;
13. import java.io.*;&lt;br /&gt;
14. class Food implemertts Serializable {int good = 3;}&lt;br /&gt;
15. class Fruit externds Food {int juice = 5;}&lt;br /&gt;
16. public class Banana extends Fruit {&lt;br /&gt;
17. int yellow = 4;&lt;br /&gt;
18. public static void main(String [] args) {&lt;br /&gt;
19. Banana b = new Banana(); Banana b2 = new Banana();&lt;br /&gt;
20. b.serializeBanana(b); // assume correct serialization&lt;br /&gt;
21. b2 = b.deserializeBanana(); // assume correct&lt;br /&gt;
22. System.out.println(”restore “+b2.yellow+ b2.juice+b2.good);&lt;br /&gt;
24. }&lt;br /&gt;
25. // more Banana methods go here&lt;br /&gt;
50. }&lt;br /&gt;
‘What is the result?&lt;br /&gt;
A. restore 400&lt;br /&gt;
B. restore 403&lt;br /&gt;
C. restore 453&lt;br /&gt;
D. Compilation fails.&lt;br /&gt;
E. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/7159599344392412712/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-91-100.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/7159599344392412712'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/7159599344392412712'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-91-100.html' title='SCJP Questions 91-100'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-7053340092160968293</id><published>2011-12-14T17:44:00.001+05:30</published><updated>2011-12-14T17:44:14.884+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 81-90"/><title type='text'>SCJP Questions 81-90</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 81&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. static void test() throws RuntimeException {&lt;br /&gt;
12. try {&lt;br /&gt;
13. System.out.print(”test “);&lt;br /&gt;
14. throw new RuntimeException();&lt;br /&gt;
15. }&lt;br /&gt;
16. catch (Exception ex) { System.out.print(”exception “); }&lt;br /&gt;
17. }&lt;br /&gt;
18. public static void main(String[] args) {&lt;br /&gt;
19. try { test(); }&lt;br /&gt;
20. catch (RuntimeException ex) { System.out.print(”runtime “); }&lt;br /&gt;
21. System.out.print(”end “);&lt;br /&gt;
22. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. test end&lt;br /&gt;
B. Compilation fails.&lt;br /&gt;
C. test runtime end&lt;br /&gt;
D. test exception end&lt;br /&gt;
E. A Throwable is thrown by main at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 82&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given a method that must ensue that its parameter is not null:&lt;br /&gt;
11. public void someMethod(Object value) {&lt;br /&gt;
12. // check for null value&lt;br /&gt;
....&lt;br /&gt;
20. System.out.println(value.getClass());&lt;br /&gt;
21. }&lt;br /&gt;
What, inserted at line 12, is the appropriate way to handle a null&lt;br /&gt;
value?&lt;br /&gt;
A. assert value == null;&lt;br /&gt;
B. assert value !null, “value is null”;&lt;br /&gt;
C. if (value == null) {&lt;br /&gt;
throw new AssertionException(”value is null”);&lt;br /&gt;
D. if (value == null) {&lt;br /&gt;
throw new IllegalArgumentException(”value is null”);&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 83&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
10. public class ClassA {&lt;br /&gt;
11. public void methodA() {&lt;br /&gt;
12. ClassB classB = new ClassB();&lt;br /&gt;
13. classB.getValue();&lt;br /&gt;
14. }&lt;br /&gt;
15. }&lt;br /&gt;
And:&lt;br /&gt;
20. class ClassB {&lt;br /&gt;
21. public ClassC classC;&lt;br /&gt;
22.&lt;br /&gt;
23. public String getValue() {&lt;br /&gt;
24. return classC.getValue();&lt;br /&gt;
25. }&lt;br /&gt;
26. }&lt;br /&gt;
And:&lt;br /&gt;
30. class ClassC {&lt;br /&gt;
31. public String value;&lt;br /&gt;
32.&lt;br /&gt;
33. public String getValue() {&lt;br /&gt;
34. value = “ClassB”;&lt;br /&gt;
35. return value;&lt;br /&gt;
36. }&lt;br /&gt;
37. }&lt;br /&gt;
Given:&lt;br /&gt;
ClassA a = new ClassA();&lt;br /&gt;
a.methodA();&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Compilation fails.&lt;br /&gt;
B. ClassC is displayed.&lt;br /&gt;
C. The code runs with no output.&lt;br /&gt;
D. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 84&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
10. public class Foo {&lt;br /&gt;
11. static int[] a;&lt;br /&gt;
12. static { a[0]=2; }&lt;br /&gt;
13. public static void main( String[] args) {}&lt;br /&gt;
14. }&lt;br /&gt;
Which exception or error will be thrown when a programmer attempts&lt;br /&gt;
to run this code?&lt;br /&gt;
A. java.lang. StackOverflowError&lt;br /&gt;
B. java.lang.IllegalStateException&lt;br /&gt;
C. java.lang.ExceptionlnlnitializerError&lt;br /&gt;
D. java.lang.ArraylndexOutOfBoundsException&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 85&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
10. public class ClassA {&lt;br /&gt;
11. public void count(int i) {&lt;br /&gt;
12. count(++i);&lt;br /&gt;
13. }&lt;br /&gt;
14. }&lt;br /&gt;
And:&lt;br /&gt;
20. ClassA a = new ClassA();&lt;br /&gt;
21. a.count(3);&lt;br /&gt;
Which exception or error should be thrown by the virtual machine?&lt;br /&gt;
A. StackOverflowError&lt;br /&gt;
B. NullPointerException&lt;br /&gt;
C. NumberFormatException&lt;br /&gt;
D. IllegalArgumentException&lt;br /&gt;
E. ExceptionlnlnitializerError&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 86&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class Boxer1 {&lt;br /&gt;
2. Integer i;&lt;br /&gt;
3. int x;&lt;br /&gt;
4. public Boxer1(int y) {&lt;br /&gt;
5. x=i+y;&lt;br /&gt;
6. System.out.println(x);&lt;br /&gt;
7. }&lt;br /&gt;
8. public static void main(String[] args) {&lt;br /&gt;
9. new Boxer1(new Integer(4));&lt;br /&gt;
10. }&lt;br /&gt;
11. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. The value “4” is printed at the command line.&lt;br /&gt;
B. Compilation fails because of an error in line 5.&lt;br /&gt;
C. Compilation fails because of an error in line 9.&lt;br /&gt;
D. A NullPointerException occurs at runtime.&lt;br /&gt;
E. A NumberFormatException occurs at runtime.&lt;br /&gt;
F. An IllegalStateException occurs at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 87&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class TestString 1 {&lt;br /&gt;
2. public static void main(String[] args) {&lt;br /&gt;
3. String str = “420”;&lt;br /&gt;
4. str += 42;&lt;br /&gt;
5. System.out.print(str);&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
What is the output?&lt;br /&gt;
A. 42&lt;br /&gt;
B. 420&lt;br /&gt;
C. 462&lt;br /&gt;
D. 42042&lt;br /&gt;
E. Compilation fails.&lt;br /&gt;
F. An exception is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 88&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. class Converter {&lt;br /&gt;
12. public static void main(String[] args) {&lt;br /&gt;
13. Integer i = args[0];&lt;br /&gt;
14. int j = 12;&lt;br /&gt;
15. System.out.println(”It is “ + (j==i) + “that j==i.”);&lt;br /&gt;
16. }&lt;br /&gt;
17. }&lt;br /&gt;
What is the result when the programmer attempts to compile the code&lt;br /&gt;
and run it with the command java Converter 12?&lt;br /&gt;
A. It is true that j==i.&lt;br /&gt;
B. It is false that j==i.&lt;br /&gt;
C. An exception is thrown at runtime.&lt;br /&gt;
D. Compilation fails because of an error in line 13.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 89&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given this method in a class:&lt;br /&gt;
21. public String toString() {&lt;br /&gt;
22. StringBuffer buffer = new StringBuffer();&lt;br /&gt;
23. buffer.append(’&amp;lt;’);&lt;br /&gt;
24. buffer.append(this.name);&lt;br /&gt;
25. buffer.append(’&amp;gt;’);&lt;br /&gt;
26. return buffer.toString();&lt;br /&gt;
27. }&lt;br /&gt;
Which is true?&lt;br /&gt;
A. This code is NOT thread-safe.&lt;br /&gt;
B. The programmer can replace StringBuffer with StringBuilder with no&lt;br /&gt;
other changes.&lt;br /&gt;
C. This code will perform well and converting the code to use&lt;br /&gt;
StringBuilder will not enhance the performance.&lt;br /&gt;
D. This code will perform poorly. For better performance, the code&lt;br /&gt;
should be rewritten: return “&amp;lt;“+ this.name + “&amp;gt;”;&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 90&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
1. public class MyLogger {&lt;br /&gt;
2. private StringBuilder logger = new StringBuuilder();&lt;br /&gt;
3. public void log(String message, String user) {&lt;br /&gt;
4. logger.append(message);&lt;br /&gt;
5. logger.append(user);&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
The programmer must guarantee that a single MyLogger object works&lt;br /&gt;
properly for a multi-threaded system. How must this code be changed&lt;br /&gt;
to be thread-safe?&lt;br /&gt;
A. synchronize the log method&lt;br /&gt;
B. replace StringBuilder with StringBuffer&lt;br /&gt;
C. No change is necessary, the current MyLogger code is already&lt;br /&gt;
thread-safe.&lt;br /&gt;
D. replace StringBuilder with just a String object and use the string&lt;br /&gt;
concatenation (+=) within the log method&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/7053340092160968293/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-81-90.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/7053340092160968293'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/7053340092160968293'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-81-90.html' title='SCJP Questions 81-90'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9044200624497812337.post-5718063723013081676</id><published>2011-12-14T17:43:00.001+05:30</published><updated>2011-12-14T17:43:37.655+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="SCJP Questions 71-80"/><title type='text'>SCJP Questions 71-80</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 71&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11.classA {&lt;br /&gt;
12. public void process() { System.out.print(”A,”); } }&lt;br /&gt;
13. class B extends A {&lt;br /&gt;
14. public void process() throws IOException {&lt;br /&gt;
15. super.process();&lt;br /&gt;
16. System.out.print(”B,”);&lt;br /&gt;
17. throw new IOException();&lt;br /&gt;
18. } }&lt;br /&gt;
19. public static void main(String[] args) {&lt;br /&gt;
20. try { new B().process(); }&lt;br /&gt;
21. catch (IOException e) { System.out.println(”Exception”); } }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Exception&lt;br /&gt;
B. A,B,Exception&lt;br /&gt;
C. Compilation fails because of an error in line 20.&lt;br /&gt;
D. Compilation fails because of an error in line 14.&lt;br /&gt;
E. A NullPointerException is thrown at runtime.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: D&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 72&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11.classA {&lt;br /&gt;
12. public void process() { System.out.print(”A “); } }&lt;br /&gt;
13. class B extends A {&lt;br /&gt;
14. public void process() throws RuntimeException {&lt;br /&gt;
15. super.process();&lt;br /&gt;
16. if (true) throw new RuntimeException();&lt;br /&gt;
17. System.out.print(“B”); }}&lt;br /&gt;
18. public static void main(String[] args) {&lt;br /&gt;
19. try { ((A)new B()).process(); }&lt;br /&gt;
20. catch (Exception e) { System.out.print(”Exception “); }&lt;br /&gt;
21. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. Exception&lt;br /&gt;
B. A Exception&lt;br /&gt;
C. A Exception B&lt;br /&gt;
D. A B Exception&lt;br /&gt;
E. Compilation fails because of an error in line 14.&lt;br /&gt;
F. Compilation fails because of an error in line 19.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 73&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. static classA {&lt;br /&gt;
12. void process() throws Exception { throw new Exception(); }&lt;br /&gt;
13. }&lt;br /&gt;
14. static class B extends A {&lt;br /&gt;
15. void process() { System.out.println(”B “); }&lt;br /&gt;
16. }&lt;br /&gt;
17. public static void main(String[] args) {&lt;br /&gt;
18.A a=new B();&lt;br /&gt;
19. a.process();&lt;br /&gt;
20.}&lt;br /&gt;
What is the result?&lt;br /&gt;
A. B&lt;br /&gt;
B. The code runs with no output.&lt;br /&gt;
C. An exception is thrown at runtime.&lt;br /&gt;
D. Compilation fails because of an error in line 15.&lt;br /&gt;
E. Compilation fails because of an error in line 18.&lt;br /&gt;
F. Compilation fails because of an error in line 19.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: F&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 74&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. static class A {&lt;br /&gt;
12. void process() throws Exception { throw new Exception(); }&lt;br /&gt;
13. }&lt;br /&gt;
14. static class B extends A {&lt;br /&gt;
15. void process() { System.out.println(”B”); }&lt;br /&gt;
16. }&lt;br /&gt;
17. public static void main(String[] args) {&lt;br /&gt;
18. new B().process();&lt;br /&gt;
19. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. B&lt;br /&gt;
B. The code runs with no output.&lt;br /&gt;
C. Compilation fails because of an error in line 12.&lt;br /&gt;
D. Compilation fails because of an error in line 15.&lt;br /&gt;
E. Compilation fails because of an error in line 18.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 75&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
84. try {&lt;br /&gt;
85. ResourceConnection con = resourceFactory.getConnection();&lt;br /&gt;
86. Results r = con.query(”GET INFO FROM CUSTOMER”);&lt;br /&gt;
87. info = r.getData();&lt;br /&gt;
88. con.close();&lt;br /&gt;
89. } catch (ResourceException re) {&lt;br /&gt;
90. errorLog.write(re.getMessage());&lt;br /&gt;
91. }&lt;br /&gt;
92. return info;&lt;br /&gt;
Which is true if a ResourceException is thrown on line 86?&lt;br /&gt;
A. Line 92 will not execute.&lt;br /&gt;
B. The connection will not be retrieved in line 85.&lt;br /&gt;
C. The resource connection will not be closed on line 88.&lt;br /&gt;
D. The enclosing method will throw an exception to its caller.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 76&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
1. public class A {&lt;br /&gt;
2. public void method1() {&lt;br /&gt;
3. B b=new B();&lt;br /&gt;
4. b.method2();&lt;br /&gt;
5. // more code here&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
1. public class B {&lt;br /&gt;
2. public void method2() {&lt;br /&gt;
3.C c=new C();&lt;br /&gt;
4. c.method3();&lt;br /&gt;
5. // more code here&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
1. public class C {&lt;br /&gt;
2. public void method3() {&lt;br /&gt;
3. // more code here&lt;br /&gt;
4. }&lt;br /&gt;
5. }&lt;br /&gt;
Given:&lt;br /&gt;
25. try {&lt;br /&gt;
26. A a=new A();&lt;br /&gt;
27. a.method1();&lt;br /&gt;
28. } catch (Exception e) {&lt;br /&gt;
29. System.out.print(”an error occurred”);&lt;br /&gt;
30. }&lt;br /&gt;
Which two are true if a NullPointerException is thrown on line 3 of&lt;br /&gt;
class C? (Choose two.)&lt;br /&gt;
A. The application will crash.&lt;br /&gt;
B. The code on line 29 will be executed.&lt;br /&gt;
C. The code on line 5 of class A will execute.&lt;br /&gt;
D. The code on line 5 of class B will execute.&lt;br /&gt;
E. The exception will be propagated back to line 27.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: BE&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 77&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Click the Exhibit button.&lt;br /&gt;
1. public class A {&lt;br /&gt;
2. public void method1() {&lt;br /&gt;
3. try {&lt;br /&gt;
4. B b=new B();&lt;br /&gt;
5. b.method2();&lt;br /&gt;
6. // more code here&lt;br /&gt;
7. } catch (TestException te) {&lt;br /&gt;
8. throw new RuntimeException(te);&lt;br /&gt;
9. }&lt;br /&gt;
6. }&lt;br /&gt;
7. }&lt;br /&gt;
1. public class B {&lt;br /&gt;
2. public void method2() throws TestException {&lt;br /&gt;
3. // more code here&lt;br /&gt;
4. }&lt;br /&gt;
5. }&lt;br /&gt;
1. public class TestException extends Exception {&lt;br /&gt;
2. }&lt;br /&gt;
Given:&lt;br /&gt;
31. public void method() {&lt;br /&gt;
32. A a=new A();&lt;br /&gt;
33. a.method1();&lt;br /&gt;
34. }&lt;br /&gt;
Which is true if a TestException is thrown on line 3 of class B?&lt;br /&gt;
A. Line 33 must be called within a try block.&lt;br /&gt;
B. The exception thrown by method1 in class A is not required to be&lt;br /&gt;
caught.&lt;br /&gt;
C. The method declared on line 31 must be declared to throw a&lt;br /&gt;
RuntimeException.&lt;br /&gt;
D. On line 5 of class A, the call to method2 of class B does not need to&lt;br /&gt;
be placed in a try/catch block.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 78&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. public static void main(String[] args) {&lt;br /&gt;
12. try {&lt;br /&gt;
13. args=null;&lt;br /&gt;
14. args[0] = “test”;&lt;br /&gt;
15. System.out.println(args[0]);&lt;br /&gt;
16. } catch (Exception ex) {&lt;br /&gt;
17. System.out.println(”Exception”);&lt;br /&gt;
18. } catch (NullPointerException npe) {&lt;br /&gt;
19. System.out.println(”NullPointerException”);&lt;br /&gt;
20. }&lt;br /&gt;
21. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. test&lt;br /&gt;
B. Exception&lt;br /&gt;
C. Compilation fails.&lt;br /&gt;
D. NullPointerException&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: C&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 79&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. static void test() throws Error {&lt;br /&gt;
12. if (true) throw new AssertionError();&lt;br /&gt;
13. System.out.print(”test “);&lt;br /&gt;
14. }&lt;br /&gt;
15. public static void main(String[] args) {&lt;br /&gt;
16. try { test(); }&lt;br /&gt;
17. catch (Exception ex) { System.out.print(”exception “); }&lt;br /&gt;
18. System.out.print(”elld “);&lt;br /&gt;
19. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. end&lt;br /&gt;
B. Compilation fails.&lt;br /&gt;
C. exception end&lt;br /&gt;
D. exception test end&lt;br /&gt;
E. A Throwable is thrown by main.&lt;br /&gt;
F. An Exception is thrown by main.&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: E&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: red;&quot;&gt;Question 80&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;
Given:&lt;br /&gt;
11. static void test() {&lt;br /&gt;
12. try {&lt;br /&gt;
13. String x=null;&lt;br /&gt;
14. System.out.print(x.toString() +“ “);&lt;br /&gt;
15. }&lt;br /&gt;
16. finally { System.out.print(“finally “); }&lt;br /&gt;
17. }&lt;br /&gt;
18. public static void main(String[] args) {&lt;br /&gt;
19. try { test(); }&lt;br /&gt;
20. catch (Exception ex) { System.out.print(”exception “); }&lt;br /&gt;
21. }&lt;br /&gt;
What is the result?&lt;br /&gt;
A. null&lt;br /&gt;
B. finally&lt;br /&gt;
C. null finally&lt;br /&gt;
D. Compilation fails.&lt;br /&gt;
E. finally exception&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;color: blue;&quot;&gt;Answer: E&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javasehelp.blogspot.com/feeds/5718063723013081676/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-71-80.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/5718063723013081676'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9044200624497812337/posts/default/5718063723013081676'/><link rel='alternate' type='text/html' href='http://javasehelp.blogspot.com/2011/12/scjp-questions-71-80.html' title='SCJP Questions 71-80'/><author><name>Java Help</name><uri>http://www.blogger.com/profile/10011268256731280757</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFmOsk8XayDQxz7Hw4CVvea9tTO6lt5TDwm74BBXyO7pOgP21ao4BgzzSGS4vhAoXZD7ewYZOyGl0pniUWi0lVD0uGLY-4wAYmBuEYI3t4GyBwtjxXaiBEyOxOSyB6Vw/s220/4598885_desert_two.jpg'/></author><thr:total>0</thr:total></entry></feed>