tag:blogger.com,1999:blog-78637221896116438312024-02-21T00:36:09.648-08:00SCJP Tutorial -1.5 Tips and TricksSCJP 1.5 and 1.6 Tips and Tricks, Mock Questions, Answers, Last Minute Revision Points, Links, Java J2EE Interview Questions and Answers, Java Programsjavaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.comBlogger29125tag:blogger.com,1999:blog-7863722189611643831.post-89497956973613560392009-08-09T20:58:00.000-07:002010-10-23T21:12:59.080-07:00Imortant Revision Notes for Classes and Constructors in Java<div style="color: blue;"><b>Class Declaration</b>--> Syntax of class declaration is as follows:</div><div style="color: blue;"><br />
<i><b>(class modifiers) class (class name)</b></i><br />
<i><b> (extends clause) (implements clause)</b></i> // Class header</div><div style="color: blue;"><br />
<i><b>{ // Class body</b></i><br />
<i><b> -- field declarations</b></i></div><div style="color: blue;"><i><b> --method declarations</b></i></div><div style="color: blue;"><i><b> --nested class declarations</b></i></div><div style="color: blue;"><i><b> --nested interface declarations</b></i><br />
<i><b> --constructor declarations</b></i><br />
<i><b> --initializer blocks</b></i></div><div style="color: blue;"><i><b>}</b></i><br />
<br />
<b>Methods of a Class</b>--> They are the class members and are also called operations. They define behaviour of the class. </div><div style="color: blue;"><br />
</div><div style="color: blue;">Syntax of their declaration is as follows:</div><div style="color: blue;"><br />
</div><div style="color: blue;"><i><b>(method modifiers) (return type) (method name) ((formal parameter list))</b></i><br />
<i><b> (throws clause) // Method prototype</b></i></div><div style="color: blue;"><i><b><br />
</b></i><br />
<i><b>{ // Method body</b></i><br />
<i><b> --local variable declarations</b></i></div><div style="color: blue;"><i><b> --nested local class declarations</b></i><br />
<i><b> --statements</b></i><br />
<i><b>}</b></i><br />
<br />
<b>Method Overloading</b>--> This means several methods share same name but have different argument list.<br />
<br />
<b>Constructor</b>--> It is a special method which is used to initialize state of an object when it is created using new operator. </div><div style="color: blue;"><br />
</div><div style="color: blue;">It has following syntax:</div><div style="color: blue;"><br />
<i><b>(accessibility modifier) (class name) ((formal parameter list))</b></i><br />
<i><b> (throws clause) // Constructor header</b></i><br />
<i><b> { // Constructor body</b></i><br />
<i><b> --local variable declarations</b></i><br />
<i><b> --nested local class declarations</b></i><br />
<i><b> --statements</b></i><br />
<i><b> }</b></i></div><div style="color: blue;"><br />
However there are<b> few constraints for a method to be constructor:</b><br />
a) Their modifiers can be only accessibility modifiers.<br />
b) They cannot have a return type<br />
c) Their should be same as the class name.<br />
<br />
<b>Default Constructor</b>--> It is the constructor with no argument. Its syntax is: classname()<br />
<br />
<b>Implicit Default Constructor</b>-->If no constructor is defined in the class, then implicit default constructor is provided by java.</div><div style="color: blue;"><br />
</div><div style="color: blue;">Its syntax is: </div><div style="color: blue;"></div><div style="color: blue;"><br />
<i><b>classname(){super();}</b></i></div>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-62435033807300945162009-07-12T02:10:00.000-07:002010-10-23T21:11:33.171-07:00Important Points to Remember in Java<div style="color: blue;"><b><span style="font-size: small;">Method Parameters:</span></b><br />
</div><div style="color: blue;"><span style="font-size: small;">Types of Method Parameters in Java:</span><br />
<span style="font-size: small;"><br />
a)<b> Formal Parameters</b>-->They are the parameters defined in the method definition.</span><br />
<span style="font-size: small;"><br />
b) <b>Actual Parameter</b>s--> They are the parameters defined in the method call.</span><br />
<span style="font-size: small;"><br />
c) <b>Final parameter</b>--> If a formal parameter is declared to be final, then its value cannot be changed during its lifetime in the method body.</span><br />
<span style="font-size: small;"></span><br />
<span style="font-size: small;"><br />
</span><span style="font-size: small;">Note: Widening, narrowing, type promotion rule applies to the parameter passing as well. They are equivalent to assignment conversions.</span><br />
<span style="font-size: small;"><br />
</span><br />
<span style="font-size: small;">***************************************</span><br />
<span style="font-size: small;">2. <b>Arrays: </b></span><br />
<span style="font-size: small;"><br />
</span><br />
<span style="font-size: small;"> a) <b>Array</b>--> Its a collection of homogeneous data elements.<br />
<br />
b) <b>Array Declaration</b>-->Syntax for array declaration is as follows:</span><br />
<span style="font-size: small;"><br />
<i><b> (datatype)[](arrayname) or (datatype) (arrayname)[]</b></i><br />
<br />
c) <b>Array Construction</b>--> This means allocating space to the array in computer memory. Its syntax is:</span><br />
<span style="font-size: small;"><br />
<i><b>(arrayname)=new (datatype)[(arraysize)]</b></i><br />
<br />
d) <b>Value assignment</b>--> This means assigning values to the array elements. <br />
<br />
e) <b>Array Initialization</b>--> Java provides the means of declaring, constructing and explicitly initializing an array in one declaration. Its syntax is:</span><br />
<span style="font-size: small;"><br />
<i><b>(datatype)[](arrayname)={(array initialization list)};</b></i></span><br />
<span style="font-size: small;"></span><br />
<span style="font-size: small;"></span><br />
<span style="font-size: small;"></span><br />
<span style="font-size: small;"></span><br />
<span style="font-size: small;"></span><br />
<span style="font-size: small;"></span><br />
<span style="font-size: small;"><br />
*******************************</span><br />
<span style="font-size: small;"><br />
<b>3. Anonymous Array</b>--> They are used to initialize the array objects which are already declared. like:<br />
int a[];<br />
a=new int[]{1,5,5,3,9,7,3}; we cannot use a={1,5,5,3,9,7,3}; to initialize an array already declared.</span><br />
<span style="font-size: small;"><br />
</span><br />
<span style="font-size: small;"><br />
</span><br />
<span style="font-size: small;">********************************<br />
</span></div>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com1tag:blogger.com,1999:blog-7863722189611643831.post-84194016186909966042009-06-16T02:06:00.000-07:002010-10-22T02:38:03.992-07:00Java More Definations and Concepts<strong><span style="color: blue;">Types of variables</span></strong><br />
<span style="color: blue;"></span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">1) <strong>Instance variables</strong>--> They are non static members of a class. Every object of a class has its own copy of these variables. Their values exist as long as object containing them exists. Initialized to default valued by default.</span><br />
<br />
<span style="color: blue;">2)<strong>Static Variables</strong>-->They belong to the class and are created when class is loaded first time at runtime.They exists as long as class exists. Initialized to default value by default.</span><br />
<br />
<span style="color: blue;">3)<strong>Local variables</strong>-->Created in method or block and executed for method or block. After the execution of method or block, they are no more accessible. They should be explicitly initialized in a non conditional statement, before being used.</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">************************************</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;"><strong>Main method</strong>--> This is the method where from execution of a program can start. It must be public, static and void. It should be public so that it can be accessed by Java Interpreter. It should be static so that It can be accessed without object of a class being created. public and static keywords can appear in any order.</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">************************************</span><br />
<span style="color: blue;"><br />
</span><br />
<strong><span style="color: blue;">Typecasting and converstions:</span></strong><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">1) <strong>Narrowing</strong>--->Conversion of broader datatype to narrower datatype is called narrowing. It results in loss of magnitued information. This means bits from the left of the binary respresentation are truncated to fit in the destination variable. Like conversion from float to int. <strong><em>In this explicit cast is required.</em></strong></span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">2)<strong>Widening</strong>--->Conversion from narrower type to the broader one is called widening. <strong><em>In this no explicit cast is required.</em></strong></span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">*******************************************</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;"><strong>Unary Numeric Promotion</strong>--> In a unary operator if type of operand is narrower than int, it is converted to int. It is applicable to +,-,>>,<<,>>>,~ and expressions in array initialization and array indexes. Note: not applicable to ++, -- operators.</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">*******************************************</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;"><strong>Binary Numeric Promotion</strong>--> In binary numeric promotion type of the expression is promoted to the type of broadest operand which is atleast an int.</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">******************************************</span><br />
<br />
<span style="color: blue;"><strong>String Concatenation</strong>-->When an operand is added to a string object using '+' sign, then any of the following cases take place:</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">a) If other operand is primitive datatype(int, long etc) its value is converted to the string object with string representation of its value.</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">b) Values like true, false and null are also converted to String objects their corresponding string respresentation. A reference variable with null value is also converted to String with string representation as "null"</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">c) For all other references, String is constructed by calling toString method of the referred object.</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">******************************************</span><br />
<span style="color: blue;"><br />
</span><br />
<strong><span style="color: blue;">Conditional Operators:</span></strong><br />
<br />
<span style="color: blue;">They are used to check the condition of an expression.</span><br />
<span style="color: blue;"><br />
</span><br />
<strong><span style="color: blue;">&& --> Shortcircuit AND </span></strong><br />
<strong><br />
<span style="color: blue;"></span></strong><br />
<strong><span style="color: blue;">& --> Bitwise AND</span></strong><br />
<br />
<strong><span style="color: blue;">|| --> Shortcircuit OR</span></strong><br />
<br />
<strong><span style="color: blue;">| --> Bitwise OR</span></strong><br />
<span style="color: blue;"></span><br />
<strong><span style="color: blue;">Difference between && and & is as given below:</span></strong><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">In case of && if first expression is false then next condition is not checked. However in case of & if first condition is false, still next condition is checked.</span><br />
<strong><span style="color: blue;">! --> NOT</span></strong><br />
<br />
<span style="color: blue;">It is used to invert the value of a boolean expression. true is changed to false and vice versa.</span><br />
<br />
<span style="color: blue;">***************************************</span><br />
<span style="color: blue;"><br />
</span><br />
<strong><span style="color: blue;">Integer Bitwise operators:</span></strong><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;"><strong>~ Bitwise Compliment</strong>--> Inverts all bits of an operand</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;"><strong>& Bitwise AND</strong>--> Returns 1 if both corresponding bits of two operands are 1, else returns 0.</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;"><strong>! Bitwise OR</strong>--> Returns 0 if both corresponding bits of two operands are 0, else returns 1.</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;"><strong>^ Bitwise Exclusivley OR</strong>--> Returns 1 if one bit of the two operands is 0 and another one is 1, else returns 0.</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">*******************************************</span><br />
<span style="color: blue;"><br />
</span><br />
<strong><span style="color: blue;">Shift Operators</span></strong><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;"><strong>a>>n</strong>--> Right shift carry sign bit</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;"><strong>a<<n< strong="">--> Left shift zero fill.</strong></span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;"><strong>a>>>n</strong>--> Right shift zero fill.</span><br />
<span style="color: blue;"><br />
</span><br />
<span style="color: blue;">*********************************************</span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-59799500328503817792009-06-10T01:51:00.000-07:002010-10-21T01:55:01.328-07:00Java Important Terms, Definations and Concepts<span style="background-color: white; color: blue; font-size: large;"><strong>Features of Java</strong></span><span style="background-color: white;"><br />
</span><br />
<span style="color: blue;"><strong>--------------------------------</strong></span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">1) <strong>Encapsulation</strong>--> This means grouping of data members and member functions into single unit called class. This also means grouping of related entities into single unit. For ex grouping of classes, interfaces and subpackages into single package.</span><br />
<br />
<span style="background-color: white; color: blue;">************************************************</span><br />
<br />
<span style="background-color: white; color: blue;">2)<strong>Polymorphism</strong>-->This means one interface many forms.We can also so existing in many forms. This can be of two type:</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">a) <strong>Static Polymorphism</strong>--> This means specifying things at compile type. For example method overloading. We specify at compile time by passing arguments that which method to be called.</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">b)<strong>Dynamic Polymorphism</strong>--> This means specifying things at runtime. For example method overriding. Say for ex: a super class variable can have object of super class or any of its sub classes. If we call an overriden method, then its the method of the class whose object is referred by super class variable at runtime.</span><br />
<br />
<span style="background-color: white; color: blue;">***************************************</span><br />
<br />
<span style="background-color: white; color: blue;">3)<strong>Inheritance</strong>--> It is the fundamental mechanism of creating a new class from an existing one. This way new class has properties of base class as well as new class. This is called is-a relation. This should be used where is-a relation is maintained throughout the lifecycle of the objects involved.</span><br />
<br />
<span style="background-color: white; color: blue;">******************************************</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">4)<strong>Aggregation</strong>-->This means creating a new class using existing class as datamember of the new class. This is called has-a relation.</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">******************************************</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">5)<strong>Class</strong>--> A class is a blueprint or category of objects. A class specifies properties and behaviour of a an object. The properties of a class are called attributes and defined by fields in Java. The behaviour of a class is operations and defined by methods in Java. They both collectively are called members.</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">******************************************</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">6)<strong>Object</strong>--> It is an instance of a class. It is constructed using blueprint of the class.</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">******************************************</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">7)<strong>Object References</strong>--> Its same as object variable.</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">******************************************</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">8)<strong>Static Members</strong>--> Static members belong to the class and not to a particular object.</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">******************************************</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">9)<strong>Instance Members</strong>--> They belong to a particular object.Each object has its own copy of instance member.</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">******************************************</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">10)<strong> Static block</strong>--> This is the block contained in a class and is executed each time class is loaded. That is its called even if a class contains no main method and we execute the class.It is generally used to initialize static members.</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">*******************************************</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">11)<strong>Non Static Block</strong>--> It is called each time an object of a class is created. If static, non-static and constructor are there in the class, then they are executed in following order: static blok-->non-static block--> constructor.</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">*****************************************</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">There are <strong>two ways of defining a new class</strong> from an existing class, inheritance and aggregation.</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white;"><span style="color: blue;"><strong>Inheritance</strong>-->This means defining a new class from an existing one, which contains its own properties, apart of properties from a base class. Its an "is a relation" in uml notation.</span></span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white;"><span style="color: blue;"><strong>Aggregation</strong>-->In this approach a new class is constructed using objects from other classes as constituent or members of this class.</span></span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">****************************************</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white;"><span style="color: blue;"><strong>12)Lexical tokens</strong>--> The low-level language elements are called lexical tokens and are used to construct more complex language constructs. identifiers, numbers, operators and special characters are called lexical tokens.</span></span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">*****************************************</span><br />
<span style="background-color: white; color: blue;">13)<strong>Keywords</strong>--> Keywords are reserved identifiers in a language and cannot be used to denote other entities.</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">******************************************</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white;"><span style="color: blue;"><strong>14) Literal</strong>--> It denotes a constant value. </span></span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">String Literal example>"Hello","world"</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">*******************************************</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<strong><span style="background-color: white; color: blue;">Primitive Datatypes and Literal Examples: </span></strong><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">Integer Literal example-> 2,3</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">Floating Point Literal example-> 2.45,3.15</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">Boolean Literal example--> true, false</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">Character literal example-->'a','2','\u002'(four digit hexadecimal number from \u0000 to \uffff), '\121'(octal number from \0 to \377)</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">Long Literal example-->10l,20l</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">double literal example-->3.21d,5.54d</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">octal literal example-->012,07</span><br />
<span style="background-color: white;"><br />
<span style="color: blue;"></span></span><br />
<span style="background-color: white; color: blue;">Hexadecimal literal example-->0x1b,0x10</span><br />
<br />
<span style="background-color: white; color: blue;">Thats all for the day !!!!</span><br />
<br />
<span style="background-color: white; color: blue;">*********************************************</span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-59607921675189358872009-05-15T06:59:00.000-07:002010-10-21T01:55:43.914-07:00Java Mock Exam Questions with detailed answers<div style="color: blue;"><b>Q1 Given:</b><br />
<br />
class J {<br />
private static int notFinalized;<br />
public static int notFinalized() {return notFinalized;}<br />
private K k;<br />
private int name;<br />
public int name() {return name;}<br />
public J(K k, int i) {this.k = k; name = i; notFinalized++;}<br />
public void finalize() {<br />
synchronized (k) {<br />
System.out.print(name);<br />
notFinalized--;<br />
k.notify();<br />
}<br />
}<br />
}<br />
<br />
class K {<br />
private void m1() {<br />
J j = null;<br />
for (int i = 0; i < 5; i++) {<br />
j = new J(this, i); // 1<br />
}<br />
Runtime.getRuntime().gc(); // 2<br />
synchronized (this) {<br />
while (J.notFinalized() > 0) {<br />
try {wait();} catch (InterruptedException ie) {}<br />
}<br />
}<br />
}<br />
public static void main(String[] args) {<br />
new K().m1();<br />
}<br />
}<br />
<br />
<br />
<b>When the processing of line 2 begins how many objects of type J that were created at line 1 are eligible for garbage collection?</b><br />
<br />
(1) 0<br />
(2) 1<br />
(3) 4<br />
(4) 5<br />
(5) Can not be determined without more information<br />
(6) Compiler error<br />
(7) Run time error<br />
(8) None of the above<br />
<br />
<b>Answer : 3<br />
Explanation :</b><br />
Method K.m1 creates five objects of type J. Each instance has a name represented by an integer between 0 and 4 inclusive. <br />
If the garbage collector does not run then the program will produce no output.If the garbage collector does run then the output of the program could be a series of integers that are the names of four of the five objects. <br />
As each new object is created its reference is assigned to the reference variable j.The previously referenced object then becomes eligible for garbage collection. The last object created, 4, is not available for garbage collection until method m1 runs to completion.<br />
The while loop in the synchronized block will never complete because J.notFinalized will never return zero.<br />
This program is intended to provide a working example of garbage collecting objects referenced by local variables.<br />
<br />
<b>Q2 What is the output of the following code when compiled and run? Select two correct answers</b><br />
<br />
<br />
1 public class Sample {<br />
2 public static void main(String[] args){<br />
3 int y=0; <br />
4 int x=z=1; <br />
5 System.out.println(y+","+x+","+z);<br />
6 }<br />
7 }<br />
<br />
<br />
<br />
(1) Prints 0,1,1<br />
(2) Error during compilation at line 3<br />
(3) Prints 0,0,1<br />
(4) Error during compilation at line 4<br />
(5) Error during compilation at line 5<br />
<br />
<b>Answer : 4,5<br />
Explanation :</b><br />
Variable z is not declared, thus, z cannot be resolved on lines 2 and 3. In Java, z cannot be declared that way. In order to get this code to compile, we have to write either:<br />
int z=1,x=z;<br />
Or<br />
int z=1;<br />
int x=z;<br />
Or<br />
int z=1;<br />
int x=1;<br />
<br />
<b>Q3 What is the output of the following code when compiled and run? Select one correct answer</b><br />
<br />
<br />
1 public class Sample {<br />
2 public static void main(String[] args){<br />
3 int j = 017;<br />
4 int i = (byte)j >> 2;<br />
5 System.out.println(Integer.toBinaryString(i));<br />
6 }<br />
7 }<br />
<br />
<br />
<br />
(1) Prints 3<br />
(2) Error during compilation at line 4<br />
(3) Error during compilation at line 5<br />
(4) Prints 11<br />
(5) Prints 0<br />
<br />
<b>Answer : 4<br />
Explanation :</b><br />
First off, 017 is an octal integer literal having the decimal value 15. Second, the cast to byte only applies to j and not to j >> 2 as a whole. Thus, j is downcast to byte and then upcast to int again before the shifting.Briefly, the cast has no effect here. Then, the binary sequence of 15 is 00000000 00000000 00000000 00001111, which, shifted 2 bits to the right, yields 00000000 00000000 00000000 00000011. Finally, the binary sequence, 11, is printed. Note that the prefixed 0's are dismissed.<br />
<br />
<b>Q4 Select three correct statements:</b><br />
<br />
(1) The garbage collection thread cannot outlive the last user thread<br />
(2) The garbage collection can be forced by invoking System.gc().<br />
(3) The garbage collection thread is a non-deamon thread<br />
(4) The finalize() method is invoked at most once by the JVM for any given object<br />
(5) The finalize() method may resurrect the object upon which it has been invoked<br />
<br />
<b> Answer : 1,4,5<br />
Explanation :</b><br />
The garbage collection thread is a deamon thread. The latter die when there are no more users threads running. The garbage collection cannot be forced.<br />
<br />
<b>Q5 What is the output of the following code when compiled and run? Select one correct answer.</b><br />
<br />
import java.io.*;<br />
public class Mohit{<br />
public static void main(String[] args) {<br />
MohitSub myref = new MohitSub();<br />
try{<br />
myref.test();<br />
}catch(IOException ioe){}<br />
}<br />
void test() throws IOException{<br />
System.out.println("In Mohit");<br />
throw new IOException();<br />
}<br />
}<br />
class MohitSub extends Mohit {<br />
void test() {<br />
System.out.println("In MohitSub");<br />
}<br />
}<br />
<br />
<br />
<br />
(1) Prints:<br />
<br />
In MohitSub<br />
<br />
(2) Prints:<br />
<br />
In Mohit<br />
<br />
(3) Prints:<br />
<br />
In Mohit<br />
In MohitSub<br />
<br />
(4) Prints:<br />
<br />
In MohitSub<br />
In Mohit<br />
<br />
(5) The code does not compile<br />
<br />
<b>Answer : 5<br />
Explanation :</b><br />
The code does not compile because no IOException is thrown when invoking myref.test(). Note that myref's declared and runtime types are MohitSub and thus no dynamic lookup will be performed. However, if you change the declared type to Mohit, the code will compile and the correct answer would be A because method test() isoverridden in MohitSub<br />
<br />
<b>Q6 What is the output of the following code when compiled and run with the following command line: java Friends two three? Select two correct answers.</b><br />
<br />
public class Friends {<br />
public static void main(String[] args) throws Exception {<br />
int i=2;<br />
boolean b = true;<br />
throw new Exception("Values are:"+(b!=b)+","+(i=args.length)+","+(b=i==2));<br />
}<br />
}<br />
<br />
<br />
<br />
(1) The exception message is Values are:false,3,true<br />
(2) The exception message is Values are:true,2,false<br />
(3) The exception message is Values are:false,2,true<br />
(4) The final value of b is false<br />
(5) An exception is thrown at runtime<br />
<br />
<b> Answer : 3,5<br />
Explanation :</b><br />
Do not mix b!=b and b=!b. In the former, we check if b's value is different from b's value (?!) which is clearly false. In the latter, we assign b's opposite value to itself, that is, if b is true, then after b=!b, b ends up being false.Moreover, be aware that b=i==2 is evaluated as b=(i==2) because operator = has the lowest precedence. Finally, note that the arguments to the Exception constructor are evaluatedfrom the left to the right. First, b!=b is evaluated, then i=args.length (args.length is 2, so i keeps its value), and finally, b=i==2.<br />
<br />
<b>Q7 Select two correct statements about the code given below?</b><br />
<br />
<br />
class A{}<br />
class B extends A implements E{} //line 1<br />
class C extends A{}<br />
class D extends B{}<br />
interface E{}<br />
public class Question07 {<br />
public static void main(String[] args) {<br />
A a = new D(); //line 2<br />
C c = new C(); //line 3<br />
E e = (E)a; //line 4<br />
B b = (B)e; //line 5<br />
}<br />
}<br />
<br />
<br />
<br />
(1) The code compiles without error and runs fine<br />
(2) Compilation error on line 1 because interface E is not yet declared (forward-referencing)<br />
(3) Compilation error on line 4 because class A does not implement interface E<br />
(4) The cast on line 4 is mandatory<br />
(5) The cast on line 5 is not mandatory<br />
<br />
<b>Answer : 1,4<br />
Explanation :</b><br />
First, pay attention to the class hierarchy (B and C are sibling classes!!) Then, there is no such thing as forward-referencing issues when using interfaces declared later in the compilation unit.On line 4, we are dealing with an object whose runtime type is D which implements interface E. The cast is mandatory, though, since the reference type (A) is not assignmentcompatible with the reference type E. The cast on line 5 is mandatory for the same reasons.<br />
<br />
<b>Q8 How many objects are eligible for garbage collection immediately after line 1? Select one correct answer.</b><br />
<br />
<br />
public class HomeGC {<br />
public static void main(String[] args) {<br />
HomeGC tGC = new HomeGC();<br />
tGC.doSomething(); //line 1<br />
Thread.sleep(20000);<br />
}<br />
<br />
public void doSomething(){<br />
Object[] objArray = new Object[2];<br />
for(int i = 0 ; i < objArray.length ; i++) {<br />
objArray[i] = new Object();<br />
}<br />
}<br />
}<br />
<br />
<br />
<br />
(1) 0<br />
(2) 1<br />
(3) 2<br />
(4) 3<br />
(5) 4<br />
<br />
<b>Answer : 4<br />
Explanation :</b><br />
We declare an array of Object of length two. We then initialize each element to a new Object. We have 2 objects in the array and the array itself (which is an object, too!), that makes 3.<br />
<br />
<b>Q9 What is the output of the following code when compiled and run? Select one correct answer.</b><br />
<br />
<br />
public class ABC {<br />
public static void main(String[] args) {<br />
try {<br />
int i = (int)(Math.random()*10);<br />
if(i<=5)<br />
System.out.println("i = "+i);<br />
else<br />
throw new Exception("i > 5");<br />
} catch (Exception e){<br />
System.err.println(e.getMessage()+" (i="+i+")");<br />
}<br />
}<br />
}<br />
<br />
<br />
<br />
(1) The output cannot be determined<br />
(2) Compilation error<br />
(3) An exception is thrown at runtime<br />
(4) Output is i = 2<br />
(5) Output is i > 5 (i=6)<br />
<br />
<b>Answer : 2<br />
Explanation :</b><br />
The code does not compile because i (declared in the try block!) is not in scope when accessed from the catch block.<br />
<br />
<b>Q10 What is the output of the following code when compiled and run? Select one correct answer.</b><br />
<br />
<br />
public class ABCSample {<br />
public static void main(String[] args) {<br />
new ABCSample().doSomething();<br />
}<br />
<br />
public void doSomething(){<br />
int i=5;<br />
Thread t = new Thread(new Runnable(){<br />
public void run(){<br />
for(int j=0;j<=i;j++){<br />
System.out.print(" "+j);<br />
}<br />
}<br />
});<br />
t.start();<br />
}<br />
}<br />
<br />
<br />
<br />
(1) Prints 0 1 2 3 4<br />
(2) Compilation error<br />
(3) No output<br />
(4) IllegalThreadStateException is thrown at runtime<br />
(5) Prints 0 1 2 3 4 5<br />
<br />
<b> Answer : 2<br />
Explanation :</b><br />
The code does not compile because the anonymous inner class (new Runnable(){...}) tries to access the non-final local variable i.</div>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-68991244885449394762009-04-19T06:36:00.000-07:002010-10-21T01:02:47.071-07:00Exam Objectives and Syllabus for SCJP 1.6<h5 style="color: #000099; font-family: arial;"><span style="font-size: 100%;"><br />
</span></h5><div class="MsoNormal" style="color: #000099; font-family: arial;"><span style="font-size: 100%;"><b>Section 1: Declarations, Initialization and Scoping </b></span></div><div class="MsoNormal" style="color: #000099; font-family: arial;"><br />
<span style="font-size: 100%;"></span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">a)</span><span style="font-size: 100%;">Develop code that declares classes (including abstract and all forms of nested classes), interfaces, and enums, and includes the appropriate use of package and import statements (including static imports). </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">b)Develop code that declares an interface. Develop code that implements or extends one or more interfaces. Develop code that declares an abstract class. Develop code that extends an abstract class. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">c)</span><span style="font-size: 100%;">Develop code that declares, initializes, and uses primitives, arrays, enums, and objects as static, instance, and local variables. Also, use legal identifiers for variable names. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">d)</span><span style="font-size: 100%;">Develop code that declares both static and non-static methods, and - if appropriate - use method names that adhere to the JavaBeans naming standards. Also develop code that declares and uses a variable-length argument list. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">e)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Given a code example, determine if a method is correctly overriding or overloading another method, and identify legal return values (including covariant returns), for the method. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">f)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Given a set of classes and superclasses, develop constructors for one or more of the classes. Given a class declaration, determine if a default constructor will be created, and if so, determine the behavior of that constructor. Given a nested or non-nested class listing, write code to instantiate the class. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">*******************************************************************</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial;"><span style="font-size: 100%;"><b>Section 2: Flow Control </b></span></div><div class="MsoNormal" style="color: #000099; font-family: arial;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">a)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Develop code that implements an if or switch statement; and identify legal argument types for these statements. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">b)Develop code that implements all forms of loops and iterators, including the use of for, the enhanced for loop (for-each), do, while, labels, break, and continue; and explain the values taken by loop counter variables during and after loop execution. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">c)Develop code that makes use of assertions, and distinguish appropriate from inappropriate uses of assertions. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">d)Develop code that makes use of exceptions and exception handling clauses (try, catch, finally), and declares methods and overriding methods that throw exceptions. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">e)Recognize the effect of an exception arising at a specified point in a code fragment. Note that the exception may be a runtime exception, a checked exception, or an error. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">f)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Recognize situations that will result in any of the following being thrown: ArrayIndexOutOfBoundsException,ClassCastException, IllegalArgumentException, IllegalStateException, NullPointerException, NumberFormatException, AssertionError, ExceptionInInitializerError, StackOverflowError or NoClassDefFoundError. Understand which of these are thrown by the virtual machine and recognize situations in which others should be thrown programatically. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">***************************************************************</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial;"><span style="font-size: 100%;"><b>Section 3: API Contents </b></span></div><div class="MsoNormal" style="color: #000099; font-family: arial;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">a)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Develop code that uses the primitive wrapper classes (such as Boolean, Character, Double, Integer, etc.), and/or autoboxing & unboxing. Discuss the differences between the String, StringBuilder, and StringBuffer classes. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">b)</span><span style="font-size: 100%;">Given a scenario involving navigating file systems, reading from files, writing to files, or interacting with the user, develop the correct solution using the following classes (sometimes in combination), from java.io: BufferedReader, BufferedWriter, File, FileReader, FileWriter, PrintWriter, and Console. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">c)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Develop code that serializes and/or de-serializes objects using the following APIs from java.io: DataInputStream, DataOutputStream, FileInputStream, FileOutputStream, ObjectInputStream, ObjectOutputStream and Serializable. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">d)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Use standard J2SE APIs in the java.text package to correctly format or parse dates, numbers, and currency values for a specific locale; and, given a scenario, determine the appropriate methods to use if you want to use the default locale or a specific locale. Describe the purpose and use of the java.util.Locale class. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">e)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Write code that uses standard J2SE APIs in the java.util and java.util.regex packages to format or parse strings or streams. For strings, write code that uses the Pattern and Matcher classes and the String.split method. Recognize and use regular expression patterns for matching (limited to: . (dot), * (star), + (plus), ?, \d, \s, \w, [], ()). The use of *, +, and ? will be limited to greedy quantifiers, and the parenthesis operator will only be used as a grouping mechanism, not for capturing content during matching. For streams, write code using the Formatter and Scanner classes and the PrintWriter.format/printf methods. Recognize and use formatting parameters (limited to: %b, %c, %d, %f, %s) in format strings. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">****************************************************************** </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial;"><span style="font-size: 100%;"><b>Section 4: Concurrency </b></span></div><div class="MsoNormal" style="color: #000099; font-family: arial;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">a)</span><span style="font-size: 100%;">Write code to define, instantiate, and start new threads using both java.lang.Thread and java.lang.Runnable. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">b)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Recognize the states in which a thread can exist, and identify ways in which a thread can transition from one state to another. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">c)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Given a scenario, write code that makes appropriate use of object locking to protect static or instance variables from concurrent access problems. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">d)</span><span style="font-size: 100%;">Given a scenario, write code that makes appropriate use of wait, notify, or notifyAll. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">****************************************************************</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial;"><span style="font-size: 100%;"><b>Section 5: OO Concepts </b></span></div><div class="MsoNormal" style="color: #000099; font-family: arial;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">a)</span><span style="font-size: 100%;">Develop code that implements tight encapsulation, loose coupling, and high cohesion in classes, and describe the benefits. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">b)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Given a scenario, develop code that demonstrates the use of polymorphism. Further, determine when casting will be necessary and recognize compiler vs. runtime errors related to object reference casting. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">c)</span><span style="font-size: 100%;">Explain the effect of modifiers on inheritance with respect to constructors, instance or static variables, and instance or static methods. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">d)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Given a scenario, develop code that declares and/or invokes overridden or overloaded methods and code that declares and/or invokes superclass, or overloaded constructors. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">e)<span style="font-family: "Times New Roman";"> </span></span><span style="font-size: 100%;">Develop code that implements "is-a" and/or "has-a" relationships. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">******************************************************************</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial;"><span style="font-size: 100%;"><b>Section 6: Collections / Generics </b></span></div><div class="MsoNormal" style="color: #000099; font-family: arial;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">a)</span><span style="font-size: 100%;">Given a design scenario, determine which collection classes and/or interfaces should be used to properly implement that design, including the use of the Comparable interface. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">b)</span><span style="font-size: 100%;">Distinguish between correct and incorrect overrides of corresponding hashCode and equals methods, and explain the difference between == and the equals method. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">c)</span><span style="font-size: 100%;">Write code that uses the generic versions of the Collections API, in particular, the Set, List, and Map interfaces and implementation classes. Recognize the limitations of the non-generic Collections API and how to refactor code to use the generic versions. Write code that uses the NavigableSet and NavigableMap interfaces. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">d)</span><span style="font-size: 100%;">Develop code that makes proper use of type parameters in class/interface declarations, instance variables, method arguments, and return types; and write generic methods or methods that make use of wildcard types and understand the similarities and differences between these two approaches. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">e)</span><span style="font-size: 100%;">Use capabilities in the java.util package to write code to manipulate a list by sorting, performing a binary search, or converting the list to an array. Use capabilities in the java.util package to write code to manipulate an array by sorting, performing a binary search, or converting the array to a list. Use the java.util.Comparator and java.lang.Comparable interfaces to affect the sorting of lists and arrays. Furthermore, recognize the effect of the "natural ordering" of primitive wrapper classes and java.lang.String on sorting. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">******************************************************************</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 19.5pt;"><span style="font-size: 100%;"><b>Section 7: Fundamentals </b></span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 19.5pt;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">a)</span><span style="font-size: 100%;">Given a code example and a scenario, write code that uses the appropriate access modifiers, package declarations, and import statements to interact with (through access or inheritance) the code in the example. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">b)</span><span style="font-size: 100%;">Given an example of a class and a command-line, determine the expected runtime behavior. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">c)</span><span style="font-size: 100%;">Determine the effect upon object references and primitive values when they are passed into methods that perform assignments or other modifying operations on the parameters. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">d)</span><span style="font-size: 100%;">Given a code example, recognize the point at which an object becomes eligible for garbage collection, determine what is and is not guaranteed by the garbage collection system, and recognize the behaviors of the Object.finalize() method. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">e)</span><span style="font-size: 100%;">Given the fully-qualified name of a class that is deployed inside and/or outside a JAR file, construct the appropriate directory structure for that class. Given a code example and a classpath, determine whether the classpath will allow the code to compile successfully. </span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">f)</span><span style="font-size: 100%;">Write code that correctly applies the appropriate operators including assignment operators (limited to: =, +=, -=), arithmetic operators (limited to: +, -, *, /, %, ++, --), relational operators (limited to: <, <=, >, >=, ==, !=), the instanceof operator, logical operators (limited to: &, |, ^, !, &&, ||), and the conditional operator ( ? : ), to produce a desired result. Write code that determines the equality of two objects or two primitives.</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;"><br />
</span></div><div class="MsoNormal" style="color: #000099; font-family: arial; margin-left: 37.5pt; text-indent: -0.25in;"><span style="font-size: 100%;">****************************************************************** </span></div>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-90713670954317357042009-03-12T12:53:00.000-07:002010-07-18T03:59:05.925-07:00Important Java interview Questions<span style="color: rgb(0, 0, 153);font-size:100%;" ><span style="font-weight: bold;font-family:arial;" >Q1. What is the SimpleTimeZone class?</span><br /><span style="font-family:arial;">The SimpleTimeZone class provides support for a Gregorian calendar.</span><br /><br /><span style="font-weight: bold;font-family:arial;" >Q2. What is the difference between a while statement and a do statement?</span><br /><span style="font-family:arial;">A while statement (pre test) checks at the beginning of a loop to see whether the next loop iteration should occur. A do while statement (post test) 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 loop body at least once.</span><br /><br /><span style="font-weight: bold;font-family:arial;" >Q3.What is the Locale class? </span><br /><span style="font-family:arial;">The Locale class is used to tailor a program output to the conventions of a particular geographic, political, or cultural region.</span><br /><br /><span style="font-weight: bold;font-family:arial;" >Q4.Describe the principles of OOPS.</span><br /><span style="font-family:arial;">There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation. </span><br /><br /><span style="font-weight: bold;font-family:arial;" >Q5. Explain the Inheritance principle.</span><br /><span style="font-family:arial;">Inheritance is the process by which one object acquires the properties of another object. Inheritance allows well-tested procedures to be reused and enables changes to make once and have effect in all relevant places.</span><br /><br /><span style="font-weight: bold;font-family:arial;" >Q6.What is implicit casting?</span><br /><span style="font-family:arial;">Implicit casting is the process of simply assigning one entity to another without any transformation guidance to the compiler. This type of casting is not permitted in all kinds of transformations and may not work for all scenarios.</span><br /><span style="font-family:arial;">Example</span><br /><span style="font-family:arial;">int i = 1000;</span><br /><span style="font-family:arial;">long j = i; //Implicit casting</span><br /><span style="font-family:arial;">Is sizeof a keyword in java?</span><br /><span style="font-family:arial;">The sizeof operator is not a keyword. </span><br /><br /><span style="font-weight: bold;font-family:arial;" >Q7.What is a native method?</span><br /><span style="font-family:arial;">A native method is a method that is implemented in a language other than Java.</span><br /><span style="font-family:arial;">In System.out.println(), what is System, out and println?</span><br /><span style="font-family:arial;">System is a predefined final class, out is a PrintStream object and println is a built-in overloaded method in the out object.</span><br /><br /><span style="font-weight: bold;font-family:arial;" >Q8.What are Encapsulation, Inheritance and Polymorphism</span><br /><span style="font-family:arial;">Or</span><br /><span style="font-weight: bold;font-family:arial;" >Explain the Polymorphism principle. Explain the different forms of Polymorphism.</span><br /><span style="font-family:arial;">Polymorphism in simple terms means one name many forms. Polymorphism enables one entity to be used as a general category for different types of actions. The specific action is determined by the exact nature of the situation.</span><br /><span style="font-family:arial;">Polymorphism exists in three distinct forms in Java: </span><br /><span style="font-family:arial;">• Method overloading </span><br /><span style="font-family:arial;">• Method overriding through inheritance </span><br /><span style="font-family:arial;">• Method overriding through the Java interface </span><br /><br /></span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com1tag:blogger.com,1999:blog-7863722189611643831.post-6701082497248065012009-02-11T01:41:00.000-08:002010-07-18T03:50:22.809-07:00Important Java interview Questions<meta equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 10"><meta name="Originator" content="Microsoft Word 10"><link style="font-family: arial; color: rgb(0, 0, 153);" rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C02%5Cclip_filelist.xml"><!--[if gte mso 9]><xml> <w:worddocument> <w:view>Normal</w:View> <w:zoom>0</w:Zoom> <w:compatibility> <w:breakwrappedtables/> <w:snaptogridincell/> <w:wraptextwithpunct/> <w:useasianbreakrules/> </w:Compatibility> <w:browserlevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} @font-face {font-family:Calibri; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin-top:0in; margin-right:0in; margin-bottom:10.0pt; margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:Calibri; mso-fareast-font-family:"Times New Roman"; mso-bidi-font-family:"Times New Roman";} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:1867788033; mso-list-template-ids:1569472154;} @list l0:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in; mso-ansi-font-size:10.0pt; font-family:Symbol;} ol {margin-bottom:0in;} ul {margin-bottom:0in;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]--> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">What if the static modifier is removed from the signature of the main method?</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">Or</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">What if I do not provide the String array as the argument to the method?</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style=";font-size:100%;" >Program compiles. But at runtime throws an error "NoSuchMethodError".<o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">Why oracle Type 4 driver is named as oracle thin driver?</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style=";font-size:100%;" >Oracle provides a Type 4 JDBC driver, referred to as the Oracle “thin” driver. This driver includes its own implementation of a TCP/IP version of Oracle’s Net8 written entirely in Java, so it is platform independent, can be downloaded to a browser at runtime, and does not require any Oracle software on the client side. This driver requires a TCP/IP listener on the server side, and the client connection string uses the TCP/IP port address, not the TNSNAMES entry for the database name.<o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">What is the difference between final, finally and finalize? What do you understand by the java final keyword?</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">Or</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">What is final, finalize() and finally?</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">Or</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">What is finalize() method?</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">Or</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">What is the difference between final, finally and finalize?</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">Or</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">What does it mean that a class or member is final? </span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style=";font-size:100%;" >o <b>final</b> - declare constant
<br />o <b>finally</b> - handles exception
<br />o <b>finalize</b> - helps in garbage collection<o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style=";font-size:100%;" >Variables defined in an interface are implicitly final. A final class can't be extended i.e., final class may not be subclassed. 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. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant). finalize() method is used just before an object is destroyed and garbage collected. finally, a key word used in exception handling and will be executed whether or not an exception is thrown. For example, closing of open connections is done in the finally method.<o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">What is the Java API?</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style=";font-size:100%;" >The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets.<o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">What is the GregorianCalendar class?</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style=";font-size:100%;" >The GregorianCalendar provides support for traditional Western calendars.<o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">What is the ResourceBundle class?</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style=";font-size:100%;" >The ResourceBundle class is used to store locale-specific resources that can be loaded by a program to tailor the program's appearance to the particular locale in which it is being run. <o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">Why there are no global variables in Java?</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style=";font-size:100%;" >Global variables are globally accessible. Java does not support globally accessible variables due to following reasons: <o:p></o:p></span></p> <ul style="font-family: arial; color: rgb(0, 0, 153);" type="disc"><li class="MsoNormal" style="line-height: normal;"><span style=";font-size:100%;" >The global variables breaks the referential transparency<o:p></o:p></span></li><li class="MsoNormal" style="line-height: normal;"><span style=";font-size:100%;" >Global variables creates collisions in namespace. <o:p></o:p></span></li></ul> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style="font-size:100%;"><b><span style="">How to convert String to Number in java program?</span></b></span><span style=";font-size:100%;" ><o:p></o:p></span></p> <p class="MsoNormal" style="line-height: normal; font-family: arial; color: rgb(0, 0, 153);"><span style=";font-size:100%;" >The valueOf() function of Integer class is is used to convert string to Number. Here is the code example:
<br />String numString = "1000";
<br />int id=Integer.valueOf(numString).intValue(); <o:p></o:p></span></p> javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-15403019223747950412009-01-18T03:34:00.000-08:002010-07-18T03:40:29.460-07:00Java Interview Questions<span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >Q1. What if the main method is declared as private? </span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >The program compiles properly but at runtime it will give "Main method not public." message.</span><br /><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >Q2. What is meant by pass by reference and pass by value in Java?</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >Pass by reference means, passing the address itself rather than passing the value. Pass by value means passing a copy of the value.</span><br /><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >Q3. If you’re overriding the method equals() of an object, which other method you might also consider?</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >hashCode()</span><br /><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >Q4.What is Byte Code?</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >Or</span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >What gives java it’s “write once and run anywhere” nature?</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >All Java programs are compiled into class files that contain bytecodes. These byte codes can be run in any platform and hence java is said to be platform independent.</span><br /><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >Q5.Expain the reason for each keyword of public static void main(String args[])?</span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >public</span><span style="color: rgb(0, 0, 153);font-family:arial;" >- main(..) is the first method called by java environment when a program is executed so it has to accessible from java environment. Hence the access specifier has to be public.</span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >static</span><span style="color: rgb(0, 0, 153);font-family:arial;" >: Java environment should be able to call this method without creating an instance of the class , so this method must be declared as static.</span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >void</span><span style="color: rgb(0, 0, 153);font-family:arial;" >: main does not return anything so the return type must be void</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >The argument String indicates the argument type which is given at the command line and arg is an array for string given during command line.</span><br /><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >Q6.What are the differences between == and .equals() ?</span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >Or</span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >what is difference between == and equals </span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >Or</span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >Difference between == and equals method</span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >Or</span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >What would you use to compare two String variables - the operator == or the method equals()?</span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >Or</span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >How is it possible for two String objects with identical values not to be equal under the == operator?</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >The == operator compares two objects to determine if they are the same object in memory i.e. present in the same memory location. It is possible for two String objects to have the same value, but located in different areas of memory.</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >== compares references while .equals compares contents. The method public boolean equals(Object obj) is provided by the Object class and can be overridden. The default implementation returns true only if the object is compared with itself, which is equivalent to the equality operator == being used to compare aliases to the object. String, BitSet, Date, and File override the equals() method. For two String objects, value equality means that they contain the same character sequence. For the Wrapper classes, value equality means that the primitive values are equal.</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > public class EqualsTest {</span><br /><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > public static void main(String[] args) {</span><br /><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > String s1 = "abc";</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > String s2 = s1;</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > String s5 = "abc";</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > String s3 = new String("abc");</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > String s4 = new String("abc");</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > System.out.println("== comparison : " + (s1 == s5));</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > System.out.println("== comparison : " + (s1 == s2));</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > System.out.println("Using equals method : " + s1.equals(s2));</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > System.out.println("== comparison : " + s3 == s4);</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > System.out.println("Using equals method : " + s3.equals(s4));</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" > }</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >}</span><br /><br /><span style="font-weight: bold; color: rgb(0, 0, 153);font-family:arial;" >Output</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >== comparison : true</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >== comparison : true</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >Using equals method : true</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >false</span><br /><span style="color: rgb(0, 0, 153);font-family:arial;" >Using equals method : true</span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-5698017051126840832008-10-13T00:03:00.000-07:002008-12-13T00:16:34.061-08:00Revision Quiz<span style="font-family:arial;color:#000099;">1. A ____ object can store and retrieve “value” objects indexed by “key” object.</span><br /><span style="font-family:arial;color:#000099;">Ans: Hashtable </span><br /><br /><span style="font-family:arial;color:#000099;">2.Wrapper class objects contain mutable values. </span><br /><span style="font-family:arial;color:#000099;">True/False?</span><br /><span style="font-family:arial;color:#000099;">Ans: False </span><br /><br /><span style="font-family:arial;color:#000099;">3.A Vector can hold object references or primitives types.</span><br /><span style="font-family:arial;color:#000099;">Ans: False </span><br /><br /><span style="font-family:arial;color:#000099;">4.Hashtable class implements which of the following interfaces?Ans: Map</span><br /><span style="font-family:arial;color:#000099;"><br />5.String s1=new String(“MyString”);String s2=new String(“MyString”);s1==s2 will return ___.</span><br /><span style="font-family:arial;color:#000099;">Ans: False </span><br /><br /><span style="font-family:arial;color:#000099;">6.Float.NaN or Double.NaN refers to ___</span><br /><span style="font-family:arial;color:#000099;">Ans: Not a number</span><br /><span style="font-family:arial;color:#000099;"><br />7.The java.lang.System is a ___ class.</span><br /><span style="font-family:arial;color:#000099;">Ans: final</span><br /><span style="font-family:arial;color:#000099;"><br />8.The ___ class is a wrapper around int data type in java.</span><br /><span style="font-family:arial;color:#000099;">Ans: Integer </span><br /><br /><span style="font-family:arial;color:#000099;">9.java.lang.Double is a concrete subclass of abstract class java.lang.Number.</span><br /><span style="font-family:arial;color:#000099;">True/False</span><br /><span style="font-family:arial;color:#000099;">Ans:True </span><br /><br /><span style="font-family:arial;color:#000099;">10.What happens when you try to compile and run the following code snippet?</span><br /><span style="font-family:arial;color:#000099;">public class Wrapper{</span><br /><span style="font-family:arial;color:#000099;">public static void main(String []a)</span><br /><span style="font-family:arial;color:#000099;">{</span><br /><span style="font-family:arial;color:#000099;">String s=”15.25”;</span><br /><span style="font-family:arial;color:#000099;">try {</span><br /><span style="font-family:arial;color:#000099;">int number=Integer.parseInt(s); //line 1</span><br /><span style="font-family:arial;color:#000099;">System.out.println(number); //line 2</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">catch(NumberFormatException nfe)</span><br /><span style="font-family:arial;color:#000099;">{</span><br /><span style="font-family:arial;color:#000099;">System.out.println(“sorry”);</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">catch(Exception e){ }</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">A) It will compile fine and display 15 when run.</span><br /><span style="font-family:arial;color:#000099;">B) It will compile fine and display Sorry when run.</span><br /><span style="font-family:arial;color:#000099;">C) It will not compile.</span><br /><span style="font-family:arial;color:#000099;">D) It will compile fine nothing will be displayed when run</span><br /><span style="font-family:arial;color:#000099;">Ans: B</span><br /><span style="font-family:arial;color:#000099;"><br />11.The java.lang.Math class has ___ constructor.</span><br /><span style="font-family:arial;color:#000099;">A) public</span><br /><span style="font-family:arial;color:#000099;">B) private</span><br /><span style="font-family:arial;color:#000099;">C) protected</span><br /><span style="font-family:arial;color:#000099;">Ans: B </span><br /><br /><span style="font-family:arial;color:#000099;">12.A Vector maintains object references in the order they were added.</span><br /><span style="font-family:arial;color:#000099;">Ans: True</span><br /><span style="font-family:arial;color:#000099;"><br />13.The String class represents a mutable string</span><br /><span style="font-family:arial;color:#000099;">Ans: False </span><br /><br /><span style="font-family:arial;color:#000099;">14.The parent class of both Integer and Character class is Number class</span><br /><span style="font-family:arial;color:#000099;">Ans: False</span><br /><span style="font-family:arial;color:#000099;"><br />15.What happens when you try to compile and run the following code snippet?</span><br /><span style="font-family:arial;color:#000099;">public class WrapChar</span><br /><span style="font-family:arial;color:#000099;">{</span><br /><span style="font-family:arial;color:#000099;">public static void main(String []a)</span><br /><span style="font-family:arial;color:#000099;">{</span><br /><span style="font-family:arial;color:#000099;">Character wrapChar=new Character(“c”);//line1</span><br /><span style="font-family:arial;color:#000099;">System.out.println(wrapChar);//line 2</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">A) Compile time error at line 1</span><br /><span style="font-family:arial;color:#000099;">B) Compile time error at line 2</span><br /><span style="font-family:arial;color:#000099;">C) Compile fine and display c when run</span><br /><span style="font-family:arial;color:#000099;">D) Compile fine but throws RuntimeException</span><br /><span style="font-family:arial;color:#000099;">Ans: A</span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com1tag:blogger.com,1999:blog-7863722189611643831.post-36081014715292643762008-09-09T02:03:00.000-07:002008-10-09T03:52:23.493-07:00LAST MINUTE REVISION NOTES<span style="color:#000099;"><em><span style="font-family:arial;font-size:100%;">The tips to attend the exam SCJP 5</span></em><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">1. Check for any obvious complier errors first like two else statements , accessing ofprivate variable/method , accessing a variable declared in a for loop outside the for loop. </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">2. If you try to unbox a null Integer value to int , it will result in a NullPointerException. </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">3. If method having the same name as Class name and having a return type is not a constructor but a ordinary method.</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">4. Map is the only interface that does not extend Collection. List , Set and Queue extends the Collection interface.</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">Collection is a interface , </span><br /><span style="font-family:arial;font-size:100%;">Collections is Class which extends Object class. </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">Map: key , value pair</span><br /><span style="font-family:arial;font-size:100%;">Hashtable , HashMap , LinkedHashMap , TreeMap </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">Set : unique values </span><br /><span style="font-family:arial;font-size:100%;">HashSet , TreeSet , LinkedHashSet</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">List : List of values allows duplicates, accessed by index</span><br /><span style="font-family:arial;font-size:100%;">ArrayList , Vector , LinkedList</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">Queue : stack or queue. to-do list. priority queue is sorted based on the natural order or based on the object of Comparator or Comparable.LinkedList , PriorityQueue </span><br /><span style="font-family:arial;font-size:100%;">The twins clases.</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">a. The Hashtable and HashMap: Both are atmost same except that Hashtable methods are synchronized and it does not allow null key/value, whereas HashMap allows one null key and many null values and its methods are not synchronized . The Hashtable is oldest class in java. The t in Hashtable is indeed a small letter rather than capital letter.</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">b. The Vector and ArrayList have same functionalities except that Vector Methods are synchronized , while ArrayList methods are not. Both ArrayList and Vector implementes RandomAccess interface (which is marker interface having no methods) as of java 5</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">c. The LinkedList can be used to access values in insertion order. The LinkedHashMap can be used access values in insertion or access order. The LinkedList implements Queue(as of java 5) and List. The LinkedList now implements queue methods like offer, poll and peek.</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">d. The TreeSet , TreeMap and PriorityQueue sorts the values.</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">e. The HashSet , Hashtable and HashMap are unordered and unsorted. if you add n values in these Collection objects , iteration will not give you a particular ordering ie no insertion or access or natural order. It iterates in a random order.</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">f. The LinkedHashSet is ordered form of HashSet , The LinkedHashMap is the ordered form of HashMap. </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">g. the offer() method adds a element in queue. the poll method returns the first method and removes it from queue. the peek just returns the first method from queue. The higgest priority is stored first in PriorityQueue. if you poll each value from PriorityQueue then values returned will be of Higgest proririty to lowest priorirty.</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">h. The Object of class which need to be added in HashSet , Hashtable , LinkedHashSet or HashMapt must override the below methods </span><br /><span style="font-family:arial;font-size:100%;">The exception are all Wrapper classes. The class Number , StringBuffer and StringBuilder.</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">For example object of Student class need to be added in LinkedHashSetA student class contains id , name. As id is a primaray key , we should not have rwo students with the same id. if the Student class does not override the above methods then</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">Student d = new Student(44); //44 is id</span><br /><span style="font-family:arial;font-size:100%;">Student d 1= new Student(44); //44 is id</span><br /><span style="font-family:arial;font-size:100%;">public int hashCode(){}</span><br /><span style="font-family:arial;font-size:100%;">public boolean equals(Object o){}</span><br /><span style="font-family:arial;font-size:100%;">public boolean equals(Object o { if (o instanceof Student) {</span><br /><span style="font-family:arial;font-size:100%;">Student t= (Student) o; </span><br /><span style="font-family:arial;font-size:100%;">if(o.getId() ==this.id ) { </span><br /><span style="font-family:arial;font-size:100%;">return true; </span><br /><span style="font-family:arial;font-size:100%;">} //if </span><br /><span style="font-family:arial;font-size:100%;">} //ifreturn false;</span><br /><span style="font-family:arial;font-size:100%;">} //method</span><br /><span style="font-family:arial;font-size:100%;">public int hashCode() {</span><br /><span style="font-family:arial;font-size:100%;">//use any of the below </span><br /><span style="font-family:arial;font-size:100%;">return id*4; </span><br /><span style="font-family:arial;font-size:100%;">return id; </span><br /><span style="font-family:arial;font-size:100%;">return 1038; //any constant</span><br /><span style="font-family:arial;font-size:100%;">//do no use any of the below</span><br /><span style="font-family:arial;font-size:100%;">Random r =new Random(44);</span><br /><span style="font-family:arial;font-size:100%;">return r.nextInt();</span><br /><span style="font-family:arial;font-size:100%;">//here t is transient variable </span><br /><span style="font-family:arial;font-size:100%;">return t*id;</span><br /><span style="font-family:arial;font-size:100%;">}</span><br /><span style="font-family:arial;font-size:100%;">if above method is implemented then d and d1 will be same object.</span><br /><span style="font-family:arial;font-size:100%;">both d and d1 are different object accoring to the LinkedHashSet , as a result both will be added to the Set</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">i. The Object of class which need to be added in TreeMap , TreeSet and PriorityQueue must implement Comparable or Comparator interface</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">j. java.lang.Comparabl e declaresompareTo( ) and accepts one Object as argument.k. java.util.Comparato r declares compare() which accepts 2 Object as argument.</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">l. in generics if you declare a variable <integer>it must be instantiated with the same type or no type at all</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">//valid</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">Listlisto1 =new ArrayList ();</span><br /><span style="font-family:arial;font-size:100%;">List </span>listo2 =new ArrayList();</span><br /><span style="color:#000099;"><span style="font-family:arial;font-size:100%;">List listo3 =new ArrayList </span>();</span><br /><span style="color:#000099;"><span style="font-family:arial;font-size:100%;">List<string> list3 =new ArrayList<string>();</span><br /><span style="font-family:arial;font-size:100%;">m1(list01);/ /error</span><br /><span style="font-family:arial;font-size:100%;">m2(list01);/ /ok</span><br /><span style="font-family:arial;font-size:100%;">m1(list02);/ /error</span><br /><span style="font-family:arial;font-size:100%;">m2(list02);/ /ok</span><br /><span style="font-family:arial;font-size:100%;">m1(list03);/ /ok</span><br /><span style="font-family:arial;font-size:100%;">m2(list03);/ /ok</span><br /><span style="font-family:arial;font-size:100%;">m1(list3);// ok</span><br /><span style="font-family:arial;font-size:100%;">m2(list3);// error</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">//invalid</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">List </span>listo =new ArrayList<string>(); //compiler error</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="color:#000099;"><span style="font-family:arial;font-size:100%;">// functions </span><br /><span style="font-family:arial;font-size:100%;">static void m1(<? extends String> a) {}</span><br /><span style="font-family:arial;font-size:100%;">static void m2( </span>a) {} </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="color:#000099;"><span style="font-family:arial;font-size:100%;">//The argument that Child class can be applied to a parent class i s not at all applicable to generics . if you specified a type in the variable declaration it must be followed while instantiating also. </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">In the Exam , there will be drag-drop quetion . in which there will be 4 variable instanciated with the genreic and passed as arguemnt to a function. you have drag and drop , if the code compiles or not.</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">m. </span><br /><span style="font-family:arial;font-size:100%;">public class <?>{} and</span><br /><span style="font-family:arial;font-size:100%;">public class <? extends Number>{}</span><br /><span style="font-family:arial;font-size:100%;">are invalid syntax and leads to compiler error </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">but it can be defined as </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">public class <t>{} or</span><br /><span style="font-family:arial;font-size:100%;">public class <t>{} </span><br /><span style="font-family:arial;font-size:100%;">public class <t,f>{} public class <t>{}</span><br /><span style="font-family:arial;font-size:100%;">public <t>T getObject() {} //correct</span><br /><span style="font-family:arial;font-size:100%;">public <t>T computeArrays( Y y) {} </span><br /><span style="font-family:arial;font-size:100%;">//correct, the font-size of s is increased to differentiate from the interface Collection</span><br /><span style="font-family:arial;font-size:100%;">//here Y is the argument and T is the return type.</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">5. For garbage collection , you must focus on the value and its reference . there may be a question . which is earliest line in which the value referred by object in line 5(some line) is eligible for garbage collection . The reference variable will be given other values in the consecutive steps but focus if the value which was created/defined in line 5 (example) is referenced by any other variable. The variable can be instance or local variable. if it is a local variable , its scope will be till the method ends but except in the situation the value is returned.</span><br /><span style="font-family:arial;font-size:100%;">For example below :</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">Object o= new Object//line 5</span><br /><span style="font-family:arial;font-size:100%;">Object s =o;//line 6</span><br /><span style="font-family:arial;font-size:100%;">// the value created in line 5 is referred by s</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">case 1 . </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">o = new Object(); //7 //o is set to different value </span><br /><span style="font-family:arial;font-size:100%;">s=null; //8 s is null or </span><br /><span style="font-family:arial;font-size:100%;">s = new Object(); // s is set to a new value </span><br /><span style="font-family:arial;font-size:100%;">}</span><br /><span style="font-family:arial;font-size:100%;">so old value has no reference. </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">here the value created in line 5 will be eligible for gc after line 5</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">case 2. </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">o= new Object();//5 </span><br /><span style="font-family:arial;font-size:100%;">Object s =o;//6 </span><br /><span style="font-family:arial;font-size:100%;">o = new Object();//7 </span><br /><span style="font-family:arial;font-size:100%;">return s;//8 }</span><br /><span style="font-family:arial;font-size:100%;">The value created in line 5 will not be eligible for garbage collection even after the method has ended as it is returned. </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">case 3: </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">private Object o; </span><br /><span style="font-family:arial;font-size:100%;">public void data() { </span><br /><span style="font-family:arial;font-size:100%;">Object o = new Object();//1 </span><br /><span style="font-family:arial;font-size:100%;">doSomething( o);//2 </span><br /><span style="font-family:arial;font-size:100%;">o = new Object();//3 </span><br /><span style="font-family:arial;font-size:100%;">o=null;//4 </span><br /><span style="font-family:arial;font-size:100%;">doSomething( null);//5 </span><br /><span style="font-family:arial;font-size:100%;">Object s = new Object();//6 </span><br /><span style="font-family:arial;font-size:100%;">Object nothing = new Object();//7 </span><br /><span style="font-family:arial;font-size:100%;">return s;//8 </span><br /><span style="font-family:arial;font-size:100%;">}//9</span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">In the above example value created in line 1 is eligible for garbage collection after the line 5. The value created in the line 3 is eligible for garbage collection after the line 4. </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">The value created in line 6 will not be eligible for garbage collection even after the method ends as the value is returned. The value created in line 7 is eligible for garbage collection after the line 9. </span><br /><span style="font-family:arial;font-size:100%;"></span><br /><span style="font-family:arial;font-size:100%;">public void doSomething( Object obj) { </span><br /><span style="font-family:arial;font-size:100%;">o= obj; </span><br /><span style="font-family:arial;font-size:100%;">}</span><br /></span></span></span></span></span><div></div></span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-5334335500428559562008-08-09T22:50:00.000-07:002008-08-09T23:28:30.627-07:00Serialization and Deserialization Important Tips<p><br /><span style="color:#3333ff;">**For an object to be serialized, it has to implement either the <strong>java.io.Serializable</strong> or <strong>java.io.Externalizable</strong> interfaces, the latter being a subtype of Serializable. </span></p><p><span style="color:#3333ff;">---------------------------------------------------------------------</span></p><p><span style="color:#3333ff;">**Serializable is a <strong>marker interface</strong>: it specifies no methods and merely indicates an object that has serializable state. </span></p><p><span style="color:#3333ff;">-------------------------------------------------------------</span></p><p><span style="color:#3333ff;">**All <strong>subclasses of a Serializable</strong> class are also serializable. </span></p><p><span style="color:#3333ff;">--------------------------------------------------------------<br /><br />**If a supertype of a Serializable object is not itself serializable, then the object can assume the responsibility for saving and reconstructing the supertype's state (public, protected, and package fields if they share a package).For this to work, the superclass must have a <strong>public no-arg constructor</strong>. </span></p><p><span style="color:#3333ff;">--------------------------------------------------------------</span></p><p><span style="color:#3333ff;">**Further, if an object at runtime refers to a nonserializable object, the serialization system will throw a <strong>NotSerializableException</strong>, since it cannot write the complete object graph to the serialized stream. </span></p><p><span style="color:#3333ff;">--------------------------------------------------------------<br /><br />**If an object holds <strong>multiple references</strong> to another object, this second object is serialized only once, and subsequent references to it will include a handle as a reference instead.</span></p><p><span style="color:#3333ff;">---------------------------------------------------------------<br /><br />**Along with instance data, the <strong>object serialization system</strong> writes a special object to the stream to represent the serializable object's class.</span></p><p><span style="color:#3333ff;">--------------------------------------------------------------- </span></p><p><span style="color:#3333ff;">**This object is of the type <strong>java.io.ObjectStreamClass</strong>, and is essentially a descriptor for the Class object associated with the serializable object. It contains the class's name, its unique version number (serialVersionUID), and the class fields. </span></p><p><span style="color:#3333ff;">---------------------------------------------------------------</span></p><p><span style="color:#3333ff;">**In addition, it also has methods to obtain the actual class represented by this object, if, and only if, this class is already present in the local VM: </span></p><p><span style="color:#3333ff;"><strong>Class forClass() : </strong>If there is no class identified in the local VM that corresponds to this ObjectStreamClass, then a null value is returned.</span></p><p><span style="color:#3333ff;">------------------------------------------------------------------<br /></span><span style="color:#3333ff;"><br /><strong>**Not all objects are suitable for serialization:</strong> </span></p><p><span style="color:#3333ff;">1.Threads, for instance, do not have state that can later be recreated. (Actually, it is possible, but very difficult, to do so.) </span></p><p><span style="color:#3333ff;">2.There can also be objects that should not be serialized for semantic reasons. Even if an object is serializable, you may not need to write all object fields to the stream during serialization. For instance, a variable that loses its meaning in a different execution context (such as something indicating the current time) should be marked transient, and will instead be initialized to its default value when reading it from the stream. </span></p><p><span style="color:#3333ff;">----------------------------------------------------------------<br /></span><span style="color:#3333ff;"><br />**If instances of a class need a special way to serialize their state, that class can implement the <strong>Externalizable interface</strong>, which mandates two methods:<br />void readExternal(ObjectInput inputStream);</span></p><p><span style="color:#3333ff;">void writeExternal(ObjectOutput outputStream);</span></p><p><span style="color:#3333ff;">-------------------------------------------------------------</span></p><p><span style="color:#3333ff;">**The main <strong>difference between Externalizable and Serializable</strong> is that the latter serializes, by default, the entire object graph, including states of an object's superclass. While in the former Only the identity of the class of an Externalizable instance is written in the serialization stream and it is the responsibility of the class to save and restore the contents of its instances.<br /></span><span style="color:#3333ff;"></span></p><p><span style="color:#3333ff;">Externalizable gives you complete control over the serialization process.<br /><br />Serialization allows you to create a JVM-independent binary representation of an in-memory Java object. This external representation may be used to transfer or store the object and to recreate it in another JVM. </span></p><p><span style="color:#3333ff;">--------------------------------------------------------------------</span></p><p><span style="color:#3333ff;"><strong>**Two streams in java.io--</strong> </span></p><p><span style="color:#3333ff;">1. ObjectInputStream </span></p><p><span style="color:#3333ff;">2. ObjectOutputStream</span></p><p><span style="color:#3333ff;">-- are run-of-the-mill byte streams and work like the other input and output streams. However, they are special in that they can read and write objects. </span></p><p><span style="color:#3333ff;">----------------------------------------------------------------<br /><br />**Serializable has no methods but <strong>Externalizable has 2 methods</strong>: readExternal() and writeExternal()</span></p><p><span style="color:#3333ff;">----------------------------------------------------------------<br /></span><span style="color:#3333ff;"><br />You can use object serialization in the following ways:<br />· <strong>Remote Method Invocation (RMI)</strong> --communication between objects via sockets<br />· <strong>Lightweight persistence</strong>--the archival of an object for use in a later invocation of the same program.</span></p><p><span style="color:#3333ff;">---------------------------------------------------------------------<br /></p></span><p><strong><span style="color:#3333ff;">**Technique to protect sensitive data in classes :</span></strong></p><p><span style="color:#3333ff;">Mark fields that contain sensitive data as private transient. transient and static fields are not serialized or deserialized. </span></p><p><span style="color:#3333ff;">Marking the field will prevent the state from appearing in the stream and from being restored during deserialization. </span></p><p><span style="color:#3333ff;">Since writing and reading (of private fields) cannot be superseded outside of the class, the class's transient fields are safe.</span> </p><p><span style="color:#3333ff;">-------------------------------------------------------------<br /></span><br /></p>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-77466336047432769092008-08-09T06:05:00.000-07:002008-08-09T22:38:54.904-07:00Accessor Types Public and Private<strong><span style="color:#3333ff;">Question 1 </span></strong><br /><span style="color:#3333ff;"></span><br /><span style="color:#3333ff;">Given :</span><br /><br /><span style="color:#3333ff;">public class Hello{<br />private int i = j;<br />private int j = 10;<br />public static void main(String args[]) {<br />System.out.println((new Hello()).i); }<br />}<br /><br />Choose</span><br /><span style="color:#3333ff;">1. Compiler error complaining about access restriction of private variables of Hello.<br />2. Compiler error complaining about forward referencing.<br />3. No error - The output is 0;<br />4. No error - The output is 10;<br /><br /><strong>Answer is 2. It is because i is a private variable of Hello. Hence it cannot be accessed directly.</strong><br /><br /><strong>Question 2</strong> </span><br /><span style="color:#3333ff;"></span><br /><span style="color:#3333ff;">Given :</span><br /><br /><span style="color:#3333ff;">public class Hello {<br />private int i = giveJ();<br />private int j = 10;<br />private int giveJ()<br />{ return j;<br />}<br />public static void main(String args[])<br />{ System.out.println((new Hello()).i);<br />}<br />}<br /></span><br /><span style="color:#3333ff;">Choose :<br />1. Compiler error complaining about access restriction of private variables of Hello.<br />2. Compiler error complaining about forward referencing. .<br />3. No Compilation error - The output is 0;<br />4. No Compilation error - The output is 10;</span><br /><span style="color:#3333ff;"></span><br /><strong><span style="color:#3333ff;">Answer 2 is correct. We cannot assign a value to instance varaible by calling the method.</span></strong>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com1tag:blogger.com,1999:blog-7863722189611643831.post-33499192092028976992008-07-06T00:26:00.000-07:002008-12-13T01:18:59.275-08:00Inner Classes<span style="font-family:arial;color:#000099;">In Object Oriented programming, for reuse and flexibility/extensibility you need to keep your classes specialized.</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">In other words, a class should have code only for the things an object of that particular type needs to do; any other behavior should be part of another class better suited for that job.</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">Sometimes, though, you find yourself designing a class where you discover you need behavior that belongs in a separate, specialized class, but also needs to be intimately tied to the class you're designing.</span><br /><br /><span style="font-family:arial;color:#000099;">One of the key benefits of an inner class is the "special relationship" an inner class instance shares with an instance of the outer class. </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">That "special relationship" gives code in the inner class access to members of the enclosing (outer) class, as if the inner class were part of the outer class. </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">In fact, that's exactly what it means: the inner class is a part of the outer class. Let's look at each of them.</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;"><strong>Regular Inner Classes</strong> </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">A normal "regular" inner class is declared inside the curly braces of another class, but outside any method or other code block.</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">class MyOuter { </span><br /><span style="font-family:arial;color:#000099;">class MyInner { }</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">An inner class is a full-fledged member of the enclosing (outer) class, so it can be marked with an access modifier as well as the abstract or final modifiers. (Never both abstract and final together— remember that abstract must be subclassed, whereas final cannot be subclassed).</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">An inner class instance shares a special relationship with an instance of the enclosing class. This relationship gives the inner class access to all of the outer class's members, including those marked private.</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">class MyOuter {</span><br /><span style="font-family:arial;color:#000099;">private int x = 7; </span><br /><span style="font-family:arial;color:#000099;">// inner class definition</span><br /><span style="font-family:arial;color:#000099;">class MyInner</span><br /><span style="font-family:arial;color:#000099;">{ public void seeOuter() </span><br /><span style="font-family:arial;color:#000099;">{ // Yes you can access the private variables of enclosing class</span><br /><span style="font-family:arial;color:#000099;">System.out.println("Outer x is " + x); </span><br /><span style="font-family:arial;color:#000099;">} </span><br /><span style="font-family:arial;color:#000099;">} // close inner class definition</span><br /><span style="font-family:arial;color:#000099;">} // close outer class</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">To instantiate an inner class, you must have a reference to an instance of the outer class to tie to the inner class.</span><br /><br /><span style="font-family:arial;color:#000099;">An inner class instance can never stand alone without a direct relationship to an instance of the outer class. </span><br /><span style="font-family:Arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">From code within the enclosing class, you can instantiate the inner class using only the name of the inner class, as follows:</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">MyInner mi = new MyInner();</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">From code outside the enclosing class's instance methods, you can instantiate the inner class only by using both the inner and outer class names, and a reference to the outer class as follows:</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">MyOuter mo = new Myouter();</span><br /><span style="font-family:arial;color:#000099;">MyOuter.MyInner inner = mo.new MyInner();</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">or</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">MyOuter.MyInner inner = new MyOuter().new MyInner();</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">From code within the inner class, the keyword this holds a reference to the inner class instance. </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">To reference the outer this (in other words, the instance of the outer class that this inner instance is tied to) precede the keyword this with the outer class name as follows: </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">MyOuter.this;</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;"><strong>Method Local Inner Classes</strong></span><br /><span style="font-family:Arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">A method-local inner class is defined within a method of the enclosing class.</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">class MyOuter2 {</span><br /><span style="font-family:arial;color:#000099;">private String x = "Outer2";</span><br /><span style="font-family:arial;color:#000099;">void doStuff() {</span><br /><span style="font-family:arial;color:#000099;">class MyInner { </span><br /><span style="font-family:arial;color:#000099;">public void seeOuter() </span><br /><span style="font-family:arial;color:#000099;">{ System.out.println("Outer x is " + x);</span><br /><span style="font-family:arial;color:#000099;">} // close inner class method</span><br /><span style="font-family:arial;color:#000099;">} // close inner class definition }</span><br /><span style="font-family:arial;color:#000099;">// close outer class method doStuff()</span><br /><span style="font-family:arial;color:#000099;">} // close outer class</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">For the inner class(Method - Local Inner Class) to be used, you must instantiate it, and that instantiation must happen within the same method, but after the class definition code.</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">class MyOuter2 {</span><br /><span style="font-family:arial;color:#000099;">private String x = "Outer2"; </span><br /><span style="font-family:arial;color:#000099;">void doStuff() {</span><br /><span style="font-family:arial;color:#000099;">class MyInner { </span><br /><span style="font-family:arial;color:#000099;">public void seeOuter() { </span><br /><span style="font-family:arial;color:#000099;">System.out.println("Outer x is " + x);</span><br /><span style="font-family:arial;color:#000099;">} // close inner class method</span><br /><span style="font-family:arial;color:#000099;">} // close inner class definition</span><br /><span style="font-family:arial;color:#000099;">MyInner mi = new MyInner(); // This line must come</span><br /><span style="font-family:arial;color:#000099;">// after the class</span><br /><span style="font-family:arial;color:#000099;">mi.seeOuter(); </span><br /><span style="font-family:arial;color:#000099;">} // close outer class method doStuff()</span><br /><span style="font-family:arial;color:#000099;">} // close outer class</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">In other words, no other code running in any other method—inside or outside the outer class—can ever instantiate the method-local inner class. </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">Like regular inner class objects, the method-local inner class object shares a special relationship with the enclosing (outer) class object, and can access its private (or any other) members. </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">However, the inner class object cannot use the local variables of the method the inner class is in unless those variables are marked final.This is bcoz the local variables of the method live on the stack, and exist only for the lifetime of the method. </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">You already know that the scope of a local variable is limited to the method the variable is declared in. When the method ends, the stack frame is blown away and the variable is history. But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an instance variable. </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">Because the local variables aren't guaranteed to be alive as long as the method-local inner class object, the inner class object can't use them. Unless the local variables are marked final! The only modifiers you can apply to a method-local inner class are abstract and final. (Never both at the same time, though.)</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">A local class declared in a static method has access to only static members of the enclosing class, since there is no associated instance of the enclosing class. If you're in a static method there is no this, so an inner class in a static method is subject to the same restrictions as the static method. In other words, no access to instance variables.</span><br /><span style="font-family:arial;color:#000099;"></span><br /><strong><span style="font-family:arial;color:#000099;">Anonymous Inner Classes</span></strong><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">Anonymous inner classes have no name, and their type must be either a subclass of the named type or an implementer of the named interface.</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;font-size:85%;color:#000099;"><strong>Type - 1 (Subclass of the named type)</strong></span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">class Popcorn { </span><br /><span style="font-family:arial;color:#000099;">public void pop() {</span><br /><span style="font-family:arial;color:#000099;">System.out.println("popcorn");</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">}class Food { </span><br /><span style="font-family:arial;color:#000099;">Popcorn p = new Popcorn()</span><br /><span style="font-family:arial;color:#000099;">{</span><br /><span style="font-family:arial;color:#000099;">public void pop() {</span><br /><span style="font-family:arial;color:#000099;">System.out.println("anonymous popcorn");</span><br /><span style="font-family:arial;color:#000099;">} </span><br /><span style="font-family:arial;color:#000099;">};</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">Polymorphism is in play when anonymous inner classes are involved. You can only call methods on an anonymous inner class reference that are defined in the reference variable type! </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">This is no different from any other polymorphic references, for example,</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">class Horse extends Animal{ </span><br /><span style="font-family:arial;color:#000099;">void buck() { }</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">class Animal { </span><br /><span style="font-family:arial;color:#000099;">void eat() { }</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">class Test {</span><br /><span style="font-family:arial;color:#000099;">public static void main (String[] args)</span><br /><span style="font-family:arial;color:#000099;">{ </span><br /><span style="font-family:arial;color:#000099;">Animal h = new Horse(); </span><br /><span style="font-family:arial;color:#000099;">h.eat(); // Legal, class Animal has an eat() method</span><br /><span style="font-family:arial;color:#000099;">h.buck(); // Not legal! Class Animal doesn't have buck() </span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;"></span><br /><strong><span style="font-size:85%;"><span style="font-family:arial;color:#000099;">Type - 2 (Implementer of the specified interface type)</span><br /></span><span style="font-family:arial;color:#000099;"></span></strong><br /><span style="font-family:arial;color:#000099;">interface Cookable {</span><br /><span style="font-family:arial;color:#000099;">public void cook();</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">class Food {</span><br /><span style="font-family:arial;color:#000099;">Cookable c = new Cookable() </span><br /><span style="font-family:arial;color:#000099;">{ public void cook() </span><br /><span style="font-family:arial;color:#000099;">{ System.out.println("anonymous cookable implementer"); </span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">};</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">Anonymous interface implementers can implement only one interface.</span><br /><span style="font-family:arial;color:#000099;"></span><br /><strong><span style="font-size:85%;"><span style="font-family:arial;color:#000099;">Type - 3 (Argument defined anonymous inner classes)</span><br /></span><span style="font-family:arial;color:#000099;"></span></strong><br /><span style="font-family:arial;color:#000099;">class MyWonderfulClass { </span><br /><span style="font-family:arial;color:#000099;">void go() {</span><br /><span style="font-family:arial;color:#000099;">Bar b = new Bar(); </span><br /><span style="font-family:arial;color:#000099;">b.doStuff(new Foo() {</span><br /><span style="font-family:arial;color:#000099;">public void foof() { System.out.println("foofy");</span><br /><span style="font-family:arial;color:#000099;">} // end foof method</span><br /><span style="font-family:arial;color:#000099;">}); // end inner class def, arg, and b.doStuff stmt.</span><br /><span style="font-family:arial;color:#000099;">} // end go() </span><br /><span style="font-family:arial;color:#000099;">} // end class </span><br /><span style="font-family:arial;color:#000099;">interface Foo { </span><br /><span style="font-family:arial;color:#000099;">void foof();</span><br /><span style="font-family:arial;color:#000099;">} </span><br /><span style="font-family:arial;color:#000099;">class Bar { </span><br /><span style="font-family:arial;color:#000099;">void doStuff(Foo f) {}</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">An argument-local inner class is declared, defined, and automatically instantiated as part of a method invocation. </span><br /><span style="font-family:arial;color:#000099;">The key to remember is that the class is being defined within a method argument, so the syntax will end the class definition with a curly brace, followed by a closing parenthesis to end the method call, followed by a semicolon to end the statement: });</span><br /><br /><span style="font-family:arial;color:#000099;">An anonymous inner class is always created as part of a statement; don't forget to close the statement after the class definition with a curly brace. </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">This is a rare case in Java, a curly brace followed by a semicolon.Because of polymorphism, the only methods you can call on an anonymous inner class reference are those defined in the reference variable class (or interface), even though the anonymous class is really a subclass or implementer of the reference variable type.</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">An anonymous inner class can extend one subclass(type-1) or implement one interface(type-2), Unlike non-anonymous classes (inner or otherwise), an anonymous inner class cannot do both. In other words, it cannot both extend a class and implement an interface, nor can it implement more than one interface.</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;font-size:85%;color:#000099;"><strong>Static Nested Classes</strong></span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">Static nested classes are inner classes marked with the static modifier.</span><br /><br /><span style="font-family:arial;color:#000099;">class BigOuter { </span><br /><span style="font-family:arial;color:#000099;">static class Nested { }</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">A static nested class is not an inner class, it's a top-level nested class.The class itself isn't really "static"; there's no such thing as a static class. The static modifier in this case says that the nested class is a static member of the outer class. </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">That means it can be accessed, as with other static members, without having an instance of the outer class. Because the nested class is static, it does not share any special relationship with an instance of the outer class. </span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">In fact, you don't need an instance of the outer class to instantiate a static nested class.Instantiating a static nested class requires using both the outer and nested class names as follows:</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">BigOuter.Nested n = new BigOuter.Nested();</span><br /><span style="font-family:arial;color:#000099;"></span><br /><em><span style="font-family:arial;color:#000099;">Example :</span><br /><span style="font-family:arial;color:#000099;"></span></em><br /><span style="font-family:arial;color:#000099;">class BigOuter { </span><br /><span style="font-family:arial;color:#000099;">static class Nest {void go() </span><br /><span style="font-family:arial;color:#000099;">( System.out.println("hi"); </span><br /><span style="font-family:arial;color:#000099;">} </span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">class Broom { </span><br /><span style="font-family:arial;color:#000099;">static class B2 {void goB2() {</span><br /><span style="font-family:arial;color:#000099;">System.out.println("hi 2"); </span><br /><span style="font-family:arial;color:#000099;">} </span><br /><span style="font-family:arial;color:#000099;">} </span><br /><span style="font-family:arial;color:#000099;">public static void main(String[] args) {</span><br /><span style="font-family:arial;color:#000099;">BigOuter.Nest n = new BigOuter.Nest(); // both class names </span><br /><span style="font-family:arial;color:#000099;">n.go();</span><br /><span style="font-family:arial;color:#000099;">B2 b2 = new B2(); // access the enclosed class</span><br /><span style="font-family:arial;color:#000099;">b2.goB2();</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;">}</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">Which produces:</span><br /><span style="font-family:arial;color:#000099;">hihi 2</span><br /><span style="font-family:arial;color:#000099;"></span><br /><span style="font-family:arial;color:#000099;">Just as a static method does not have access to the instance variables and non-static methods of the class, a static nested class does not have access to the instance variables and non-static methods of the outer class.</span><br /><br /><span style="font-family:arial;color:#000099;">Look for static nested classes with code that behaves like a nonstatic (regular inner) class.</span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-26805660755828404322008-06-01T22:14:00.000-07:002008-08-09T22:35:06.413-07:00Latest Dumps SCJP<span style="color:#3333ff;">Q1 Select three correct statements.<br /><br /><br />(1) A static method may override another static method<br />(2) A static method cannot override a non-static method<br />(3) A non-static method cannot override a static method<br />(4) A non-static method may be overloaded by a static method<br />(5) A synchronized method cannot be overridden<br /><br /><br />Answer : 2,3,4<br /><br /><strong>Explanation :</strong><br />Overriding is for non-static methods and hiding is for static methods. So the following statements are the only true statements about hiding and overriding:<br /><br />a static method (in a subclass) may hide another static method (in a superclass)<br />a static method (in a subclass) cannot hide a non-static method (in a superclass)<br />a non-static method (in a subclass) may override another non-static method (in a superclass)<br />a non-static method (in a subclass) cannot override a static method (in a superclass)</span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-46663492543765273602008-05-06T11:35:00.000-07:002008-10-09T03:37:21.313-07:00SCJP Questions with Answers<span style="font-family:arial;"><span style="color:#000099;"><span style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)">Q 1</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">What is the output of the following code when compiled and run? Select two correct answers.</span><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">public class TechnoSample { </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">public static void main(String[] args){ </span><br style="COLOR: rgb(51,51,255)"></span></span><span style="font-family:arial;"><span style="color:#000099;"><span style="COLOR: rgb(51,51,255)">for(int i = 0; i <><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">System.out.println(getPrimitive(127)); //line 1 </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">} </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">} </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">public static int getPrimitive(byte b) { //line 2 </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">return (short)(Math.random()*b); //line 3 </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">}</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">}</span><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(1) Compilation error on line 1 </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(2) Compilation error on line 2 </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(3) Compilation error on line 3 </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(4) Line 3 compiles fine </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(5) Prints 10 random numbers between 0 and 127 </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><span style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)">Answer : 1,4 </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><span style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)">Explanation :</span><span style="COLOR: rgb(51,51,255)"> </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">Line 1 does not compile because getPrimitive() takes a byte and we pass it an int. In a normal assignment (byte b = 127;) this would work because 127 is in the range for byte values and the compiler implicitely does a norrowing conversion.But this is not the case in method invocations. A quote from JLS 5.3: "The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matchingresolution process". This speaks for itself. Line 3 compiles fine because we have to do with a widening primitive conversion form short to int which is perfectly straightforward. </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)">Q2</span><span style="COLOR: rgb(51,51,255)"> Select three correct statements.</span><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(1) A static method may override another static method </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(2) A static method cannot override a non-static method </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(3) A non-static method cannot override a static method </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(4) A non-static method may be overloaded by a static method </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(5) A synchronized method cannot be overridden </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><span style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)">Answer : 2,3,4 </span><br style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)"><span style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)">Explanation : </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">Overriding is for non-static methods and hiding is for static methods. So the following statements are the only true statements about hiding and overriding:</span><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">a static method (in a subclass) may hide another static method (in a superclass)</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">a static method (in a subclass) cannot hide a non-static method (in a superclass)</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">a non-static method (in a subclass) may override another non-static method (in a superclass)</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">a non-static method (in a subclass) cannot override a static method (in a superclass)</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)">Q3</span><span style="COLOR: rgb(51,51,255)"> Select three correct statements about the following code.</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">public class TechnoSample {</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">public static void main(String[] args) { </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">TechnoSample myref = new TechnoSampleSub(); </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">try{ </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">myref.test(); </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">}</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">catch(Exception e){} </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">} </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">void test() throws Exception{ </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">System.out.println("In TechnoSample"); </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">throw new Exception(); </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">}</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">}</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">class TechnoSample Sub extends TechnoSample { </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">void test() { </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">System.out.println("In TechnoSampleSub"); </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">}</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">}</span><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(1) The try-catch block that encloses myref.test(); is mandatory for the code to compile </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(2) Prints: In TechnoSample </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(3) Prints: In TechnoSampleSub </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(4) Method test() in class TechnoSampleSub has no obligation to declare a throws clause </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(5) An exception is thrown at runtime </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><span style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)">Answer : 1,3,4 </span><br style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)"><span style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)">Explanation : </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">myref is an instance of class TechnoSampleSub referenced by a variable of type TechnoSample. Method test() in class TechnoSample is overridden in class TechnoSampleSub, thus the one to be invoked is the one declared in class TechnoSampleSub(Polymorphism!). Moreover, test() has no obligation to declare a throws clause (see overriding rules!). The try-catch block is mandatory because myref could as well reference an instanceof class TechnoSample and in that case the method test() to be invoked would be the one declared in class TechnoSample which throws an exception. </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)">Q4 </span><span style="COLOR: rgb(51,51,255)">Given the following code:</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">import java.util.Date; </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">public class Example { </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">public static void main(String args[]) { </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">Date d1 = new Date (99, 11, 31); </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">Date d2 = new Date (99, 11, 31); </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">method(d1, d2); </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">System.out.println("d1 is " + d1 + "\nd2 is " + d2); </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">} </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">public static void method(Date d1, Date d2) {</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">d2.setYear (100);</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">d1 = d2; </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">}</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">}</span><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">Which one or more of the following correctly describe the behavior when this program is compiled and run? </span><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(1) compilation is successful and the output is:</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">d1 is Fri December 31 00:00:00 GMT 1999 d2 is Fri December 31 00:00:00 GMT 1999</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(2) compilation is successful and the output is:</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">d1 is Fri December 31 00:00:00 GMT 1999 d2 is Sun December 31 00:00:00 GMT 2000</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(3) compilation is successful and the output is:</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">d1 is Sun December 31 00:00:00 GMT 2000 d2 is Sun December 31 00:00:00 GMT 2000</span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)"></span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(4) the assignment 'd1 = d2' is rejected by the compiler because the Date class cannot overload the operator '=' </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">(5) the expression (d1 is " + d1 + "\nd2 is " + d2) is rejected by the compiler because the Date class cannot overload the operator '+' </span><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><span style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)">Answer : 2 </span><br style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)"><span style="FONT-WEIGHT: bold; COLOR: rgb(51,51,255)">Explanation : </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">1) is false because we know that the data in d2 was changed. 3) is false because we know that the data in d1 was not changed. The names d1 and d2 are used in both main and method to be confusing. They are different and stored on the stack in different place. All the interesting stuff that happen in the Example class is in method. main simply initializes some data and prints the results. In method, the following happens: </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">1.d2 has its year set to 100 (really 2000, as 2.Object d1 is set to be the same as d2. This is a change of the actual reference, not in the data at d1. </span><br style="COLOR: rgb(51,51,255)"><span style="COLOR: rgb(51,51,255)">Both of these line are perfectly legal, and do not result in a compilation error, so d) is false. I will also point out here that e) is String context. toString() is defined by the Object class and so it is available on all classes in Java. Most non-trivial classes override toString() to return more explicit information about themselves. </span><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"><br style="COLOR: rgb(51,51,255)"></span><br /><span style="font-size:130%;"></span></span></span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-77704344994599480052008-04-03T21:35:00.000-07:002008-10-09T02:51:41.712-07:00LAST MINUTE REVISION POINTS<span style="font-family:arial;color:#000099;">1. <strong>Evaluation</strong> and execution –remember that evaluation is from left to right but<br />execution is from right to left.<br /><br />2. There must be some statement after do keyword in <strong>do – while</strong> loop for it to<br />compile without error.<br />i.e.<br />do ; while(false); //correct<br />do {;}while(false); //correct<br />do {}while(false); //correct<br />do while(false); //error<br /><br />3. If “t” is a reference variable then ,<br />t.equals(null) is </span><span style="color:#000099;"><span style="font-family:arial;"><strong>false<br /></strong>(null).equals(t) <strong>compiler error</strong><br />let’s say t2 is some other reference variable then<br />t.equals(t2) </span></span><span style="color:#000099;"><span style="font-family:arial;"><strong>false and not error<br /></strong>consider,<br />t = null;<br />t.equals(t2); //not compiler error but runtime error<br /><br />4. If a class is declared inside a package with <strong>public modifier</strong> then that class<br />becomes invisible to all other classes in other packages unless they import the<br />package or use extended form of addressing the class.<br /><br />5. The <strong>Iterator method</strong> of collection interface when invoked returns an instance of<br />Iterator class.<br /><br />6. given,<br />char c = ‘a’;<br />int i = 1;<br />c + =i; //correct<br />c = c+ i; //illegal<br /><br />7. when use int numbers in basic arithmetic operation then the output is an integer<br />number. Hence ,<br />int i = 4/3;<br />“i” will have the value 1.<br /><br />8. <strong>Native methods</strong> can be set to any access level - public , protected, private, default.<br /><br />9. The methods in the immediate super class in the inheritance tree may be accessed<br />through the use of keyword “super” , but classes above the immediate <strong>super class</strong><br />are not visible.<br /><br />10. </span></span><span style="color:#000099;"><span style="font-family:arial;"><strong>Valid comments<br /></strong>· /* this is a comment */<br />· /** this is a comment */<br />· /*this is a comment **/<br />· // /** this is a comment */ */<br />Important:-<br />· /* //this is a comment */<br />11. invalid comments<br />· /** this is a comment */ */<br /><br />12. If a method declares some exception in throws clause and the subclass of the<br />given class while overriding the method declares some new exception then before<br />assuming that it causes compiler error first check whether the new exception<br />thrown in the subclass’ method is <strong>unchecked exception</strong> or not.<br /><br />13. After solving the logic inside the problem before jumping to conclusion check<br />whether <strong>some code is unreachable</strong> or not. Because if it happens so then it results<br />in compiler error.<br /><br />14. The following form of instantiating a <strong>static inner class</strong> results in compiler error,<br />new <outerclassname>().new <staticinnerclassname>();<br /><br />15. long l = <strong>Integer.MAX_VALUE</strong>;<br />float f = l;<br />double d = l;<br />then “d==f” is false due to rounding of numbers in float literal.<br />But when we assign l to <strong>Long.MAX_VALUE</strong> or to <strong>Integer.MIN_VALUE</strong> then<br />we get the result of “d==f” as true.<br /><br />16. We can place <strong>label</strong> statements around a block of code wherever we wish , unless<br />the name used is not a keyword and follows all the rules meant for identifier.<br />For eg.<br />labelA:<br />{<br />…..some complex code….<br />…..some complex code….<br />if(someThingIsTrue)<br />{<br />break labelA;<br />}<br />}<br />this way we place break statement with label in any labeled block of code<br />which may break out of the code if something comes true.<br />Furthur the same labels can be used for other block of code as long as they<br />don’t overlap.<br /><br />17. An <strong>abstract method</strong> cannot be marked as both<br />· Abstract and strictfp<br />· Abstract and native<br />· Abstract and synchronized<br />· Abstract and final<br />· Abstract and private<br />· Abstract and static<br /><br />18. <strong>Shift operators</strong> can be used only on integers .<br /><br />19. <strong>Switch statements</strong> can evaluate byte , short, char , int.<br />but not long, float.double.<br />i.e. long l = 10;<br />switch(l){}//causes compiler error<br />before jumping to conclusion about switch statements , verify whether<br />the case arguments are lying within the range of the switch argument.<br />For e.g. byte b = 10;<br />Switch(b)<br />{<br />case 10: …….. complexcode………break;<br />case 1000: …….. complexcode………break;<br />}<br />here second case statement causes compiler error since it is out of range of<br />byte literal.<br /><br />20. The <strong>case argument</strong> must be primitive literal type or final variable.<compatible><br /><br />21. <strong>For loop</strong> declarations,<br /></span></span><span style="color:#000099;"><span style="font-family:arial;"><strong>valid<br /></strong>· for(int i=0,j=0;i<10;i++);<strong>invalid<br /></strong>· for(i=0,int j=0;;);<br />· int k =1;<br />for(int i=0,k=0;;); </span></span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-59816484266021846622008-03-03T01:43:00.000-08:002008-08-09T22:25:11.278-07:00SCJP 1.5 Dump questions<span style="COLOR: rgb(0,0,153)">Q1</span><br /><span style="COLOR: rgb(0,0,153)">class test</span><br /><span style="COLOR: rgb(0,0,153)">{</span><br /><span style="COLOR: rgb(0,0,153)">public static void main(String[] <span class="blsp-spelling-error" id="SPELLING_ERROR_0">args</span>) </span><br /><span style="COLOR: rgb(0,0,153)">{</span><br /><span style="COLOR: rgb(0,0,153)">test inst_test = new test();</span><br /><span style="COLOR: rgb(0,0,153)">String pig[][] = { {"one little piggy"}, {"two little piggies"}, {"three little piggies"} };</span><br /><span style="COLOR: rgb(0,0,153)">for ( Object []oink : pig )</span><br /><span style="COLOR: rgb(0,0,153)">{</span><br /><span style="COLOR: rgb(0,0,153)">for ( Object piggy : oink )</span><br /><span style="COLOR: rgb(0,0,153)">{</span><br /><span style="COLOR: rgb(0,0,153)">System.out.println(piggy);</span><br /><span style="COLOR: rgb(0,0,153)">}</span><br /><span style="COLOR: rgb(0,0,153)">}</span><br /><span style="COLOR: rgb(0,0,153)">}</span><br /><span style="COLOR: rgb(0,0,153)">}</span><br /><br /><span style="COLOR: rgb(0,0,153)">a. one little piggy two little piggies three little piggies</span><br /><span style="COLOR: rgb(0,0,153)">b. Compile Error incompatible types.</span><br /><span style="COLOR: rgb(0,0,153)">c. java.lang.String;@187c6c7 java.lang.String;@187c6c8 java.lang.String;@187c6c9 </span><br /><span style="COLOR: rgb(0,0,153)">( or something like that )</span><br /><span style="COLOR: rgb(0,0,153)">d. <span class="blsp-spelling-error" id="SPELLING_ERROR_1">Runtime</span> <span class="blsp-spelling-error" id="SPELLING_ERROR_2">Null Pointer Exception</span></span><br /><span style="COLOR: rgb(0,0,153)">e. Prints nothing</span><br /><br /><span style="COLOR: rgb(0,0,153)">Answer1:</span><br /><span style="COLOR: rgb(0,0,153)">a. oink refers to every object reference in a one dimensional row of pig[][]. </span><br /><span style="COLOR: rgb(0,0,153)">piggy refers to every object within that row.</span><br /><br /><span style="COLOR: rgb(0,0,153)">--------------------------------------------------------------------------</span><br /><span style="COLOR: rgb(0,0,153)">Q2</span><br /><span style="COLOR: rgb(0,0,153)">class test</span><br /><span style="COLOR: rgb(0,0,153)">{</span><br /><span style="COLOR: rgb(0,0,153)">public static void main(String[] <span class="blsp-spelling-error" id="SPELLING_ERROR_3">args</span>) </span><br /><span style="COLOR: rgb(0,0,153)">{</span><br /><span style="COLOR: rgb(0,0,153)">test inst_test = new test();</span><br /><span style="COLOR: rgb(0,0,153)">int i1 = 2000;</span><br /><span style="COLOR: rgb(0,0,153)">int i2 = 2000;</span><br /><span style="COLOR: rgb(0,0,153)">int i3 = 2;</span><br /><span style="COLOR: rgb(0,0,153)">int i4 = 2;</span><br /><span style="COLOR: rgb(0,0,153)">Integer <span class="blsp-spelling-error" id="SPELLING_ERROR_4">Ithree</span> = new Integer(2); // 1</span><br /><span style="COLOR: rgb(0,0,153)">Integer <span class="blsp-spelling-error" id="SPELLING_ERROR_5">Ifour</span> = new Integer(2); // 2</span><br /><span style="COLOR: rgb(0,0,153)">System.out.println( <span class="blsp-spelling-error" id="SPELLING_ERROR_6">Ithree</span> == <span class="blsp-spelling-error" id="SPELLING_ERROR_7">Ifour</span> );</span><br /><span style="COLOR: rgb(0,0,153)">inst_test.method( i3 , i4 );</span><br /><span style="COLOR: rgb(0,0,153)">inst_test.method( i1 , i2 );</span><br /><span style="COLOR: rgb(0,0,153)"></span><br /><span style="COLOR: rgb(0,0,153)">}</span><br /><span style="COLOR: rgb(0,0,153)">public void method( Integer i , Integer eye )</span><br /><span style="COLOR: rgb(0,0,153)">{</span><br /><span style="COLOR: rgb(0,0,153)">System.out.println(i == eye );</span><br /><span style="COLOR: rgb(0,0,153)">}</span><br /><span style="COLOR: rgb(0,0,153)">}</span><br /><br /><span style="COLOR: rgb(0,0,153)">a. true false true</span><br /><span style="COLOR: rgb(0,0,153)">b. false true false</span><br /><span style="COLOR: rgb(0,0,153)">c. false false false</span><br /><span style="COLOR: rgb(0,0,153)">d. true true false</span><br /><span style="COLOR: rgb(0,0,153)">e. Compile error</span><br /><br /><span style="COLOR: rgb(0,0,153)">Answer 2: </span><br /><span style="COLOR: rgb(0,0,153)">b: false true false. <span class="blsp-spelling-error" id="SPELLING_ERROR_8">lthree</span> and <span class="blsp-spelling-error" id="SPELLING_ERROR_9">lfour</span> are two <span class="blsp-spelling-error" id="SPELLING_ERROR_10">seperate</span> objects. if the lines 1 and 2 were </span><br /><span style="COLOR: rgb(0,0,153)"><span class="blsp-spelling-error" id="SPELLING_ERROR_11">lthree</span> = 2 and <span class="blsp-spelling-error" id="SPELLING_ERROR_12">lfour</span> = 2 the result would have been true. This is when the objects are created in the pool. When the references I and eye in the pool are compared 2==2 results in true and 2000==2000 is false since it exceeds 127.</span><br /><br /><span style="COLOR: rgb(0,0,153)">-------------------------------------------------------------------------</span><br /><span style="COLOR: rgb(0,0,153)">Q3</span><br /><span style="COLOR: rgb(0,0,153)"><span class="blsp-spelling-error" id="SPELLING_ERROR_13">enum</span> cafe { </span><br /><span style="COLOR: rgb(0,0,153)">BIG ( 10 ) ,</span><br /><span style="COLOR: rgb(0,0,153)">SMALL ( 1 ),</span><br /><span style="COLOR: rgb(0,0,153)">MED ( 5 ) </span><br /><span style="COLOR: rgb(0,0,153)">int <span class="blsp-spelling-error" id="SPELLING_ERROR_14">mySize</span> = 0; </span><br /><span style="COLOR: rgb(0,0,153)">cafe ( int size )</span><br /><span style="COLOR: rgb(0,0,153)">{</span><br /><span style="COLOR: rgb(0,0,153)"><span class="blsp-spelling-error" id="SPELLING_ERROR_15">mySize</span> = size;</span><br /><br /><span style="COLOR: rgb(0,0,153)">}</span><br /><span style="COLOR: rgb(0,0,153)"></span><br /><span style="COLOR: rgb(0,0,153)">}</span><br /><br /><span style="COLOR: rgb(0,0,153)">What happens when this <span class="blsp-spelling-error" id="SPELLING_ERROR_16">enum</span> is in the code outside a class ?</span><br /><br /><span style="COLOR: rgb(0,0,153)">a. Compiles fine</span><br /><span style="COLOR: rgb(0,0,153)">b. Compiler error</span><br /><span style="COLOR: rgb(0,0,153)">c. <span class="blsp-spelling-error" id="SPELLING_ERROR_17">Runtime</span> Exception occurs if <span class="blsp-spelling-error" id="SPELLING_ERROR_18">mySize</span> is accessed.</span><br /><br /><span style="COLOR: rgb(0,0,153)">Answer 3: </span><br /><span style="COLOR: rgb(0,0,153)">a: Compile error: semicolon missing after MED ( 5 ). Watch out for that semicolon when an <span class="blsp-spelling-error" id="SPELLING_ERROR_19">enum</span> has variables and functions.</span><br /><br /><span style="COLOR: rgb(0,0,153)">---------------------------------------------------------------------</span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com1tag:blogger.com,1999:blog-7863722189611643831.post-75397687920138714342008-02-03T22:00:00.000-08:002008-10-09T03:17:50.469-07:00Some Suggestions for SCJP takers<span style="font-family:lucida grande;font-size:130%;"><br /><span style="color:#000099;">1] Try to give SCJP from Java 5 (SCJP 310-055) </span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">2] If you are feeling that Generics/ Collection is tough then please read Java Complete Reference Book </span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">3] I faced some difficulty while taking the exam bcz of drag and drop questions. Actually the problem is in Exam software. It was not working properly. I was like playing game . So be careful about those questions. </span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">4] For Garbage collection question you must do paper work. Then and only then you will get 100% in these questions. </span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">e.g. MyClass obj1 = new MyClass(); </span><br /><span style="color:#000099;">then drow like this... </span><br /><span style="color:#000099;">new Myclass1 ---------obj1 </span><br /><span style="color:#000099;">and if I say obj1=null then do this.... </span><br /><span style="color:#000099;">new Myclass1 -------------obj1 </span><br /><span style="color:#000099;">Then you can guess that one object has been garbage collected. (new Class()) And it will take hardly 10 to 15 sec to draw isn't it? </span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">5] One more thing that many of you doesn't know we can return array from method like this also........keep in mind .. </span><br /><span style="color:#000099;">public int getArr() [] </span><br /><span style="color:#000099;">{ </span><br /><span style="color:#000099;">return new int[]{1,2}; </span><br /><span style="color:#000099;">} </span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">6] I got lots questions on generics and I got 100% in it ..so be prepared. </span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">7] Be a detective ....go through each and every line...you can do this by moving mouse pointer on each line.... </span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">8] Go through each and every objectives....because you may feel that this is the right answer but there may be another answer which is better than this.... </span><br /><span style="color:#000099;">e.g a] Compiles fine </span><br /><span style="color:#000099;">b] does not compile </span><br /><span style="color:#000099;">c] throws exception </span><br /><span style="color:#000099;">d] error at line number... </span><br /><span style="color:#000099;">e] Compiles with warnings . -------right answer </span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">9] K&B book is enough but for Generics first read Complete Reference and then read K&B. </span><br /><span style="color:#000099;">10 ] Be confident about program flow specially with Exception Handling programs. And don't worry if you are newcomer it doesn't matter. </span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">And I will be always there to help you . </span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">Do write to me in case of any more tips : </span><br /><span style="color:#000099;">wise(dot)mohit(at)gmail(dot)com</span><br /></span><br /></span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-25658992045129009892008-01-23T23:13:00.000-08:002008-03-25T09:51:59.804-07:00Some Questions on Objects for SCJP 5<span style="color:#000099;">Q1 What is the result of executing the following fragment of code:</span><br /><span style="color:#000099;">boolean b1 = false;</span><br /><span style="color:#000099;">boolean b2 = false;</span><br /><span style="color:#000099;">if (b2 != b1 = !b2) </span><br /><span style="color:#000099;">{ System.out.println("true");</span><br /><span style="color:#000099;">}</span><br /><span style="color:#000099;">else { System.out.println("false");</span><br /><span style="color:#000099;">}</span><br /><span style="color:#000099;">Select 1 correct option<br />(1)Compile time error<br />(2)It will print true<br />(3)It will print false<br />(4)Runtime error<br />(5)It will print nothing<br />Answer : 1<br /><strong>Explanation :</strong> Note that, boolean operators have more precedence than =. (In fact, = has least precedenace) so, in (b2 != b1 = !b2) first b2 != b1 is evaluated which returns a value 'false'. So the expression becomes false = !b2. And this is illegalbecause false is a value and not a variable!Had it been something like (b2 = b1 != b2) then its valid because it will boil down to : b2 = false. Because all an if() needs is a boolean, now b1 != b2 returns false which is a boolean andas b2 = false is an expression and every expression has a return value (which is actually the LHS of the erpression). Here it returns true which is again a boolean.Note, return value of expression (i is int) : i = 10 , is 10 (int). </span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">------------------------------------------------------------------------------------------------------------------------------</span><br /><br /><span style="color:#000099;">Q2</span><br /><span style="color:#000099;">Given two collection objects referenced by c1 and c2, which of these statements are true?Select 2 correct options<br />(1)c1.retainAll(c2) will not modify c1<br />(2)c1.removeAll(c2) will not modify c1<br />(3)c1.addAll(c2) will return a new collection object, containing elements from both c1 and c2<br />(4)For: c2.retainAll(c1); c1.containsAll(c2); 2nd statement will return true<br />(5)For: c2.addAll(c1); c1.retainAll(c2); 2nd statement will have no practical effect on c1</span><br /><br /><span style="color:#000099;">Answer : 4,5<br /><strong>Explanation :</strong> public boolean retainAll(Collection c) retains only the elts in this collection that are contained in the specified collection. In other words, removes from this collection all of its elts that are not contained in the specified collectionpublic boolean removeAll(Collection c) removes all this collection's elts that are also contained in the specified collection. After this call returns, this collection will contain no elts in common with the specifiedcollectionpublic boolean containsAll(Collection c) returns true if this collection contains all of the elts in the specified collectionpublic boolean addAll(Collection c) adds all the elts in the specified collectionto this collection. The behavior of this opern is undefined if the specified collection is modified while the opern is in progress(ie., the behavior of this call is undefined if the specified collection is this collection, and is nonempty)</span><br /><span style="color:#000099;">---------------------------------------------------------------------------------------------------------------------<br /></span><br /><span style="color:#000099;">Q 3<br />What happens when the following code gets executed:</span><br /><span style="color:#000099;">class Sample { </span><br /><span style="color:#000099;">public static void main(String[] args)</span><br /><span style="color:#000099;"> { double d1 = 1.0; </span><br /><span style="color:#000099;">double d2 = 0.0;</span><br /><span style="color:#000099;"> byte b =1; </span><br /><span style="color:#000099;">d1 = d1/d2</span><span style="color:#000099;">; </span><br /><span style="color:#000099;">b = (byte) d1; </span><br /><span style="color:#000099;">System.out.print(b); </span><br /><span style="color:#000099;">}</span><br /><span style="color:#000099;">}<br />(1)It results in the throwing of an ArithmeticExcepiton<br />(2)It results in the throwing of a DivedeByZeroException<br />(3)It displays the value 1.5<br />(4)It displays the value –1<br />Answer : 4<br /><strong>Explanation : </strong>1.0/0.0 results in Double.POSITIVE_INFINITY. Double.POSITIVE_INFINITY is converted to Integer.MAX_VALUE ('0' followed by 31 '1's). Integer.MAX_VALUE is then cast to byte value, which simply takes the last 8 bits(11111111) and is -1.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">---------------------------------------------------------------------------------------------------------</span><br /><br /><span style="color:#000099;">Q4<br />Class finalization can be done by implementing the following method:static void classFinalize() throws Throwable;True Or False?<br />(1)True<br />(2)False<br />Answer : 2<br /><strong>Explanation :</strong> PREVIOUSLY: If a class declares a class method classFinalize that takes no arguments and returns no result: static void classFinalize() throws Throwable { . . . } then this method will be invoked before the class is unloaded . Like thefinalize method for objects, this method will be automatically invoked only once. This method may optionally be declared private, protected, or public. NOW: Class finalization has been removed from the Java language. Thefunctionality of JLS 12.7 is subsumed by instance finalization (JLS 12.6).Here is a rationale for this decision. http://java.sun.com/docs/books/jls/class-finalization-rationale.htmlSimilar thing has happend toclass unloading: A class or interface may be unloaded if and only if its class loader is unreachable (the definition of unreachable is given in JLS 12.6.1). Classes loaded by the bootstrap loader may not be unloaded.</span><br /><span style="color:#000099;">---------------------------------------------------------------------------------------------------------------------------</span><br /><br /><span style="color:#000099;">Q5<br />Consider the following method:</span><br /><span style="color:#000099;">public void getLocks(Object a, Object b)</span><br /><span style="color:#000099;">{ synchronized(a) </span><br /><span style="color:#000099;">{ synchronized(b) { //do something } } }</span><br /><span style="color:#000099;">and the following instantiations:</span><br /><span style="color:#000099;">Object obj1 = new Object(); </span><br /><span style="color:#000099;">Object obj2 = new Object();</span><br /><span style="color:#000099;">obj1 and obj2 are accesible to two different threads and the threads are about to call the getLocks() method.Assume the first thread calls the method getLocks(obj1, obj2).</span><br /><span style="color:#000099;">Which of the following is true? Options Select 1 correct option<br />(1)The second thread should call getLocks(obj2, obj1)<br />(2)The second thread should call getLocks(obj1, obj2)<br />(3)The second thread should call getLocks() only after first thread exits out of it<br />(4)The second thread may call getLocks() any time and passing parameters in any order<br />(5)None of the above</span><br /><br /><span style="color:#000099;">Answer : 2<br /><strong>Explanation :</strong> (1) This may result in a deadlock (3) The is not necessary. Option 2 works just fine.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">------------------------------------------------------------------------------------------------------------</span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-87925212244383791172008-01-13T20:32:00.000-08:002008-03-25T09:51:36.926-07:00SCJP Mock Questions and Answers<span style="color: rgb(0, 0, 153);">Ques1</span><br /><span style="color: rgb(0, 0, 153);"> Given:</span><br /><span style="color: rgb(0, 0, 153);">1. public class MyThread implements Runnable { </span><br /><span style="color: rgb(0, 0, 153);">2. private String holdA = "This is "; </span><br /><span style="color: rgb(0, 0, 153);">3. private int[] holdB = {1,2,3,4,5,6,7,8,9,10}; </span><br /><span style="color: rgb(0, 0, 153);">4.</span><br /><span style="color: rgb(0, 0, 153);">5. public static void main(String args[]) { </span><br /><span style="color: rgb(0, 0, 153);">6. MyThread z = new MyThread(); </span><br /><span style="color: rgb(0, 0, 153);">7. (new Thread(z)).start(); </span><br /><span style="color: rgb(0, 0, 153);">8. (new Thread(z)).start(); </span><br /><span style="color: rgb(0, 0, 153);">9. }</span><br /><span style="color: rgb(0, 0, 153);">10.</span><br /><span style="color: rgb(0, 0, 153);">11. public synchronized void run() {</span><br /><span style="color: rgb(0, 0, 153);">12. for(int w = 0;w <><br /><span style="color: rgb(0, 0, 153);">13. System.out.println(holdA + holdB[w] + ".");</span><br /><span style="color: rgb(0, 0, 153);">14. }</span><br /><span style="color: rgb(0, 0, 153);">15. }</span><br /><span style="color: rgb(0, 0, 153);">16. }</span><br /><br /><span style="color: rgb(0, 0, 153);">What is the result?</span><br /><br /><span style="color: rgb(0, 0, 153);"> </span><br /><span style="color: rgb(0, 0, 153);"> (1) Compilation fails because of an error on line 6 </span><br /><span style="color: rgb(0, 0, 153);"> (2) Compilation fails because of an error on line 11 </span><br /><span style="color: rgb(0, 0, 153);"> (3) Compilation fails because of errors on lines 7 and 8 </span><br /><span style="color: rgb(0, 0, 153);"> (4) Compilation succeeds and the program prints each value in the holdB array at the end of the "This is " line. Each value is printed two times before the program ends, and the values are not printed in sequential order </span><br /><span style="color: rgb(0, 0, 153);"> (5) Compilation succeeds & the prog. prints each val in the holdB array at the end of the "This is " line. Each val is printed in order from 1-10 & after the val 10 prints, it starts printing the vals 1-10 in order again </span><br /><span style="color: rgb(0, 0, 153);"> </span><br /><span style="color: rgb(0, 0, 153);"> </span><br /><span style="color: rgb(0, 0, 153);"> Answer : 5 </span><br /><span style="color: rgb(0, 0, 153);"> Explanation : </span><br /><span style="color: rgb(0, 0, 153);">Option 5 is correct because the Runnable interface is implemented by declaring a synchronized run() method. The method is declared as synchronized to signify that the object lock must be obtained</span><br /><br /><span style="color: rgb(0, 0, 153);">Options 1, 2, and 3 are incorrect because compilation succeeds. Option 4 is incorrect, but would be correct if the run() method were not declared as synchronized.</span><br />----------------------------------------------------------------------------------------------------------------------------<br /><br /><span style="color: rgb(0, 0, 153);">Ques 2 :Which statement about the Map interface is true?</span><br /><br /><span style="color: rgb(0, 0, 153);"> </span><br /><span style="color: rgb(0, 0, 153);"> (1) Entries are placed in a Map using the values() method </span><br /><span style="color: rgb(0, 0, 153);"> (2) Entries are placed in a Map using the entrySet() method </span><br /><span style="color: rgb(0, 0, 153);"> (3) A key/value association is added to a Map using the put() method </span><br /><span style="color: rgb(0, 0, 153);"> (4) A key/value association is added to a Map using the putAll() method </span><br /><span style="color: rgb(0, 0, 153);"> </span><br /><span style="color: rgb(0, 0, 153);"> </span><br /><span style="color: rgb(0, 0, 153);"> Answer : 3 </span><br /><span style="color: rgb(0, 0, 153);"> Explanation : </span><br /><span style="color: rgb(0, 0, 153);">Option 3 is correct because the put() method is used to add a key/value association to a Map.</span><br /><br /><span style="color: rgb(0, 0, 153);">Option 1 is incorrect because the values() method returns a Collection of all values in a Map.Option 2 is incorrect because the entrySet() method returns a Set of all mappings in a Map. Option 4 is incorrect because the pubAll() method copies all mappings from one Map to another. </span><br />---------------------------------------------------------------------------------------------------------------------------<br /><br /><span style="color: rgb(0, 0, 153);">Ques 3 :</span><br /><span style="color: rgb(0, 0, 153);">Consider the following class definition: </span><br /><span style="color: rgb(0, 0, 153);">1. public class Test extends Base {</span><br /><span style="color: rgb(0, 0, 153);">2. public Test(int j) {</span><br /><span style="color: rgb(0, 0, 153);">3. }</span><br /><span style="color: rgb(0, 0, 153);">4. public Test(int j, int k) {</span><br /><span style="color: rgb(0, 0, 153);">5. super(j, k);</span><br /><span style="color: rgb(0, 0, 153);">6. }</span><br /><span style="color: rgb(0, 0, 153);">7. }</span><br /><br /><span style="color: rgb(0, 0, 153);">Which of the following forms of constructor must exist explicitly in the definition of the Base class?</span><br /><br /><span style="color: rgb(0, 0, 153);"> </span><br /><span style="color: rgb(0, 0, 153);"> (1) Base() { } </span><br /><span style="color: rgb(0, 0, 153);"> (2) Base(int j) { } </span><br /><span style="color: rgb(0, 0, 153);"> (3) Base(int j, int k) { } </span><br /><span style="color: rgb(0, 0, 153);"> (4) Base(int j, int k, int l) { } </span><br /><span style="color: rgb(0, 0, 153);"> </span><br /><span style="color: rgb(0, 0, 153);"> </span><br /><span style="color: rgb(0, 0, 153);"> Answer : 1,3 </span><br /><span style="color: rgb(0, 0, 153);"> Explanation : </span><br /><span style="color: rgb(0, 0, 153);">1 and 3 are correct. In the constructor at lines 2 and 3, there is no explicit call to either this() or super(), which means that the compiler will generate a call to the zero argument superclass constructor, as in 1. The explicit call to super() at line 5 requires that the Base class must have a 7.constructor as in 3. This has two consequences. First, 3 must be one of the required constructors and therefore one of the answers.Second, the Base class must have at least that constructor defined explicitly, so the default constructor is not generated, but must be added explicitly. Therefore the constructor of 1 is also required and must be a correct answer.At no point in the Test class is there a call to either a superclass constructor with one or three arguments, so 2 and 4 need not explicitly exist.<br /><br />-----------------------------------------------------------------------------------------------------------------------------<br /></span><br /><span style="color: rgb(0, 0, 153);"> </span></span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-72865410921582022932007-12-29T05:50:00.000-08:002008-08-10T06:23:03.002-07:00Threads Some Important Points<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKR2Rmxu2nEVaJV5E6RkFDgCeLSLerTKIbcrNCpz5ZUEUlYvYeeuQTlUtuOoN4CdF3lkPzrFwEyfQSkaiPPXxn-NhRbQ7K8uz1fnaxrnZqLzGrdIE6nBbDA0wDlb1nF-WJxqJ6RyXzlWA/s1600-h/Thread+States.bmp"><img id="BLOGGER_PHOTO_ID_5232876189173058018" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKR2Rmxu2nEVaJV5E6RkFDgCeLSLerTKIbcrNCpz5ZUEUlYvYeeuQTlUtuOoN4CdF3lkPzrFwEyfQSkaiPPXxn-NhRbQ7K8uz1fnaxrnZqLzGrdIE6nBbDA0wDlb1nF-WJxqJ6RyXzlWA/s320/Thread+States.bmp" border="0" /></a><br /><div><span style="color:#000099;">1. Methods in object class<br />· wait : throws interruptedException<br />· notify<br />· notifyAll</span><br /><span style="color:#000099;"><strong>Remember</strong> : they are final and must be called from within a synchronized<br />context, otherwise illegalmonitorstate exception is thrown at runtime.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">*******************************************</span><br /><br /><span style="color:#000099;">2. yield , sleep are static members of Thread class.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">********************************************</span><br /><br /><span style="color:#000099;">3. stop , resume , suspend are deprecated members of Thread class.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">*********************************************</span><br /><br /><span style="color:#000099;">4. InterruptedException is checked exception.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">**********************************************</span><br /><br /><span style="color:#000099;">5. Given<br />synchronized (expression){…..}<br />· expression may evaluate to reference to object.<br />· expression cannot evaluate to primitive type or null.<br />· If execution block completes ( abruptly or normally )the lock is released.<br />· synchronized statements can be nested.<br />· synchronized statements with identical expression can be nested.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">*************************************************</span><br /><br /><span style="color:#000099;">6. Thread.yeild method<br />· may cause the current thread to move from runnable to ready state .<br />· It isn’t guaranteed i.e. same thread may start executing again.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">******************************************************</span><br /><br /><span style="color:#000099;">7. Thread.sleep method<br />· makes thread to move from running to not – runnable state<br />· thread takes lock with it if it has got one.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">************************************************</span><br /><br /><span style="color:#000099;">8. Thread class inherits Runnable interface.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">*************************************************</span><br /><br /><span style="color:#000099;">9. A dead thread can never be restarted.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">**************************************************</span><br /><br /><span style="color:#000099;">10. When overriding the ‘run’ method of the Thread class remember the following<br />points<br />· the prototype must be public void run(){}<br />· Access modifier must be public.<br />· ReturnType must be void.<br />· No arguments must be specified and if done it doesn’t cause error but it can’t<br />be used to start a new thread.<br />. if the class implements Runnable interface then it has to possess run method<br />with no arguments specified otherwise compiler error is caused.<br />· it must not declare any new checked exception in its throws clause.<br />· it must not be declared static<br />· It can declare unchecked exception in its throws clause.<br />· If start method is called twice then illegalThreadStateException is thrown.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">************************************************</span><br /><br /><span style="color:#000099;">11. the run method in Thread class is empty method which does nothing.</span><br /><br /><span style="color:#000099;">*******************************************************</span><br /><span style="color:#000099;">12. join,sleep,wait methods declare InterruptedException in their throws clause.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">*************************************************</span><br /><br /><span style="color:#000099;">13. If we extend thread class or implement runnable interface then the present run()<br />method must not be marked static ,if it is marked it causes compiler error.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">***************************************************</span><br /><br /><span style="color:#000099;">14. The priority of a thread can be altered after the thread has been started using<br />setPriority method .The code compiles fine.</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">***************************************************</span><br /></span></span><span style="font-family:times new roman;font-size:130%;color:#000099;"></span></div>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-60658297885581625902007-12-22T05:30:00.000-08:002008-03-25T09:51:36.927-07:00Mock Questions for Test Preparation<span style="color:#000099;">1) Will this work?<br /><br />public static void main(String args[])<br />{<br />RuntimeException re;<br />throw re;<br />}<br /><br />Ans -> No , compile time exception :: variable re may not have been initialized<br />-------------------------------------------------------------------------------------------------------------</span><br /><span style="color:#000099;"><br />2) NumberFormatException is a subclass of IOException<br /><br />-------------------------------------------------------------------------------------------------------------<br /><br />3) if ("string".toUpperCase() =="STRING")<br />{<br />System.out.println("Yes");<br />}<br />else System.out.println("No");<br /><br />Ans -> No // if we use .equals() here , then we get “yes” . This is because , “string” and “STRING” are two separate object references pointing to two separate objects. == is used to compare whether two object references point to same object or not. .equals() compares the objects for their contents.<br />-------------------------------------------------------------------------------------------------------------------<br /><br />3) when we extend a class , the static class variables are also passed on to the child class. Final class variables are not passed. Transient class variables are passes( they are implicitly initialized ).<br /><br />--------------------------------------------------------------------------------------------------------------------<br /><br />4) We cannot extend a math class ( its final) . all its constants and methods are static.<br /><br />---------------------------------------------------------------------------------------------------------------------<br /><br />5) We cannot instantiate a math class .. its constructors are private.<br /><br />---------------------------------------------------------------------------------------------------------------------<br /><br /><br />5) class XTC {<br /><br />public static void main ( String [ ] ka ) {<br />int s = 64 / 9 ;<br />float f = 64 / 9 ;<br />double d = 64 / 9 ;<br />System . out . println ( s + " & " + f + " & " + d ) ;<br />}<br />};<br /><br />Ans -> 7 , 7.0 , 7.0<br /><br />---------------------------------------------------------------------------------------------------------------------<br /><br />6) What is the output of the following ? Will it compile at all ?<br /><br />class XTC {<br /><br />public static void main ( String [ ] ka ) {<br />Integer b = new Integer(3) ;<br />System . out . println ( b instanceof Integer ) ;<br /><br />}<br />};<br /><br />Ans -> true<br /><br />-----------------------------------------------------------------------------------------------------------------------<br /><br />7) What is the output of the following ? Will it compile at all ?<br /><br />class XTC {<br /><br />public static void main ( String [ ] ka ) {<br />Integer b = null;<br />System . out . println ( b instanceof Integer ) ;<br /><br />}<br />};<br /><br />Ans -> false<br /><br />--------------------------------------------------------------------------------------------------------------------<br /><br /><br />8) What is the output of the following ? Will it compile at all ?<br /><br />class XTC {<br /><br />public static void main ( String [ ] ka ) {<br />Integer b = null;<br />System . out . println ( b instanceof Object ) ;<br /><br />}<br />};<br /><br />Ans -> false<br /><br />------------------------------------------------------------------------------------------------------------<br /><br />9) What is the output of the following ? Will it compile at all ?<br /><br />class XTC {<br /><br />public static void main ( String [ ] ka ) {<br />Integer b = new Integer(3);<br />System . out . println ( b instanceof Object ) ;<br /><br />}<br />};<br /><br />Ans -> true<br /><br />------------------------------------------------------------------------------------------------------------------------<br /><br /><br />10) if (Double . POSITIVE_INFINITY == Double . POSITIVE_INFINITY )<br />{<br />System . out . println ( “Yes”) ;<br />}<br /><br />Ans -> Yes<br /><br />--------------------------------------------**********************---------------------------------------------</span><br /></span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com0tag:blogger.com,1999:blog-7863722189611643831.post-83056766840976292812007-12-17T21:48:00.000-08:002008-10-09T03:44:37.933-07:00SCJP Tutorial -- Exception Handling<span style="font-family:times new roman;"><span style="color:#000099;"><span style="FONT-WEIGHT: bold"><span style="font-family:arial;font-size:180%;">Exception Handling</span></span><span style="font-size:100%;"><span style="font-size:180%;"><span style="font-family:arial;"> </span></span></span></span></span><br /><span style="font-family:Arial;font-size:180%;color:#000099;"></span><br /><span style="font-family:times new roman;"><span style="font-size:100%;"><span style="color:#000099;"><span style="font-size:180%;"><span style="font-family:arial;"><img id="BLOGGER_PHOTO_ID_5148236304255963250" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFpjNqbMV2mHc1NjWECd7QIogIpqvFbmrpRRsHA-urvcVWLKXXm1c_3XRwq_lOO0YkKxdx39OrR_SLFRtmmwIlhQEyTAYCJJ15vwJIvrnXvue_B-rlujFsg-2-5EfuhPUlcMKKblT3r40/s320/exceptFig1.gif" border="0" /><br /></span><br /></span><span style="font-family:arial;"><span style="FONT-STYLE: italic"><strong>Defination</strong></span> :- </span></span><span style="font-family:arial;"><span style="color:#000099;"><span style="font-size:130%;">An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.<br /><br /></span>There are 3 main advantages for exceptions:<br />1. Separates error handling code from "regular" code<br />2. Propagating errors up the call stack (without tedious programming)<br />3. Grouping error types and error differentiation </span></span></span></span><br /><br /><p><span style="font-family:arial;font-size:100%;color:#000099;"><em>Syntax :-</em></span></p><br /><p><span style="font-family:arial;"><span style="color:#000099;"><span style="font-size:100%;">try { </span><br /></span></span></p><p><span style="font-family:arial;"><span style="color:#000099;"><span style="font-size:100%;">int y =5;</span><br /></span></span></p><p><span style="font-family:arial;"><span style="color:#000099;"><span style="font-size:100%;">for(int i =5; i<=0; i++)</span><br /></span></span></p><p><span style="font-family:arial;"><span style="color:#000099;"><span style="font-size:100%;">{ int z = x/i; } </span><br /></span></span></p><p><span style="font-family:arial;font-size:100%;color:#000099;">}catch(Exception e1)</span></p><p><span style="font-family:arial;"><span style="color:#000099;"><span style="font-size:100%;">{ system.out.println( "Exception Occured" +e.printstacktrace());</span><br /></span></span></p><p><span style="font-family:arial;font-size:100%;color:#000099;">}</span></p><p><span style="font-family:arial;"><span style="color:#000099;"><span style="font-size:100%;">finally {</span><br /></span></span></p><p><span style="font-family:arial;"><span style="color:#000099;"><span style="font-size:100%;">System.out.println("Fional code is always executed");</span><br /></span></span></p><p><span style="font-family:arial;font-size:100%;color:#000099;">}</span></p><p><br /><span style="font-family:Times New Roman;"><span style="font-size:100%;"><span style="font-size:130%;"><span style="color:#000099;"><span style="FONT-STYLE: italic;font-family:lucida grande;font-size:180%;" ><strong>Important tricks to be Noted :-</strong></span><br /></span></span></span></span><br /></p><blockquote><p><span style="font-size:100%;color:#000099;"><em>" An exception causes a jump to<br />the end of try block. If the exception occurred in a method called from a try<br />block, the called method is abandoned. </em></span></p><em><p><br /><span style="font-size:100%;color:#000099;">******************************************************************************************************</span></em><span style="font-size:100%;color:#000099;"><em><br />" If there's a catch block for<br />the occurred exception or a parent class of the exception, the exception is now<br />considered handled. </em></span></p><p><span style="font-size:100%;color:#000099;"><em>*******************************************************************************************************<br /><br />" At least one 'catch' block or one 'finally' block<br />must accompany a 'try' statement. If all 3 blocks are present, the order is<br />important. (try/catch/finally) </em></span></p><p><span style="font-size:100%;color:#000099;"><em>*******************************************************************************************************<br /><br />" finally and catch can come only with<br />try, they cannot appear on their own. </em></span></p><p><span style="font-size:100%;color:#000099;"><em>****************************************************************************************************<br /><br />" Regardless of whether or not an<br />exception occurred or whether or not it was handled, if there is a finally<br />block, it'll be executed always. (Even if there is a return statement in try<br />block). </em></span></p><p><span style="font-size:100%;color:#000099;"><em>**************************************************************************************************<br /><br />" System.exit() and error conditions are the only exceptions<br />where finally block is not executed. </em></span></p><p><span style="font-size:100%;color:#000099;"><em>****************************************************************************************************<br /><br />" If there was no exception or the<br />exception was handled, execution continues at the statement after the<br />try/catch/finally blocks. </em></span></p><p><span style="font-size:100%;color:#000099;"><em>****************************************************************************************************<br /><br />" If the exception is not handled, the process<br />repeats looking for next enclosing try block up the call hierarchy. If this<br />search reaches the top level of the hierarchy (the point at which the thread was<br />created), then the thread is killed and message stack trace is dumped to<br />System.err. </em></span></p><p><span style="font-size:100%;color:#000099;"><em>****************************************************************************************************<br /><br />" Use throw new xxxException() to throw an exception. If the<br />thrown object is null, a NullPointerException will be thrown at the handler. </em></span></p><p><span style="font-size:100%;color:#000099;"><em>****************************************************************************************************<br /><br />" If an exception handler re-throws an exception (throw in a catch<br />block), same rules apply. Either you need to have a try/catch within the catch<br />or specify the entire method as throwing the exception that's being re-thrown in<br />the catch block. Catch blocks at the same level will not handle the exceptions<br />thrown in a catch block - it needs its own handlers. </em></span></p><p><span style="font-size:100%;color:#000099;"><em>****************************************************************************************************<br /><br />" The method<br />fillInStackTrace() in Throwable class throws a Throwable object. It will be<br />useful when re-throwing an exception or error. </em></span></p><p><span style="font-size:100%;color:#000099;"><em>****************************************************************************************************<br /><br />" The Java language<br />requires that methods either catch or specify all checked exceptions that can be<br />thrown within the scope of that method. </em></span></p><p><span style="font-size:100%;color:#000099;"><em>****************************************************************************************************<br /><br />" All objects of type<br />java.lang.Exception are checked exceptions. (Except the classes under<br />java.lang.RuntimeException) If any method that contains lines of code that might<br />throw checked exceptions, compiler checks whether you've handled the exceptions<br />or you've declared the methods as throwing the exceptions. Hence the name <span style="FONT-WEIGHT: bold">checked exceptions</span>. </em></span></p><p><span style="font-size:100%;color:#000099;"><em>****************************************************************************************************<br /><br />" If there's no<br />code in try block that may throw exceptions specified in the catch blocks,<br />compiler will produce an error. (This is not the case for super-class Exception) </em></span></p><p><span style="font-size:100%;"><span ><span style="color:#000099;"><em>****************************************************************************************************<br /><br />" Java.lang.RuntimeException and java.lang.Error need not be handled or<br />declared.</em> </span></span></span></p><p><span style="font-size:100%;color:#000099;">****************************************************************************************************</span></p></blockquote><br /><span style="font-size:0;"><span style="font-size:100%;"><span style="font-size:130%;"><p><br /></span></span></span></p><span style="color:#000099;"><span style="font-family:arial;"><span style="font-size:100%;"><span style="FONT-STYLE: italic"><strong>Classification of Exception:<br /></strong></span><br /><br /></span></span></span><span style="color:#000099;"><span style="font-family:arial;"><span style="font-size:100%;"><strong>Exception</strong>-->ClassNotFoundException, ClassNotSupportedException, IllegalAccessException, InstantiationException, IterruptedException, NoSuchMethodException, RuntimeException, AWTException, IOException<br /><br /></span></span></span><span style="color:#000099;"><span style="font-family:arial;"><span style="font-size:100%;"><strong>RuntimeException</strong>-->EmptyStackException, NoSuchElementException, ArithmeticException, ArrayStoreException, ClassCastException, IllegalArgumentException, IllegalMonitorStateException,<br />IndexOutOfBoundsException, NegativeArraySizeException, NullPointerException, SecurityException.<br /><br /><strong>IllegalArgumentException--></strong>IllegalThreadStateException, NumberFormatException<br /><br /><strong>IndexOutOfBoundsException</strong>-->ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException<br /><br /><strong>IOException</strong>-->EOFException, FileNotFoundException, InterruptedIOException, UTFDataFormatException, MalformedURLException, ProtocolException, SockException, UnknownHostException, UnknownServiceException.</span></span></span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com1tag:blogger.com,1999:blog-7863722189611643831.post-30900290179114163812007-12-13T23:18:00.000-08:002007-12-26T04:06:09.028-08:00Notes on Java Fundamentals<span style="color:#000099;"><span style="font-family:times new roman;"><span style="font-size:100%;"><strong>Basic Lexical elements----</strong><br />--->keywords & other reserved words<br /><br />*******************************************************************<br />1.<strong> Access modifiers</strong> - private,public,protected<br />2.<strong>other modifiers</strong>---abstract,final,static,synchronized,transient, volatile<br /><strong>3 Name space keywords</strong> -- import,package<br /><br />*******************************************************************<br />byte,short,int,long-- signed two's complement integers<br />boolean -- 1 bit<br />char-- 16 bit unicode characters rather than ASCII set used in C<br /><br />********************************************************************<br />Formula ------<strong>2^(n-1) to 2^(n-1)-1` </strong><br /></span></span></span><p><strong><span style="font-family:times new roman;font-size:100%;color:#000099;">********************************************************************</span></p></strong><span style="color:#000099;"><span style="font-family:times new roman;"><span style="font-size:100%;"><strong>Exceptional Floating point constants---</strong><br /></span><br />Float.NAN </span></span></span><span style="color:#000099;"><span style="font-family:times new roman;"><span style="font-size:100%;"><strong>(NAN Stands for Not A Number)<br /></strong>Float.NEGATIVE_INFINITY<br />Float.POSITIVE_INFINITY<br />Double.NAN<br />Double.NEGATIVE_INFINITY<br />Double.POSITIVE_INFINITY<br /><br />************************************************************************<br /><br />char represented in <strong>single quotes</strong> -<br />'\u0000'--- followed by four hexadecimal<br /><br />************************************************************************<br /><br /><strong>Integral</strong> can be decimal, hexadecimal or octal<br />Hexadecimal starts with 0x<br />Octals start with 0<br /><br />************************************************************************<br />Floating point e or E stands for exponential value<br />eg 7e3 =7000<br /><br />*************************************************************************<br /><br />note--> double x=77; is valid declaration as doublex =77D;<br /><br />*************************************************************************<br /><br />To convert boolean, characters,integers,floating point numbers to String use <strong>valueOf()</strong> method<br />String is not null terminated.<br /><br />**************************************************************************<br /><br />Once initialized <strong>string objects are inmutable</strong> i.e cannot change their contents. so use stringBuffer if the contents have to be changed.<br /><br />****************************************************************************<br /><br />String bounds checked at run time rather than compile time.<br /><br />string abc ="sdefg";<br />is not same as<br />string abc =new("string");<br /><br />if we use first one compiler scans current cache to check for another string object with same literal value.<br />If it is there it reuses same object.<br /><br />********************************************************************************<br /></span></span></span></span>javaexperthttp://www.blogger.com/profile/09146063393779990705noreply@blogger.com1