0% found this document useful (0 votes)
3 views217 pages

Lara Core Java Part2

The document provides an overview of Java wrapper classes, detailing their purpose in converting primitive data types to objects and vice versa. It covers various wrapper classes, their constructors, methods for conversion, and examples of usage. Additionally, it introduces the concepts of autoboxing and unboxing in Java.

Uploaded by

hunk065
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views217 pages

Lara Core Java Part2

The document provides an overview of Java wrapper classes, detailing their purpose in converting primitive data types to objects and vice versa. It covers various wrapper classes, their constructors, methods for conversion, and examples of usage. Additionally, it introduces the concepts of autoboxing and unboxing in Java.

Uploaded by

hunk065
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 217

2010

RS TECHNOLOGIES LARA TECHNOLOGIES

RS Technologies
Core Java – Part 2
# 102/12, 2nd Main, 5th Cross, Venkateswara College Road, Ramaiah Garden, Chikka
Adugodi, Thavarakere, Bangalore – 560029.
Phone No: 080 – 4131 0124

www.javaeasy2all.com 1 08041310124

Released On
Sep 6th 2010
RS TECHNOLOGIES LARA TECHNOLOGIES

CONTENT SUMMARY

1. WRAPPER CLASSES

2. INNER CLASSES

3. ENUMS

4. EXCEPTION HANDLING

5. ASSERTIONS

www.javaeasy2all.com 2 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

WRAPPER CLASS

Wrapper classes:
 It converts primitive values to objects.
 For every primitive data type there is a corresponding wrapper class.
 It is used to convert primitive data type to derived data type or derived data type to
primitive data type.
 Primitive values in java are not objects.So in order to include the primitives in the
activities reserved for objects for ex- to be used as a elements of collections or to be
returned as objects from a method we need wrapper classes.
 All wrapper classes is their in java.lang package.
 All Wrapper classes are final and concrete.
 The objects of wrapper classes are immutable i.e their state can not be changed.
 In every wrapper class ,there is a constructor which takes corresponding primitive
data type.For ex-Inside integer wrapper class there is a constructor which takes int
primitive data type.Inside a character wrapper class there is a constructor which takes
char primitive.
 void is also a Wrapper class.But it does not wrap any primitive value and can not be
instantiable.
 Conversion of Primitive into Derived is called Boxing.
 Conversion of Derived into Primitive is called Unboxing.

Primitive Data type Wrapper class


byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

Numeric Wrapper Classes


Each numeric Wrapper class defines minimum and maximum value of the corresponding
primitive data type.
<Wrapper classname>.MIN_VALUE
<Wrapper classname>.MAX_VALUE

int max=Integer.MAX_VALUE
double max=Double.MIN_VALUE

www.javaeasy2all.com 3 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Hierarchy of Wrapper Classes

Object

Void Number Character Boolean

Byte Short Integer Long Float Double

 All of the Wrapper classes except Character provides two constructors.One that takes
a primitive of the type being constructed and other takes a String representation of the
type being constructed.
Wrapper Type(type v)
Wrapper Type(string s)

Integer intObj1=new Integer(79);


Integer intObj2=new Integer(“79”);
Float floatObj1=new Float(89.55f);
Float floatobj2=new Float(“89.55f”);
Character charObj=new Character(‘d’);//only one constructor for Character
 The constructor for the Bolean wrapper Class take either a Boolean Value true or false
or a case-insensitive string with the value “true” or “false”.
Boolean flag1=new Boolean(“false”);
Boolean flag2=new Boolean(“TruE”);

Creating Wrapper Objects

www.javaeasy2all.com 4 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

1.By Using Constructors:

A.Converting primitive values to wrapper objects


Character c1=new Character(‘c’);
Boolean b1=new Boolean(true);
Integer i1=new Integer(8000);
Double d1=new Double(8.34);

B.Converting strings to Wrapper Objects


Boolean b2=new Boolean(“True”);//true
Boolean b3=new Boolean(“xx”);//false
Integer i2=new Integer(“8000”);
Double d2=new Double(“8.45”);
Long long1=new Long(8);

2.By Using valueOf()method

Each wrapper class except Character defines the static method valueof(String s) that
returns the Wrapper object corresponding to the primitive value represented by the
string object passed as argument.It throws NumberFormatException if the string
parameter is not a valid number.

Static WrapperType valueOf(String s)


Boolean b4=Boolean.valueof(“false”);
This method is overloaded depending on different bases.
Static WrapperType valueOf(String s,int base)
Byte b1=byte.valueof(“1111”,2);
Short s2=Short.valueOf(“012”,8);
Integer i2=new Integer(“-b”,16);

Using Wrapper Conversion Utlities

1.Converting Wrapper Objects to Strings

String tostring()
Each wrapper class overrides the tostring() method from the Object class.
Character c1=new Character(‘g’);
String s1=c1.toString();
Boolean b1=new Boolean(true);
String s2=b1.toString();
2.Converting Primitive values to Strings
Each wrapper class defines a static method toString(type v) that returns the
String corresponding to the primitive value.
static String toString(type v)
String s1=Character.toString(‘\n’);

www.javaeasy2all.com 5 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

String s2=Boolean.toString(false);
String s3=Integer.toString(2000);//base 10
String s4=Double.toString(1.55);
For integer primitive types,base is assumed to be 10.
The wrapper class Integer and Long define overloaded toString() method.
3. Converting integer values to String in different Notations
Static String toBinaryString(int i)//base 2
Static String toHexString(int i)//base 16
Static String toOctalString(int i)//base 8
These three methods return an unsighned String value of base 2,16,8 with no
leading zeros.
Static String toString(int i,int base)
This method returns the minus sign(‘-‘) as the first character if I is negative.
Static String toString(int i)
This is same as toString(int i,int base) where base is 10.
4. Converting Wrapper objects to primitive values
Each Wrapper class except Boolean and Character defines a typeValue()
Which returns the primitive value in the wrapper object.
type typeValue()
double d=d1.doubleValue();
int i=i1.intValues();
5. Converting any numeric wrapper objects into any numeric primitive values
Byte b1=new Byte((byte17);
Byte b1=new Byte((byte17);
Integer i1=new Integer(2005);
Double d1=new Double(1.48);
short s1=i1.shortValue();
long l1=b1.longValue();
int i2=d1.intValue();//truncation
double d2=i1.doubleValue();
6.Converting any numeric wrapper objects into any numeric primitive values
Each numeric wrapper class defines a static method parseType(String s) that
returns the primitive numeric value of String it contains.
This method will throw a NumberFormatException if the String parameter is
not a valid argument.

1.static type parseType(String s)


byte b1=Byte.parseByte(“18”);
Int i1=Integer.parseInt(“2007”);
Int i2=Integer.parseInt(“abc”);//NFE

2.static type parseType(String s,int base)


byte b1=Byte.parseByte(“1010”,2);
short s1=Short.parseShort(“013”,8);
www.javaeasy2all.com 6 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

long l1=Long.parseLong(“-a”,16);
int i1=Integer.parseInt(“500”,16);

Examples Related To Wrapper Classes

1. Program
class Wrap1
{
public static void main (String args[])
{
int i1=5000;
Integer i2=new Integer(5000);
Integer i3=new Integer(“5000”);
Integer i4=new Integer(i1);
Integer i5=Integer.valueOf(5000);
Integer i6=Integer.valueOf(“101010”,2);
Integer i7=Integer.valueOf(“015”,8);
Integer i8=Integer.valueOf(“b”,16);
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
System.out.println(i5);
System.out.println(i6);
System.out.println(i7);
System.out.println(i8);

Output
5000
5000
5000
5000
5000
42
13
11
2. Program
class Wrap2
{
public static void main (String args[])
{
char c1=’a’;

www.javaeasy2all.com 7 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Character c2=new Character(‘c’);


Character c3=new Character(c1);
System.out.println(c2);
System.out.println(c3);

Output
c
a
3. Program
class Wrap3
{
public static void main (String args[])
{
Character c4=new Character(‘c’);
Character c5=Character.valueOf(‘d’);
System.out.println(c4);
System.out.println(c5);

Output
c
d
4. Program
class Wrap4
{
public static void main (String args[])
{
Boolean b1=new Boolean(“True”);
Boolean b2=new Boolean(“bb”);
Boolean b3=new Boolean(false);
Boolean b4=new Boolean(b1);
Boolean b5= Boolean.valueOf(“false”);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
System.out.println(b5);
}

www.javaeasy2all.com 8 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
true
false
false
true
false

5. Program
class Wrap5
{
public static void main (String args[])
{
Boolean b1=new Boolean(“True”);
if(b1)
{
System.out.println(“It is valid in jdk1.5!”);

Output
It is valid in jdk1.5!
6. Program
class Wrap6
{
public static void main (String args[])
{
Integer i1 = new Integer(“true”);
Integer i2 = Integer.ValueOf(‘c’);
Double d1=Double.valueOf(“false”);
Short s1 = Short.valueOf(“6.57”);
System.out.println(“Hello World”);

Output
CTE
7. Program
class Wrap7
{

www.javaeasy2all.com 9 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

public static void main (String args[])


{
Integer i1 = new Integer(5);
Short s1 = Short.valueOf(“100”);
Boolean b1 = new Boolean(“false”);
Double d1=Double.valueOf(“false”);
Float f1 = Float.valueOf(“7.89”);
String s2 = i1.toString();
String s3 = s1.toString();
String s4 = b1.toString();
String s5 = f1.toString();
String s6 = Character.toString(‘a’);
String s7 = Integer.toString(895);
String s8 =Double.toString(8.99);
String s9 = Boolean.toString(true);
String s10 = Float.toString(5.55f);
String s11 = Long.toString(659,16);
String s12 = Integer.toHexString(256);
byte b2 = Byte.parseByte(“17”);
int i5 = Integer.parseInt(s2);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
System.out.println(s6);
System.out.println(s7);
System.out.println(s8);
System.out.println(s9);
System.out.println(s10);

Output
NFE
8. Program
class Wrap8
{
public static void main(String[] args)
{
String s1 = Float.toString(5.55); //won’t complie
System.out.println(s1);
}
}

www.javaeasy2all.com 10 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
CTE
9. Program
class Wrap9
{
public static void main(String[] args)
{
short sh = Short.parseShort(“8.99”);
System.out.println(sh);
}
}
Output
NFE(Number Format Exception)
10. Program
class Wrap10
{
public static void main(String[] args)
{
short sh = Short.parseShort(8); // won’t complie
System.out.println(sh);
}
}

Output
CTE
11. Program
class Wrap12
{
public static void main(String[] args)
{
int i7 = Integer.parseInt(“true”); //won’t complie
System.out.println(i7);
}
}

Output
NFE(Number format Exception)
12. Program
class Wrap13
{
public static void main(String[] args)
{
double d5 = Double.parseDouble(“def”); //NFE
System.out.println(d5);

www.javaeasy2all.com 11 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
}

Output
NFE(Number format Exception)
13. Program
class Wrap14
{
public static void main (String args[])
{
Integer i1 = new Integer(50);
Integer i2 = Integer.valueOf(“60”);
Float f1 = Float.valueOf(“5.66”);
Double d1=Double.valueOf(“100.65”);
Long l1 = Long.valueOf(“101010”,2);
Boolean b1 = Boolean.valueOf(“false”);
Double d2 =d1.doubleValue();
int i3 = i1.intValue();
int i4 = i2.intValue();
long l2 = l1.longValue();
byte b2 = i1.byteValue();
short s1 =d1.shortValue();
long l3 = i1.longValue();
System.out.println(d2);
System.out.println(i3);
System.out.println(i4);
System.out.println(l2);
System.out.println(b2);
System.out.println(l3);

Output
100.65
50
60
42
50
50
14. Program
class Wrap15
{
public static void main(String[] args)
{

www.javaeasy2all.com 12 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Integer i1 = new Integer("abc");


System.out.println(i1.intValue());
}
}
Output
NFE
15. Program
class Wrap16
{
public static void main(String[] args)
{
Double d1 = Double.valueOf("abc");
System.out.println(d1.doubleValue());
}
}
Output
NFE
16. Program
class Wrap17
{
public static void main(String[] args)
{
String s1 = "300";
int i = Integer.parseInt(s1);
System.out.println(i);
}
}
Output
300

Autoboxing And AutoUnBoxing

AutoBoxing:-It is the process of conversion of primitive to its corresponding wrapper object.


As for example int primitive to its Integer object,boolean primitive to its
boolen object.
AutoUnBoxing:-It is the reverse process of AutoBoxing.

1. Program
class Manager
{
public static void main(String args[])
{
Integer i=10;
int k=i+10;

www.javaeasy2all.com 13 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(k);
}

Output
20
2. Program
class Manager1
{
static void test(int i)
{
System.out.println(i);
}
public static void main(String args[])
{
Integer i=new Integer(10);
test(i);//Auto UnBoxing
}

Output
10
3. Program
class Manager2
{
static void test(Byte b)
{
System.out.println(1);
}
public static void main(String args[])
{
byte b=10;
test(b);//Auto Boxing
}

Output
1
4. Program
class Manager2
{
static Integer i;
static void add(int k)

www.javaeasy2all.com 14 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
int m=i+k;
System.out.println(m);
}
public static void main(String args[])
{
add(10);
}

Output
CS(Null pointer exception)
5. Program
class A
{
Integer i;
int add()
{
return i+i;
}
}
class Manager
{
public static void main(String args[])
{
A a1=new A();
int i=a1.add();
System.out.println(i);
}
}
Output
CS(Null pointer exception)
6. Program
class B
{
public static void main (String args[])
{
Integer i1=new Integer(10);
int i2=i1;
System.out.println(“done”);
}

www.javaeasy2all.com 15 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
done

7. Program
class C
{
public static void main(String args[])
{
Boolean b=true;
if(b)
{
System.out.println(“yes”);
}
Boolean b1=new Boolean(“false”);
If(b1)
{
System.out.println(1);
}
else
{
System.out.println(2);
}
}
}
Output
yes
2

8. Program
class D
{
public static void main(String args[])
{
Integer i1=10;
int i2=20;
int i3=i1+i2;
System.out.println(i3);
}
}
Output
30
 If two reference variables refer to different objects then they are not equal.if they
contains same value then they are meaningfully equal.
9. Program

www.javaeasy2all.com 16 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

class Test
{
public static void main(String args[])
{
Integer i1=4678;
Integer i2=4678;
if(i1==i2)
{
System.out.println(“same Objects”);
}
else
{
System.out.println(“Different Objects”);

}
if(i1.equals(i2))
{
System.out.println(“Meaningfully equal”);
}
else
{
System.out.println(“not equal”);
}

Output
Different Objects
Meaningfully equal
10. Program
class Test1
{
public static void main(String args[])
{
Integer i1=10;
Integer i2=10;
if(i1==i2)
{
System.out.println(“same Objects”);
}
else
{
System.out.println(“Different Objects”);

www.javaeasy2all.com 17 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
if(i1.equals(i2))
{
System.out.println(“Meaningfully equal”);
}

Output
Same Objects
Meaningfully equal
11. Program
class Test1
{
public static void main(String args[])
{
Boolean b1=false;
Boolean b2=false;
if(b1= =b2)
{
System.out.println(“same Objects”);
}
if(b1.equals(b2))
{
System.out.println(“Meaningfully equal”);
}

Output
Same Objects
Meaningfully equal
 While invoking methods complier considers three things
o Autowidning
o Autoboxing
o Var-args
12. Program
class E
{
static void test(int i)
{

www.javaeasy2all.com 18 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(“int”);
}
static void test(long l)
{
System.out.println(“long”);
}

static void test(double d)


{
System.out.println(“double”);
}
public static void main(String args[])
{
byte b=10;
short s=10;
long l=10;
float f=10.0f;
test(b);
test(s);
test(l);
test(f);
}
}

Output
int
int
long
double
13. Program
class F
{
static long test(int i)
{
return i;
}
public static void main(String args[])
{
short s=10;
double d2=test(s);
System.out.println(d2);
}
}

www.javaeasy2all.com 19 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
10.0

Autoboxing and Autowidening


While invoking a method if JVM will find a method with a parameter that is wrapper
class of the argument and also a method that is wider than the argument then the
complier will choose widening over boxing
14. Program
class W1
{
static void test(Integer i)
{
System.out.println(“Integer”);
}
static void test(long l)
{
System.out.println(“long”);
}
public static void main(String args[])
{
int i=70;
test(i);
}

Output
long

15. Program
class W2
{
static void test(int i)
{
System.out.println(“int”);
}
static void test(Byte b)
{
System.out.println(“Byte”);
}
public static void main(String args[])
{
byte b=10;
test(b);
}

www.javaeasy2all.com 20 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
int

16. Program
class W3
{
static void test(double d)
{
System.out.println(“double”);
}
static void test(Integer i)
{
System.out.println(“Integer”);
}
public static void main(String args[])
{
int i=100;
test(i);
}

Output
double

17. Program
class W4
{
static void test(Long l)
{
System.out.println(“long”);
}
public static void main(String args[])
{
byte b=10;
test(b);
}

Output
CTE
18. Program

www.javaeasy2all.com 21 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

class W4
{
static void test(long l)
{
System.out.println(“long”);
}
public static void main(String args[])
{
byte b=10;
test(b);
}

Output
long
19. Program
class W5
{
static void test(Object o)
{
System.out.println(s);
}
public static void main (String args[])
{
short s=10;
test(s);
}
}

Output
CTE

In this case it follows autoboxing then autowidening.While invoking short primitive


was boxed to short which is widened to object as it satisfies IS-A relationship.
Autowidening and var-args
20. Program
class W6
{
static void test(int i,int j)
{
System.out.println(“integer”);
}
static void test(byte … b)
{

www.javaeasy2all.com 22 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(“byte”);
}
public static void main (String args[])
{
byte b=10;
test(b,b);
}

Output
integer

Autoboxing and var-args

21. Program
class W7
{
static void test(Byte b,Byte i)
{
System.out.println(“Byte”);
}
static void test(byte … b)
{
System.out.println(“byte”);
}
public static void main (String args[])
{
byte b=10;
test(b,b);
}
}

Output
Byte
22. Program
class W8
{
static void test(Byte b,Byte b)
{
System.out.println(“Byte”);
}
static void test(byte … b)
{
System.out.println(“byte”);

www.javaeasy2all.com 23 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
public static void main (String args[])
{
byte b=10;
test(b,b);
}
}

Output
CTE
23. Program
class Wrap15
{
public static void main (String args[])
{
A a=new A();
a.test((byte)10);
}
}
class A
{
void test(byte b)
{
System.out.println(“bYte”);
}
void test(int i)
{
System.out.println(“int”);
}
void test(Byte b)
{
System.out.println(“byte”);
}
void test(byte … b)
{
System.out.println(“VAR ARGS”);
}
}

Output
byte
24. Program
class Wrap16
{

www.javaeasy2all.com 24 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

public static void main (String args[])


{
A a=new A();
a.test((byte)10);
}
}
class A
{
void test(int i)
{
System.out.println(“int”);
}
void test(Byte b)
{
System.out.println(“Byte”);
}
void test(byte … b)
{
System.out.println(“VAR ARGS”);
}
}

Output
int
25. Program
class Wrap17
{
public static void main (String args[])
{
A a=new A();
a.test((byte)10);
}
}
class A
{
void test(Byte b)
{
System.out.println(“bYte”);
}
void test(byte … b)
{
System.out.println(“VAR ARGS”);
}
}

www.javaeasy2all.com 25 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
bYte
26. Program
class Wrap18
{
public static void main (String args[])
{
A a=new A();
a.test((byte)10);
}
}
Class A
{
void test(byte … b)
{
System.out.println(“VAR ARGS”);
}
}

Output
VAR ARGS
27. Program
class Wrap19
{
public static void main (String args[])
{
A a=new A();
a.test();
a.test(25);
}
}
Class A
{
void test()
{
System.out.println(“NO_ ARGS”);
}
void test(int … i)
{
System.out.println(“VAR ARGS”);
}
}

Output

www.javaeasy2all.com 26 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

NO_ ARGS
VAR ARGS
28. Program
class Wrap20
{
public static void main (String args[])
{
A a=new A();
a.test();
a.test(25);
}
}
Class A
{
void test(int … i)
{
System.out.println(“VAR ARGS”);
}
}

Output
VAR ARGS
VAR ARGS
29. Program
class Wrap21
{
public static void main (String args[])
{
A a=new A();
a.test();
a.test(25,10);
a.test(1,25,10);
a.test(5,7,25,10);
}
}
class A
{
void test(int … x)
{
System.out.println(x.length);
for(int i: x)
{
System.out.println(i);
www.javaeasy2all.com 27 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
for(int i=0;i<x.length;i++)
{
System.out.println(x[i]);
}
}

Output

0
2
25
10
25
10
3
1
25
10
1
25
10
4
5
7
25
10
5
7
25
10

30. Program
class Wrap22
{
public static void main (String args[])
{
System.out.println(“Hello World”);
}
}

Output
Hello World
31. Program
class Wrap23

www.javaeasy2all.com 28 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
public static void main (String … args)
{
System.out.println(“Hello World”);
}
}

Output
Hello World
32. Program
class Wrap24
{
void test(String s1,int … x)
{
System.out.println(x.length);
}
public static void main(String args[])
{
Wrap24 a=new Wrap24();
a.test(“xyz”);
a.test(“abc”,10);
a.test(“123”,123,456);
}
}

Output
0
1
2
33. Program
class Wrap25
{
public static void main (String args[])
{
System.out.println(“Arrays”);
}
public static void main (String … args)
{
System.out.println(“var args”);
}
}

Output
CTE(Dublicate main method)

www.javaeasy2all.com 29 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

34. Program
class Wrap26
{
int test(byte all[])
{
System.out.println(“Arrays”);
return 10;
}
double test(byte … args)
{
System.out.println(“var args”);
return 10.0;
}
public static void main(String … x)
{
Wrap26 a=new Wrap26();
a.test((byte)10);
}

Output
CTE
35. Program
class Wrap27
{
public static void main(String args[])
{
byte b=100;
test(b);
}
static void test(int f)
{
System.out.println(“int”);
}
static void test(byte d)
{
System.out.println(“byte”);
}
static void test(byte … d)
{
System.out.println(“byte…”);
}
}

www.javaeasy2all.com 30 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
byte
36. Program
class Wrap28
{
public static void main(String args[])
{
byte b=100;
test(b);
}
static void test(int f)
{
System.out.println(“int”);
}
static void test(byte … d)
{
System.out.println(“byte…”);
}

Output
int
37. Program
class Wrap29
{
public static void main(String args[])
{
byte b=100;
test(b);
}
static void test(int f)
{
System.out.println(“int”);
}
static void test(Byte d)
{
System.out.println(“Byte”);
}
static void test(byte … d)
{
System.out.println(“byte…”);
}

www.javaeasy2all.com 31 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
int
38. Program
class Wrap30
{
public static void main(String args[])
{
byte b=100;
test(b);
}
static void test(Byte d)
{
System.out.println(“Byte”);
}
static void test(byte … d)
{
System.out.println(“byte…”);
}

Output
Byte
39. Program
class Wrap31
{
public static void main(String args[])
{
byte b=100;
test(b);
}
static void test(byte … d)
{
System.out.println(“byte…”);
}

Output
byte...
40. Program
class Wrap32
{
public static void main(String args[])
{

www.javaeasy2all.com 32 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Integer i=null;
method(i);
}
static void method(int k)
{
System.out.println(k);
}

Output
NullPointer Exception
41. Program
class Wrap33
{
public static void main(String args[])
{
byte b=10;
method(b);
}
static void method(int i)
{
System.out.println(“primitive Type call”);
}
static void method(Integer i)
{
System.out.println(“Wrapper Type call”);
}

Output
Primitive type call
42. Program
class Wrap34
{
public static void main(String args[])
{
int i=10;
method(i);
}
static void method(Long l)
{
System.out.println(“Widening conversion”);
}

www.javaeasy2all.com 33 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
Compiler error
43. Program
class Wrap35
{
public static void main(String args[])
{
Integer i=10;
Integer j=10;
System.out.println(i==j);
System.out.println(i.equals(j));
}

Output
true
true
44. Program
class Wrap35
{
public static void main(String args[])
{
Integer i=200;
Integer j=200;
System.out.println(i==j);
System.out.println(i.equals(j));
}

Output
false
true
45. Program
class Wrap36
{
public static void main(String args[])
{
Boolean b1= new Boolean(true);
Boolean b2= new Boolean(true);
Boolean b3= true;
Boolean b4=true;

www.javaeasy2all.com 34 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(b1==b2);
System.out.println(b1==b3);
System.out.println(b3==b4);
System.out.println(b1==b4);
}

Output
false
false
true
false
46. Program
class Wrap37
{
public static void main(String args[])
{
int i=10;
method(i);
}
static void method(long l)
{
System.out.println(“long called”);
}
static void method(Integer i)
{
System.out.println(“Integer called”);
}

Output
Long called
47. Program
class Wrap39
{
public static void main(String args[])
{
Integer i=10;
int k=10;
method(i,k);
}

www.javaeasy2all.com 35 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

static void method(int i,Integer k)


{
System.out.println(“int,Integer called”);
}
static void method(int i,int k)
{
System.out.println(“int,int called”);
}
static void method(Integer i,Integer k)
{
System.out.println(“Integer,Integer called”);
}
static void method(Integer i,int k)
{
System.out.println(“Integer,int called”);
}

Output
Integer,int called
48. Program
class Wrap40
{
public static void main(String args[])
{
int i=10;
method(i);
}
static void method(Object o)
{
System.out.println(“Object called”);
}
static void method(Number n)
{
System.out.println(“Number called”);
}

Output
Number called
49. Program
class Wrap41
{

www.javaeasy2all.com 36 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

static void method(byte b)


{
System.out.println(“byte”);
}
static void method(short s)
{
System.out.println(“short”);
}
static void method(int i)
{
System.out.println(“int”);
}
static void method(long l)
{
System.out.println(“long”);
}
static void method(float f)
{
System.out.println(“float”);
}
static void method(double d)
{
System.out.println(“double”);
}
static void method(Byte b)
{
System.out.println(“Byte”);
}
static void method(Double d)
{
System.out.println(“Double”);
}
static void method(number n)
{
System.out.println(“number”);
}
static void method(Object obj)
{
System.out.println(“object”);
}
static void method(byte … b)
{
System.out.println(“var args”);
}
www.javaeasy2all.com 37 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

public static void main(String args[])


{
byte b=10;
method(b);
}

Output
Byte
50. Program
Public class Bar
{
static void foo(int … x)
{
//insert code here
}

}
Which two code fragments,inserted indepently at line 5 will alow the class to
compile?(Choose two)
A. foreach(x) System.out.println(z);
B. for(int z : x)System.out.println(z);
C. while(x.hasNext())System.out.println(x.next());
D. for (int i=0; i<x.length; i++)System.out.println(x[i]);

Answer BD
51. Program
public class A
{
public String doit(int x,int y)
{
return “a”;
}
public string doit(int … vals)
{
return “b”;
}
}

Given:
A a=new A();
System.out.println(a.doit(4,5));
What is the result?
A. Line 12 prints “a” to System.out

www.javaeasy2all.com 38 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

B. Line 12 prints “b” to System.out.


C. An exception is thrown at line 13 at runtime.
D. Compilation of class A will fail due to an error in line 6
Answer : A
52. Program
public class Boxer1
{
Integer i;
int x;
public Boxer1(int y)
{
x=i+y;
System.out.println(x);
}
public static void main(String args[])
{
new Boxer1(new Integer(4));
}
}
What is the result?
A. The value “4” is printed at the command line.
B. Compilation fails because of an error in line 7.
C. Compilation fails because of an error in line 12.
D. A NullPointerException occurs at runtime.
E. A NumberFormatException occurs at runtime.
F. An IllegalStateException occurs at runtime.
Answer: D
53. Program
public class Yikes
{
public staic void go(Long n)
{
System.out.println(“Long”);
}
public staic void go(Short n)
{
System.out.println(“Short”);
}
public staic void go(int n)
{
System.out.println(“int”);
}
public static void main(String args[])
{
www.javaeasy2all.com 39 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

short y=6;
long z=7;
go(y);
go(z);
}
}
What is the result?
A. int Long
B. short Long
C. Compilation fails
D. An Exception is thrown at runtime
Answer: A
54. Program
public class Wow
{
public static void go(short n)
{
System.out.println(“short”);
}
public static void go(Short n)
{
System.out.println(“SHORT”);
}
public static void go(Long n)
{
System.out.println(“LONG”);
}
public static void main(String args[])
{
Short y=6;
int z=7;
go(y);
go(z);
}
}
What is the result?
A. short LONG
B. SHORT LONG
C. Compilation fails
D. An Exception is thrown at runtime
Answer: C
55. Program
public static int sum(List list)
{
www.javaeasy2all.com 40 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

int sum=0;
for(Iterator iter=list.iterator(); iter.hasnext();)
{
int i= ((Integer)iter.next()).intValue();
sum+=i;
}
return sum;
}
Which three changes must be made to the method sum to use generic?(Choose three)
A. Remove line 6
B. Replace line 6 with “int i=iter.next();”
C. Replace line 4 with “for(int i: intlist) {“
D. Replace line 4 with “for(Iterator iter : intlist) {“
E. Replace the method declaration with “sum(List<int> intList)”
F. Replace the method declaration with “sum(List<Integer>intList)”
Answer: ACF
56. Program
public void genNumbers()
{
ArrayList numbers=new ArrayList();
for(int i=0; i<10; i++)
{
int value=i*((int)Math.random());
Integer intObj=new Integer(value);
numbers.add(intObj);
}
System.out.println(numbers);
}
Which line of code marks the earliest point that an object referenced by intObj
becomes a candidate for garbage collection?
A. Line 8
B. Line 9
C. Line 10
D. Line 11
E. The object is NOT a candidate for garbage collection
Answer: D
57. Program
public class MyClass
{
public Integer startingI;
public void methodA()
{
Integer i=new Integer(25);
startingI=i;
www.javaeasy2all.com 41 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

methodB(i);
}
private void methodB(Integer i2)
{
i2=i2.intValue();
//line 13. insert code here
}
}
If method is invoked ,which two are true at line 13?(Choose two.)
A. i2==startingI returns true.
B. i2==startingI returns false.
C. i2.equals(startingI) returns true.
D. i2.equals(startingI) returns false.
Answer: BC

www.javaeasy2all.com 42 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

INNER CLASS:

 Any of the executable block of that particular class is called as inner class.(or)A class
which is a part of another class .
 Inner class is a class written within in another class.

 Basically there are 4 types of inner class


1. Global--non static
2. Global--static
3. local
4. anonymous

 T he first type of inner class is non static inner class it is directly a part of class.
 We can access all the members of the outer class except static members.
 It is also called as instance inner class.
 We can’t define static members. Only it can be allowed non static members.
 We can keep inner class upto n-number of class inside one class .
 We can keep any access level in this type of inner class but static access modifier is
not at all allowed.
 The second type of inner class is static inner class which should be a mandatory part
of a file-class only.
 We can keep all access levels & access modifiers even static also .
 We can keep even a main method inside the static inner class.
 Both abstract and final are allowed with static.
 We can’t keep a static inner class in any of the executable blocks even in SIB. it
should be member of a file-class only.
 The third type of inner class is local inner class. You can declare an inner class within
the the body of a method. such a class is known as a local inner class.
 Local inner class is a part of any executable block.
 Inside a method body we can’t use private ,protected ,public ,static ,transient and
volatile keywords.
 Inside method body final can be synchronized.
 Non final local variable should not be used inside a local inner class.
 Only final local variable should be used inside local inner class.
 In Local inner class we can’t declare or define static members.
 The fourth type of inner class is anonymous inner class.
 anonymous inner class does’t have any name.
 we can’t create constructor of anonymous inner class.
 We have to use only super class constructor.
 Inside anonymous inner class we can keep multiple IIBs.
 For every class .class file will be generated.
 Inside a anonymous inner class we can’t use static.

www.javaeasy2all.com 43 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Instance inner class:

58. Program
class A
{
class B
{
int i;
}
public static void main(String[] args)
{
B b1=new B();
System.out.println(b1.i);
}

}
Output
Compile time errors

59. Program
class Outer
{
int outer_x=100;
void test()
{
Inner in=new Inner();
{
in.display();
}
}
class Inner
{
void display()
{
System.out.println("disply:outr_x="+outer_x);
}
}
}
public class InnerClassDemo
{
public static void main(String[] args)
{
Outer out=new Outer();
out.test();

www.javaeasy2all.com 44 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
}
Output
disply:outr_x=100
60. Program
class A
{
class B
{
int i=10;
}
public static void main(String[] args)
{
B b1=new B();
System.out.println(b1.i);
}

}
Output
Compile time error
61. Program
class A
{
class B
{
int i=10;
}
public static void main(String[] args)
{
A a1=new A();
A.B b1=a1.new B();
System.out.println(b1.i);
}
}
Output
10

62. Program
class A
{
int i=20;
class B
{
int i=10;
}

www.javaeasy2all.com 45 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

public static void main(String[] args)


{
A a1=new A();
A.B b1=a1.new B();
System.out.println(b1.i);
}
}
Output
10
63. Program
class A
{
int i=20;
class B
{
int i=10;
{
i=30;
}
}
public static void main(String[] args)
{
A a1=new A();
A.B b1=a1.new B();
System.out.println(b1.i);
}
}
Output
30

64. Program
class A
{
int i=10;
class B
{
static int i=20;
}
public static void main(String[] args)
{
A a1=new A();
A.B b1=a1.new B();
System.out.println(b1.i);
}

www.javaeasy2all.com 46 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
Compile time error

65. Program
class A
{
int i=10;
class B
{
static final int i=20;
}
public static void main(String[] args)
{
A a1=new A();
A.B b1=a1.new B();
System.out.println(b1.i);
}
}
Output
20
66. Program
class A
{
int i=20;
class B
{
void test()
{
System.out.println("Test-method");
}
}
public static void main(String[] args)
{
A a1=new A();
A.B b1=a1.new B();
b1.test();
System.out.println("done");
}
}
Output
Test-method
done.

www.javaeasy2all.com 47 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

67. Program
class A
{
static int i=20;
class B
{
void test()
{
System.out.println(A.i);
System.out.println("Test-method");
}
}
public static void main(String[] args)
{
A a1=new A();
A.B b1=a1.new B();
b1.test();
System.out.println("done");
}
}

Output
20
Test-method
done.

68. Program
class A
{
class B
{
void test()
{
System.out.println("Test-method");
}
static void test1()
{
System.out.println("Test1-method");
}
}

public static void main(String[] args)


{
A a1=new A();

www.javaeasy2all.com 48 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

A.B b1=a1.new B();


b1.test();
System.out.println("done");
}
}
Output
Compile time error

69. Program
class A
{
static int i=10;
static int j=20;
class B
{
void test()
{
System.out.println(i);
System.out.println(j);
}
}

public static void main(String[] args)


{
A a1=new A();
A.B b1=a1.new B();
b1.test();
}
}
Output
10
20

70. Program
class A
{
static int i=10;
static int j=20;
class B
{
void test()
{
System.out.println(i);
System.out.println(j);
}

www.javaeasy2all.com 49 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

class C
{
void test()
{
System.out.println(i);
System.out.println(j);
System.out.println("C-test");
}
}
}
public static void main(String[] args)
{
A a1=new A();
A.B b1=a1.new B();
A.B.C c1=b1.new C();
b1.test();
c1.test();
}
}
Output
10
20
10
20
C-test

71. Program
class A
{
static int i=10;
static int j=20;
class B
{
void test()
{
System.out.println(i);
System.out.println(j);
}
class C
{
void test()
{
int i=30;
int j=40;

www.javaeasy2all.com 50 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(i);
System.out.println(j);

}
}
}

public static void main(String[] args)


{
A a1=new A();
A.B b1=a1.new B();
A.B.C c1=b1.new C();
b1.test();
c1.test();
}
}
Output
10
20
30
40

72. Program
class A
{
private int i=10;
private int j=20;
private class B
{
void test()
{
System.out.println(i);
System.out.println(j);
}
}
public static void main(String[] args)
{
A a1=new A();
A.B b1=a1.new B();
b1.test();
}
}
Output
10

www.javaeasy2all.com 51 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

20

73. Program
public class A
{
private int i=10;
private int j=20;
public class B
{
void test()
{
System.out.println(i);
System.out.println(j);
}
}
public class C
{
void test()
{
System.out.println(i);
System.out.println(j);
}
}
public static void main(String[] args)
{
A a1=new A();
A.B b1=a1.new B();
A.C c1=a1.new C();
b1.test();
c1.test();
}
}
Output
10
20
10
20

74. Program
public class A
{
private int i;
private int j;
interface D

www.javaeasy2all.com 52 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
int i=20;
void test();
}
class E implements D
{
void test()
{
System.out.println("Test");
System.out.println(i);
System.out.println(j);
}
}
public static void main(String[] args)
{
A a1=new A();
A.E e1=a1.new E();
e1.test();
}
}

Output
Compile time error

75. Program
public class A
{
private int i;
private int j;
interface D
{
int i=20;
void test();
}
class E implements D
{
public void test()
{
System.out.println("Test");
System.out.println(i);
System.out.println(j);
}
}
public static void main(String[] args)

www.javaeasy2all.com 53 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
A a1=new A();
A.E e1=a1.new E();
e1.test();
}
}
Output
Test
20
10

76. Program
public class A
{
abstract class D
{
int i=20;
int j;
void test();
}
class E extends D
{
public void test()
{
System.out.println("test");
}
}
public static void main(String[] args)
{
A a1=new A();
A.E e1=a1.new E();
e1.test();
}
}
Output
Compile time error

77. Program
public class A
{
abstract class D
{
int i=20;
int j;
void test();

www.javaeasy2all.com 54 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
class E extends D
{
public void test()
{
System.out.println("Test");
}
}
public static void main(String[] args)
{
A a1=new A();
A.E e1=a1.new E();
e1.test();
}
}
Output
Test

78. Program
public class A
{
final class D
{
int i=20;
int j;
void test()
{
System.out.println("hello");
}
}
class E extends D
{
public void test()
{
System.out.println("test");
}
}
public static void main(String[] args)
{
A a1=new A();
A.E e1=a1.new E();
e1.test();
}
}

www.javaeasy2all.com 55 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
Compile time error

79. Program
final class A
{
final class B
{
final class C
{
final class D
{
void test()
{
System.out.println("class-D");
}
}
}
}
public static void main(String[] args)
{
A a1=new A();
A.B b1=a1.new B();
A.B.C c1=b1.new C();
A.B.C.D d1=c1.new D();
d1.test();
System.out.println("done");
}
}
Output
Class-D
Done.

80. Program
interface Foo
{
int bar();
}
public class Beta
{
class A implements Foo
{
public int bar()
{
return 1;

www.javaeasy2all.com 56 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
}
public int fubar(Foo foo)
{
return foo.bar();
}
public void testFoo()
{
System.out.println(fubar(new A()));
}
public static void main(String[] args)
{
new Beta().testFoo();
}
}
Output
1

81. Program
class Line
{
public class Point
{
public int x,y;
}
public Point getPoint()
{
System.out.println("point");
return new Point();
}
}
class Triangle
{
public Triangle()
{
Line.Point p=(new Line().getPoint());
}
public static void main(String[] args)
{
Triangle t=new Triangle();
}
}
Output
Point

www.javaeasy2all.com 57 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

static inner class:

82. Program
package com.lara;
public class A
{
int i;
static int j;
static class C
{
void test()
{
System.out.println(i);
System.out.println(j);
}
}
public static void main(String[] args)
{
A a1=new A();
A.C c1=new C();
c1.test();
}
}
Output
Compile time error

83. Program
package com.lara;
public class A
{
static int i;
static int j;
static class C
{
void test()
{
System.out.println(i);
System.out.println(j);
}
}
public static void main(String[] args)
{
A a1=new A();
A.C c1=new C();

www.javaeasy2all.com 58 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

c1.test();
}
}
Output
0
0
84. Program
package com.lara;
public class A
{
static class C
{
int i=10;
int j=20;
void test()
{
System.out.println(i);
System.out.println(j);
}
}
public static void main(String[] args)
{
A a1=new A();
A.C c1=new C();
c1.test();
}
}
Output
10
20

85. Program
package com.lara;
public class A
{
static int i;
static int j;
static class C
{
static void test1()
{
test2();
}
void test2()
{
www.javaeasy2all.com 59 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(i);
System.out.println(j);
}
}
public static void main(String[] args)
{
A a1=new A();
A.C c1=new C();
c1.test1();
}
}
Output
Compile time error
86. Program
package com.lara;
class A
{
private static void test()
{
System.out.println(2);
}
static class B
{
void test()
{
System.out.println(1);
A.test();
}
}
public static void main(String[] args)
{
B b1=new B();
b1.test();
}
}
Output
1
2
87. Program
package com.lara;
class A
{
public A()
{
System.out.println(1);
www.javaeasy2all.com 60 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
static class B
{
static int i;
public void A()
{
System.out.println(i);
}
}
public static void main(String[] args)
{
System.out.println(B.i);
}
}
Output
0
88. Program
package com.lara;
class A
{
public A()
{
System.out.println(1);
}
static class B extends A
{
static float i=12.33f;
public static void A()
{
System.out.println(i);
}
}
public static void main(String[] args)
{
B.A();
System.out.println(B.i);
}
}
Output
12.33
12.33

89. Program
public class A

www.javaeasy2all.com 61 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
public A()
{
System.out.println("1");
class B
{
B()
{
System.out.println("2");
}
B b1=new B();
}
}
public static void main(String[] args)
{
A a1=new A();
}
}
Output
1

90. Program
package com.lara;
class A
{
public A()
{
System.out.println(1);
}
static class B extends A
{
static byte i=128;
public static void A()
{
System.out.println(i);
}
}
public static void main(String[] args)
{
B.A();
System.out.println(B.i);
}
}

www.javaeasy2all.com 62 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
compile time error
91. Program
public class A
{
public A()
{
System.out.println("1");
class B
{
B()
{
System.out.println("2");
}
}
B b1=new B();
}
public static void main(String[] args)
{
A a1=new A();
}
}
Output
1
2
92. Program
class A
{
public static void test()
{
System.out.println(1);
return;
}
static class B
{
public void test()
{
System.out.println(2);
}
}
public static void main(String[] args)
{
A a1=new A();
B b1=new B();
a1.test();
www.javaeasy2all.com 63 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

b1.test();
}
}
Output
1
2
93. Program
class A
{
public static void test()
{
System.out.println(1);
return;
}
static class B extends A
{
public void test()
{
System.out.println(2);
}
}
public static void main(String[] args)
{
A a1=new A();
B b1=new B();
a1.test();
b1.test();
}
}
Output
Compile time error
94. Program
class A
{
static
{
class B
{
public void main()
{
System.out.println(“main”);
}
}
}

www.javaeasy2all.com 64 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

public static void main(String[] args)


{
B a1=new B();
b1.main();
}
}
Output
Compile time error
95. Program
static class A
{
static void test()
{
System.out.println(1);
}
class B
{
public void main()
{
System.out.println(1);
}
}
public static void main(String[] args)
{
B.main();
}
}
Output
Compile time error
96. Program
class A
{
A(int i)
{
System.out.println(i);
}
static class B extends A
{
B()
{
super.A(10);
}
}
public static void main(String[] args)

www.javaeasy2all.com 65 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
B b1=new B();
}
}
Output
Compile time error
97. Program
class A
{
static class B
{
protected static void test()
{
System.out.println(1);
}
}
static class C
{
public static void test1()
{
System.out.println(2);
B b1=new B();
b1.test();
}
}
public void testmain()
{
C c1=new C();
c1.test1();
}
public static void main(String[] args)
{
System.out.println("main");
A a1=new A();
a1.testmain();
}
}
Output
Main
2
1
98. Program
class A
{

www.javaeasy2all.com 66 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

static class B
{
static void B()
{
{
System.out.println("IIB1");
}
{
System.out.println("IIB2");
}
}
}
public static void main(String[] args)
{
System.out.println(1);
B b1=new B();
b1.B();
}
}
Output
1
IIB1
IIB2

Local inner class:

99. Program
class A
{
public static void main(String[] args)
{
int i=10;
class Local
{
void test()
{
System.out.println(i);
}
}
}
}
Output
Compile time error

www.javaeasy2all.com 67 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

100. Program
class A
{
public static void main(String[] args)
{
final int i=10;
class Local
{
static void test()
{
System.out.println(i);
}
}
Local.test();
}
}
Output
Compile time error
101. Program
class A
{
public static void main(String[] args)
{
final int i=10;
static class Local
{
void test()
{
System.out.println(i);
}
}
Local l1 = new Local();
l1.test();
}
}
Output
Compile time error
102. Program
class A
{
public static void main(String[] args)
{
final int i=10;
abstract class Local

www.javaeasy2all.com 68 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
void test()
{
System.out.println(i);
}
abstract void method();
}

}
}
Output
Compile time success
103. Program
class A
{
public static void main(String[] args)
{
final int i=10;
final class Local
{
void test()
{
System.out.println(i);
}
}

}
}
Output
Compile time success
104. Program
class A
{
public static void main(String[] args)
{
System.out.println(1);
public class Local
{
void test()
{
System.out.println(2);
}

www.javaeasy2all.com 69 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
}
Output
Compile time error

105. Program
class A
{
public static void main(String[] args)
{
System.out.println(1);
class Local
{
private void test()
{
System.out.println(2);
}

}
Local l1 = new Local();
l1.test();

}
}
Output
1
2
106. Program
class A
{
public static void main(String[] args)
{
System.out.println(1);
abstract class B
{
abstract void test();
public void main(String[] array)
{
System.out.println(3);
}
}
B b1 = new B()
{
www.javaeasy2all.com 70 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

void test()
{
System.out.println(2);
}
};
b1.test();
b1.main(args);

}
}

Output
1
2
3
107. Program
class A
{
void testA()
{
System.out.println("A");
}
public static void main(String[] args)
{
class B extends A
{
void testB()
{
System.out.println("B");
}
}
B b1 = new B();
b1.testA();
b1.testB();
}
}
Output
A
B
108. Program
class A
{
public A()
{
final int i = 10;
www.javaeasy2all.com 71 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(i);
class B
{
protected void test()
{
System.out.println("test:"+i);
}
}
B b1 = new B();
b1.test();
}
public static void main(String[] args)
{
A a1 = new A();
}

}
Output
10
test:10

109. Program
interface I
{
void method();
}
class A
{
public A()
{
class C implements I
{
public void method()
{
System.out.println("method");
}
}
C c1 = new C();
c1.method();
}
public static void main(String[] args)
{
A a1 = new A();
}

www.javaeasy2all.com 72 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
Output
Method

110. Program
class A
{
public static void main(String[] args)
{
System.out.println(1);
class B
{
public static void main(String[] args)
{
System.out.println("2");
}
}
}
}
Output
Compile time error

111. Program
public class A
{
private static int i=10;
public static void main(String[] args)
{
class B
{
public void test(String[] args)
{
i+=10;
System.out.println(i);
}
}
B b1=new B();
b1.test(args);
System.out.println(i);
}
}
Output
20
20
www.javaeasy2all.com 73 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

112. Program
class A
{
A()
{
System.out.println(10);
}
public static void main(String[] args)
{
class B
{
B()
{
super();
System.out.println("B()");
}
}
B b1=new B();
}
}
Output
B()

113. Program
public class A
{
public static int i=10;
public static void main(String[] args)
{
class B
{
public void test(String[] args)
{
System.out.println(i);
}
}
B b1=new B();
b1.test(args);
System.out.println(i);
}
}
Output
10
10

www.javaeasy2all.com 74 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

114. Program
public class B
{
public static int i=10;
public static void main(String[] args)
{
static class C
{
public void hello()
{
System.out.println("hello");
}
}
C c1=new C();
c1.hello();
}
}
Output
Compile time error

115. Program
public class B
{
public static int i=10;
public static void main(String[] args)
{
class C
{
public void hello()
{
System.out.println("hello");
}
}
C c1=new C();
c1.hello();
}
}
Output
Hello

116. Program
public class B
{
public static int i=10;

www.javaeasy2all.com 75 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

public static void main(String[] args)


{
private class C
{
private C()
{
System.out.println("local");
}
}
C c1=new C();
System.out.println("main");
}
}
Output
Compile time error

117. Program
public class B
{
public static int i=10;
public static void main(String[] args)
{
class C
{
private C()
{
System.out.println("local");
}
}
C c1=new C();
System.out.println("main");
}
}
Output
local
main

118. Program
package com.java;
interface Pizza
{
int bar();
}
public class AA
{
www.javaeasy2all.com 76 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

public int spirt(Pizza p)


{
return p.bar();
}
public void money()
{
class Target implements Pizza
{
public int bar()
{
return 2;
}
}
System.out.println(spirt(new Target()));
}
public static void main(String[] args)
{
new AA().money();
}
}
Output
2
119. Program
public class B
{
private B()
{
System.out.println(3);
}
public static void main(String[] args)
{
class C
{
private C()
{
System.out.println(2);
new B();
}
}
System.out.println(1);
C c1=new C();
}
}

www.javaeasy2all.com 77 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
1
2
3
120. Program
class MyOuter
{
private String x="Outr2";
void doStuff()
{
class MyInner
{
public void SeeOuter()
{
System.out.println("Outer x is" + x);
}
}
}
}
Output
Compile time success

121. Program
class A
{
private A()
{
System.out.println(2);
class D
{
private D()
{
System.out.println(3);
}
}
D d1 = new D();
}
public static void main(String[] args)
{
System.out.println(1);
A a1 = new A();
}

www.javaeasy2all.com 78 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Output
1
2
3
122. Program
public class A
{
private static int i;
private void test()
{
System.out.println(2);
}
public static void main(String[] args)
{
class B extends A
{
void test()
{
System.out.println(1);
}
}
B b1=new B();
b1.test();
A a1=new A();
a1.test();
}
}
Output
1
2

Anonymous inner class :

123. Program
package com.rst;
class A
{
void test()
{
System.out.println("A");
}
}
public class Manager
{
public static void main(String[] args)

www.javaeasy2all.com 79 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
A a1=new A();
A a2=new A()
{
public void test()
{
System.out.println("A-I");
}
};
a1.test();
a2.test();
}
}
Output
A
A-I

124. Program
class Popcorn
{
public void pop()
{
System.out.println("popcorn");
}
}
class Food
{
public static void main(String[] args)
{
Popcorn p=new Popcorn();
Popcorn p1=new Popcorn()
{
private void pop()
{
System.out.println("anonymous popcorn");
}
};
p.pop();

}
}
Output
Compile time error

www.javaeasy2all.com 80 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

125. Program
class Popcorn
{
public void pop()
{
System.out.println("popcorn");
}
}
class Food
{
public static void main(String[] args)
{
Popcorn p=new Popcorn();
Popcorn p1=new Popcorn()
{
public void pop()
{
System.out.println("anonymous popcorn");
}
};
p.pop();
p1.pop();
}
}
Output
popcorn
anonymous popcorn

126. Program
package com;
class Person
{
private void test()
{
System.out.println("person");
}
}
public class Manager
{
public static void main(String[] args)
{
Person p1=new Person();
Person p2=new Person()
{

www.javaeasy2all.com 81 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

public void test()


{
System.out.println("Manager");
}
};
p1.test();
p2.test();
}
}
Output
Compile time error

127. Program
package com;
class Person
{
protected void test()
{
System.out.println("person");
}
}
public class Manager
{
public static void main(String[] args)
{
Person p1=new Person();
Person p2=new Person()
{
public void test()
{
System.out.println("Manager");
}
};
p1.test();
p2.test();
}
}
Output
Person
Manager

128. Program
package com;
abstract class A
{
www.javaeasy2all.com 82 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

void test1()
{
System.out.println(1);
}
}
abstract class B
{
abstract void test2();
}
interface C
{
void test1();
}
class Manager
{
public static void main(String[] args)
{
A a1=new A()
{
};
B b1=new B()
{
public void test2()
{
System.out.println(2);
}
};
C c1=new C()
{
public void test1()
{
System.out.println(3);
}
};
a1.test1();
b1.test2();
c1.test1();
}
}
Output
1
2
3
129. Program

www.javaeasy2all.com 83 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

package com;
class A
{
void test1()
{
System.out.println(1);
}
}
class Manager
{
static void print(A a1)
{
a1.test1();
}
public static void main(String[] args)
{
A a1=new A();
print(a1);
A a2=new A()
{
void test1()
{
System.out.println(2);
}
};
print(a2);
print(new A()
{
void test1()
{
System.out.println(3);
}
});
}
}
Output
1
2
3

130. Program
package com;
class A
{
int test()
www.javaeasy2all.com 84 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
return 10;
}
}
class Manager
{
public static void main(String[] args)
{
int i=test1();
System.out.println(i);
i=test2();
System.out.println(i);
}
static int test1()
{
return new A().test();
}
static int test2()
{
return new A()
{
int test()
{
return 20;
}
}.test();
}
}
Output
10
20
131. Program
package com;
interface A
{
void test1();
}
class Manager
{
static A method1()
{
return new A()
{
public void test1()

www.javaeasy2all.com 85 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
System.out.println("A-test1");
}
};
}
public static void main(String[] args)
{
A a1=method1();
a1.test1();
}
}
Output
A-test1
132. Program
package com.lara;
interface Switch
{
void on();
void off();
}
class Fan
{
private boolean runningstatus;
public Switch getSwitch()
{
return new Switch()
{
public void on()
{
runningstatus=true;
}
public void off()
{
runningstatus=false;
}
};
}
public boolean runningStatus()
{
return runningstatus;
}
}
public class FanController
{
public static void main(String[] args)
www.javaeasy2all.com 86 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
Fan f1=new Fan();
Switch s1=f1.getSwitch();
s1.on();
System.out.println(f1.runningStatus());
s1.off();
System.out.println(f1.runningStatus());
}
}
Output
true
false

133. Program
package com.java;
abstract class C
{
void test1()
{
System.out.println("C");
}
abstract void test2();
}
class Manager
{
public static void main(String[] args)
{
C c2=new C()
{
protected void test2()
{
System.out.println("A-I");
}
};
c2.test1();
c2.test2();
}
}
Output
C
A-I

134. Program
package com.java;
interface D

www.javaeasy2all.com 87 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
void test1();
}
class Manager
{
public static void main(String[] args)
{
D d1=new D()
{
public void test1()
{
System.out.println("A-I");
}
};
d1.test1();
}
}
Output
A-I

135. Program
package com.java;
interface Foo
{
int bar();
}
public class Sprite
{
public static void main(String[] args)
{
public int fubar(Foo foo)
{

}
public void testFoo()
{
fubar(
new Foo()
{
public int bar()
{
return 1;
}
});

www.javaeasy2all.com 88 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
}
}
Output
Compile time error

136. Program
package com.java;
class A
{
public static void test()
{
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A()
{
public void test()
{
System.out.println("A-type");
}
};
}
}
Output
Compile time error

137. Program
package com.java;
abstract class A
{
public void test()
{
}
}
class Manager
{
public static void main(String[] args)
{
A a1=new A()
{
www.javaeasy2all.com 89 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

public void test()


{
System.out.println("A-type");
}
};
a1.test();
}
}
Output
A-type

138. Program
class A
{
void method()
{
System.out.println("mthod1");
}
}
public class Manager
{
Manager()
{
A a1=new A()
{
public void method()
{
System.out.println("ananymous-innerclass");
}
};
}
public static void main(String[] args)
{
Manager m1=new Manager();
}
}
Output
Compile time success

139. Program
class A
{
static int i;
void method()
{

www.javaeasy2all.com 90 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println("mthod1");
}
}
public class Manager
{
Manager()
{
A a1=new A()
{
static int i=23;
public void method()
{
System.out.println("ananymous-innerclass");
}
static void test()
{
System.out.println("static-method");
}
};
}
public static void main(String[] args)
{
Manager m1=new Manager();
}
}
Output
Compile time error

140. Program
class A
{
static int i;
void method()
{
System.out.println("mthod1");
}
}
public class Manager
{
Manager()
{
A a1=new A()
{
public void method()

www.javaeasy2all.com 91 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
System.out.println("ananymous-innerclass");
}
public void test()
{
System.out.println("static-method");
System.out.println(i);
}
};
a1.method();

}
public static void main(String[] args)
{
Manager m1=new Manager();
}
}
Output
Anonymous-innerclass

141. Program
package com.java;
class Job
{
String tittle;
double salary;
public Job(String tittle,double salary)
{
this.tittle=tittle;
this.salary=salary;
}
void print()
{
System.out.println(tittle +":"+ salary);
}
}
class Employee
{
private int index;
Job all[];
Employee(Job all[])
{
this.all=all;
}

www.javaeasy2all.com 92 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

Job nextJob()
{
return all[index++];
}
boolean hasNext()
{
return index<all.length;
}
}
class Manager
{
public static void main(String[] args)
{
Job j1=new Job("software",50000);
Job j2=new Job("hardware",10000);
Job all[]={j1,j2};
Employee e=new Employee(all);
print(e);
System.out.println("-------------");
print(e);
}
static void print(Employee e)
{
Job j=null;
while(e.hasNext())
{
j=e.nextJob();
j.print();
}
}
}
Output
software:50000.0
hardware:10000.0
-----------------------
142. Program
package com.java;
class Job
{
String tittle;
double salary;
public Job(String tittle,double salary)
{
this.tittle=tittle;

www.javaeasy2all.com 93 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

this.salary=salary;
}
void print()
{
System.out.println(tittle +":"+ salary);
}
}
class Employee
{
private int index;
Job all[];
Employee(Job all[])
{
this.all=all;
}
Job nextJob()
{
return all[index++];
}
boolean hasNext()
{
if(index!=all.length)
{
return true;

}
else
{
index=0;
return false;
}
}
}
class Manager
{
public static void main(String[] args)
{
Job j1=new Job("software",50000);
Job j2=new Job("hardware",10000);
Job all[]={j1,j2};
Employee e=new Employee(all);
print(e);
System.out.println("-------------");
print(e);
www.javaeasy2all.com 94 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
static void print(Employee e)
{
Job j=null;
while(e.hasNext())
{
j=e.nextJob();
j.print();
}
}
}
Output
software:50000.0
hardware:10000.0
-------------
software:50000.0
hardware:10000.0

143. Program
package com.java;
class Job1
{
String title;
double salary;
}
class Iterator
{
int index;
Job1 all[];
Iterator(Job1 all[])
{
this.all=all;

}
public boolean hasNext()
{
return index<all.length;
}
public Job1 next()
{
return all[index++];
}
}
class Employee1
{
www.javaeasy2all.com 95 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

String name;
Job1 all[];
Employee1(Job1 all[])
{
this.all=all;
}
public Iterator getIterator()
{
Iterator it=new Iterator(all);
return it;

}
}
public class Manager1
{
public static void main(String[] args)
{
Job1 all[]=new Job1[2];
Job1 j1=new Job1();
j1.title="software";
j1.salary=50000;
Job1 j2=new Job1();
j2.title="hardware";
j2.salary=30000;
all[0]=j1;
all[1]=j2;
Employee1 emp=new Employee1(all);
print(emp);
System.out.println("==================");
print(emp);
}
static void print(Employee1 emp)
{
Iterator it=emp.getIterator();
while(it.hasNext())
{
Job1 j1=it.next();
System.out.println(j1.title+":"+j1.salary);
}
}
}
Output
software:50000.0
hardware:30000.0

www.javaeasy2all.com 96 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

==================
software:50000.0
hardware:30000.0

144. Program
package com.java;
class Job11
{
String title;
double salary;
}
class Employee11
{
String name;
Job11 all[];
Employee11(Job11 all[])
{
this.all=all;
}
public Iterator1 getIterator()
{
Iterator1 it=new Iterator1(all);
return it;
}

class Iterator1
{
int index;
Job11 all[];
Iterator1(Job11 all[])
{
this.all=all;
}
public boolean hasNext()
{
return index<all.length;
}
public Job11 next()
{
return all[index++];
}
}
}
public class Manager2
{
www.javaeasy2all.com 97 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

public static void main(String[] args)


{
Job11 all[]=new Job11[2];
Job11 j1=new Job11();
j1.title="software";
j1.salary=40000;
Job11 j2=new Job11();
j2.title="hardware";
j2.salary=20000;
all[0]=j1;
all[1]=j2;
Employee11 emp=new Employee11(all);
print(emp);
System.out.println("==================");
print(emp);
}
static void print(Employee11 emp)
{
Employee11.Iterator1 it=emp.getIterator();
while(it.hasNext())
{
Job11 j1=it.next();
System.out.println(j1.title+":"+j1.salary);

}
}
}
Output
software:40000.0
hardware:20000.0
==================
software:40000.0
hardware:20000.0

145. Program
package com.java;
interface TestA
{
String toString();
}
public class Test
{
public static void main(String[] args)
{
System.out.println(new TestA()
www.javaeasy2all.com 98 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

{
public String toString()
{
return "test";
}
});
}
}
Output
Test

www.javaeasy2all.com 99 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

ENUMS

 enum is a keyword which is introuduced in JDK1.5


 In order to represent a group of fixed constants enums are used . Example:
. To represent Months we can use enums .
. To represent WeakDays we can use enums .
 Every enum constant should be a perfect Java valid identifer .
 Enums can be declared as their own seperate class,or as a class member,however they
must not be declared within a method.Here’s an example of declaring an enum inside
a class,

class Coffee
{
enum coffeesize{BIG, HUGE, OVERWHELMING}
coffeesize size;
}
public class CoffeeTest
{
public static void main(String[] args)
{
Coffee drink = new Coffee();
}
}
EXAMPLES

146. Program
public enum Months
{
JAN, FEB, MAR;
}
Output
compile time success

147. Program
public enum Months
{
JAN(31), FEB(28), MAR(31);
}
Output
compile time error

148. Program
enum Months
{
JAN(31), FEB(28), MAR(31);
Months(int i)
{
System.out.println(“hello”);

www.javaeasy2all.com 100 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
}
class Manager
{
public static void main(String arg[])
{
Months m1 = Months.JAN;
}
}
Output
hello
hello
hello

149. Program
enum Test
{
A, B(10), C("abc");
Test()
{
System.out.println("no arg");
}
Test(int i)
{
System.out.println("int arg");
}
Test(String s)
{
System.out.println("string arg");
}
}
class Manager2
{
public static void main(String[] args)
{
Test t1 = Test.A;
}
}
Output
no arg
int arg
string arg

150. Program
enum Months
{
JAN(31), FEB(28);
int i;
private Months(int i)
{

www.javaeasy2all.com 101 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

this.i = i;
}
}
class Manager2
{
public static void main(String[] args)
{
Months m = Months.FEB;
System.out.println(m);
System.out.println(m.i);
}
}

Output
FEB
28

151. Program
enum E
{
en1,en2;
static int i;
private E()
{
System.out.println(i);
}
}
Output
compile time error

152. Program
enum Month
{
JAN(31) ,FEB(28);
private Month(int i)
{
System.out.println(this+":"+i);
}
}
class Manager2
{
public static void main(String[] args)
{
Month m1 = Month.JAN;
}
}
Output
JAN:31
FEB:28

www.javaeasy2all.com 102 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

153. Program
package com.lara;
enum Days
{
MON, TUE, WEN, THU, FRI, SAT, SUN
}
public class Manager
{
public static void main(String[] args)
{
Days d1=Days.MON;
System.out.println(d1);
Days d2=Days.FRI;
System.out.println(d2);
Days d3=Days.SUN;
System.out.println(d3);
}
}
Output
MON
FRI
SUN

154. Program
package com.lara;
enum Month
{
JAN(31), FEB(28), MAR(31), APR(30), MAY(31), JUNE(30),
JULY(31);
int days;
Month(int days)
{
this.days=days;
}
}
public class Manager2
{
public static void main(String[] args)
{
Month m1=Month.MAR;
System.out.println(m1);
System.out.println(m1.days);
Month m2=Month.MAY;
System.out.println(m2);
System.out.println(m2.days);
}
}
Output
MAR
31

www.javaeasy2all.com 103 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

MAY
31

155. Program
package com.lara;
public class Ant
{
static class Bat
{
enum Cat
{
HR, MANAGER, PRINCIPAL, DOCTOR
}
}
public static void main(String[] args)
{
Ant.Bat.Cat abc=Ant.Bat.Cat.MANAGER;
System.out.println(abc);
}
}
Output
MANAGER

156. Program
package com.lara;
public enum Color
{
RED, GREEN, BLUE;
public static void main(String[] args)
{
Color c1=Color.GREEN;
System.out.println(c1);
}
}
Output
GREEN

157. Program
package com.lara;
class Nav
{
enum Direction
{
NORTH, SOUTH, EAST, WEST;
}
}
enum Color1
{
RED, GREEN, BLUE;
}

www.javaeasy2all.com 104 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

public class Manager3


{
public static void main(String[] args)
{
Nav.Direction NORTH=Nav.Direction.NORTH;
System.out.println(NORTH);
Nav.Direction s1=Nav.Direction.SOUTH;
System.out.println(s1);
Color1 c2= Color1.BLUE;
System.out.println(c2);
}
}
Output
NORTH
SOUTH
BLUE

158. Program
package com.lara;
public class Ant
{
class Bat
{
enum Cat
{
HR, MANAGER, PRINCIPAL, DOCTOR
}
}
public static void main(String[] args)
{
Ant.Bat.Cat abt=Ant.Bat.Cat.MANAGER;
System.out.println(abt);
}
}
Output
Compile time error

159. Program
package com.lara;
public class Ant
{
static class Bat
{
final enum Cat
{
HR, MANAGER, PRINCIPAL, DOCTOR
}
}
public static void main(String[] args)
{

www.javaeasy2all.com 105 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Ant.Bat.Cat abt=Ant.Bat.Cat.MANAGER;
System.out.println(abt);
}
}
Output
Compile time error

160. Program
package com.lara;
public class Manager
{
enum Month
{
JAN(31), FEB(28), MAR(31), APRI(30), MAY(31),
JUNE(30), JULY(31);
int days;
Month(int days)
{
this.days=days;
}
int getDays()
{
return days;
}
}
public static void main(String[] args)
{
Month m1=Month.MAR;
System.out.println(m1);
System.out.println(m1.getDays());
Month m2=Month.MAY;
System.out.println(m2+":"+m2.getDays());

}
}
Output
MAR
31
MAY:3

161. Program
package com.rst;
public class Person1
{
enum Color3
{
RED(0xff0000),GREEN(0x00ff00),BLUE(0x000ff);
private int rgb;
Color3(int rgb)
{

www.javaeasy2all.com 106 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

this.rgb=rgb;
}
public int getColor()
{
return rgb;
}
}
public static void main(String[] args)
{
Person1.Color3 pc=Person1.Color3.GREEN;
System.out.println(pc);
Color3 c1=Color3.BLUE;
System.out.println(c1);
System.out.println(Color3.RED);
}
}
Output
GREEN
BLUE
RED
162. Program
public class Manager
{
abstract enum Alpha{A,B,C};
public static void main(String[] args)
{
Alpha a1 = Alpha.A;
System.out.println(a1);
}
}
Output
CTE

163. Program
enum G
{
G()
{
System.out.println(1);
}
}
class Manager1
{
public static void main(String[] args)
{
G a1 = G.A;
System.out.println(a1);
}

www.javaeasy2all.com 107 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Output
1
A
164. Program
enum E
{
e1,e2(2);
private E()
{
System.out.println("no arg constructor");
}
E(int i)
{
System.out.println("one arg constructor ");
}
}
public class Manager2
{
public static void main(String[] args)
{
E e = E.e1;
System.out.println(e);
E e5 = E.e2;
System.out.println(e5);
System.out.println("done");
}
}

Output
no arg constructor
one arg constructor
e1
e2
done
165. Program
enum Test
{
a(1),b(2),c(3);
int i;
private Test(int i)
{
this.i = i;
}
static int j = 20;
}
public class Manager2
{
public static void main(String[] args)
{

www.javaeasy2all.com 108 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Test t1 = Test.a;
Test t2 = Test.b;
System.out.println(t1.i);
System.out.println(t1.j);
System.out.println(t2.j);
}
}

Output
1
20
20
166. Program
enum Test
{
static int j = 20;
a(1),b(2),c(3);
int i;
private Test(int i)
{
this.i = i;
}

}
public class Manager2
{
public static void main(String[] args)
{
Test t1 = Test.a;
Test t2 = Test.b;
System.out.println(t1.i);
System.out.println(t1.j);
System.out.println(t2.j);
}
}

Output
compile time error

167. Program
package com.lara;
enum Days
{
MON, TUE, WEN, THU, FRI, SAT,SUN
}
public class Manager
{
public static void main(String[] args)
{

www.javaeasy2all.com 109 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Days d1 = Days.MON;
System.out.println(d1);
}

}
Output
MON

168. Program
package com.rst;
enum Months
{
JAN, FEB, MAR, APR, MAY, JUN
}

public class Manager


{
public static void main(String[] args)
{
Months m1 = Months.JUN;
System.out.println(m1);
}

Output
JUN

169. Program
package com.lara;
enum Months
{
JAN, FEB, MAR, APR, MAY, JUN
}
enum Days
{
MON, TUE, WEN, THU, FRI, SAT
}

public class Manager


{
public static void main(String[] args)
{
Months m1 = Months.JUN;
System.out.println(m1);
Days d1 = Days.SAT;
System.out.println(d1);
}

www.javaeasy2all.com 110 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Output
JUN
SAT

170. Program
class A
{
enum Test
{
A, B, C
}
}
public class Manager2
{
public static void main(String[] args)
{
A.Test t1 = A.Test.A;
System.out.println(t1);
}
}

Output
A

171. Program
enum Direction
{
EAST, WEST, NORTH
}
public class Manager2
{
public static void main(String[] args)
{
Direction d = Direction.SOUTH;
System.out.println(d);

Output
compile time error

172. Program
enum Test
{
A, B, C, D
}
public class Manager2
{
public static void main(String[] args)

www.javaeasy2all.com 111 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
Test t = Test.A;
System.out.println(t);
System.out.println(t.ordinal());
}
}
Output
A
0

173. Program
enum Test
{
A, B, C, D
}
public class Manager2
{
public static void main(String[] args)
{
Test all[] = Test.values();
for(int i=0;i<all.length;i++)
{
System.out.println(all[i]);
}
}

Output
A
B
C
D

174. Program
enum Test
{
A, B, C, D
}
public class Manager2
{
public static void main(String[] args)
{
Test t = Test.valueOf("A");
if(t == null)
{
System.out.println("NO A IN TEST");
}
else
{

www.javaeasy2all.com 112 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(t);
}
}

}
Output
A

175. Program
enum C
{
en1,en2,en3;
static int i;
C()
{
System.out.println(i);
}
}

Output
compile time error

176. Program
enum Manager
{
a, b(10), c(10.0);
Manager()
{

}
Manager(int i)
{

}
Manager(Double d)
{
}
}
Output
compile time success

177. Program
enum Manager
{
a, b(10), c(10.0);
Manager()
{

}
Manager(int i)

www.javaeasy2all.com 113 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
Manager()
{

}
}

Output
compile time error

178. Program
enum A
{
a, b;
A()
{
System.out.println(1);
}
A(int i)
{
System.out.println(2);
}
}
public class Manager
{
public static void main(String[] args)
{
A a1 = A.a;
System.out.println(a1);
System.out.println("success");
}
}

Output
1
1
a
success

179. Program
package com.lara;
public class Switchenum
{
public static void main(String[] args)
{
int x=1;
switch(x)
{

www.javaeasy2all.com 114 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

case 1:System.out.println("x is one");


case 2:System.out.println("x is two");
case 3:System.out.println("x is three");
}
System.out.println("out of switch");
}

}
Output
x is one
x is two
x is three
out of switch

180. Program
package com.lara;
public class Switchenum
{
public static void main(String[] args)
{
int x=1;
switch(x)
{
case 1:System.out.println("x is one");
break;
case 2:System.out.println("x is two");
break;
case 3:System.out.println("x is three");
}
System.out.println("out of switch");
}

Output
x is one
out of switch

181. Program
package com.lara;
public class Switchenum
{
enum Color
{
RED,GREEN,BLUE
}
public static void main(String[] args)
{
Color c = Color.GREEN;
switch(c)

www.javaeasy2all.com 115 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
case RED:System.out.println("RED");
case GREEN:System.out.println("GREEN");
case BLUE:System.out.println("BLUE");
}
}

Output
GREEN
BLUE

182. Program
package com.lara;
public class Switchenum
{
enum color
{
RED,GREEN,BLUE
}
public static void main(String[] args)
{
color c = color.RED;
switch(c)
{
case RED:System.out.println("RED");
case GREEN:System.out.println("GREEN");
case BLUE:System.out.println("BLUE");
}

Output
RED
GREEN
BLUE

183. Program
package com.lara;
public class Switchenum
{
enum color
{
RED,GREEN,BLUE
}
public static void main(String[] args)

www.javaeasy2all.com 116 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
color c = color.RED;
switch(c)
{
case RED:System.out.println("RED");
break;
case GREEN:System.out.println("GREEN");
break;
case BLUE:System.out.println("BLUE");
}
}
}
Output
RED

184. Program
public class Manager
{
public static void main(String[] args)
{
enum Months
{
JAN, FEB, MAR
}
}

}
Output
compile time error

185. Program
class Nav
{
public enum Direction
{
NORTH, SOUTH, EAST, WEST
}
class Sprite
{
//insert code here
}
}

Which code,inserted in Sprite class,that allows the Sprite class to compile?


A. Direction d = NORTH;
B. Nav.Direction d = NORTH;
C.Direction d =Direction.NORTH;
D.Nav.Direction d = Nav.Direction.NORTH;
Answer:
D

www.javaeasy2all.com 117 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

186. Program
package sun.scjp;
public enum color
{
RED, GREEN, BLUE
}
package sun.beta;
//insert code here
public class Beta
{
color g = GREEN;
public static void main(String[] args)
{
System.out.println(GREEN);
}
}

The class Beta and enum color are in different packages.


Which Two code fragements,inserted individually in package sun.beta
declaration,Will allow this code to compile?(Choose two.)
A. import sun.scjp.color.*;
B. import static sun.scjp.Color.*;
C. import sun.scjp.Color;import static sun.scjp.Color.*;
D. import sun.scjp.*;import static sun.scjp.Color.*;
E. import sun.scjp.Color;import static sun.scjp.Color.GREEN;
Answer:
CE

187. Program
public class Ball
{
public enum Color
{
RED,GREEN,BLUE;
}
public void foo()
{
//insert code here
{
System.out.println(c);
}

}
}

Which code inserted in public void foo,that causes the foo method to print
RED,GREEN,BLUE?
A. for(Color c : Color.values())
B. for(Color c = RED;c<=BLUE;c++)
C. for(Color c;c.hasNext();c.next())

www.javaeasy2all.com 118 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

D. for(Color c = Color[0];c<=Color[2];c++)
E. for(Color c = Color.RED;c<=Color.BLUE;c++)
Answer:
A

188. Program
public class Fabric
{
public enum color
{
RED(0xff0000),GREEN(0x00ff00),BLUE(0x0000ff);
private final int rgb;
Color(int rgb)
{
this.rgb;
}
public int getRGB()
{
return rgb;
}
};
public static void main(String[] args)
{
//insert code here
}
}
Which two code fragements,inserted independently in main method,that
allows the Fabric class tocompile?(Choose two.)

A. Color skyColor = BLUE;


B. Color treeColor = Color.GREEN;
C. Color purple = new Color(0xff00ff);
D. if(RED.getRGB()<BLUE.getRGB()){}
E. Color purple = Color.BLUE+Color.RED;
F. if(Color.RED.ordinal()<Color.BLUE.ordinal()){}
Answer:
BF

189. Program
public enum Title
{
MR("Mr."), MRS("Mrs."), MS("Ms.");
private final String title;
private Title(String t)
{
title = t;
}
public String format(String last,String first)
{
return title + "" +first + "" + last;

www.javaeasy2all.com 119 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
}
public static void main(String[]args)
{
System.out.println(Title.MR.format("Doe","John"));
}

What is the result?


A. Mr.John Doe
B. An exception is thrown at runtime.
C. Compilation fails because of an error in
line,MR("Mr."),MRS("Mrs."),MS("Ms.");
D. Compilation fails because of an error in line,public String format(String
last,String first);
E. Compilation fails because of an error in
line,System.out.println(Title.MR.format("Doe","John"));
Answer
A

190. Program
public class Test
{
public enum Dogs
{
collie,harrier,shepherd
};
public static void main(String[]args)
{
Dogs myDog = Dogs.shepherd;
switch(myDog)
{
case collie:System.out.print("collie");
case default:System.out.print("retriever");
case harrier:System.out.print("harrier");
}
}
}
What is the result?
A. harrier.
B. shepherd.
C. retriever.
D. compilation fails.
E. retriever harrier.
F. An exception is thrown at runtime.
Answer:
D

191. Program
public class Test
{

www.javaeasy2all.com 120 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

public enum Dogs


{
collie,harrier
};
public static void main(String[]args)
{
Dogs myDog = Dogs.collie;
switch(myDog)
{
case collie:System.out.print("collie");
case harrier:System.out.print("harrier");
}
}
}
What is the result?
A. collie.
B. harrier.
C. compilation fails.
D.collie harrier.
E.An exception is thrown at runtime
Answer:
D

192. Program
enum Coffeesize
{
BIG(8), HUGE(10), OVERWHELMING(16);
int ounces;
Coffeesize(int ounces)
{
this.ounces=ounces;
}
private int getounces()
{
return ounces;
}
}
public class Coffee
{
Coffeesize size;
public static void main(String[] args)
{
Coffee drink1 = new Coffee();
drink1.size=Coffeesize.BIG;
Coffee drink2 = new Coffee();
drink2.size=Coffeesize.HUGE;
System.out.println(drink1.size.getounces());
System.out.println(drink2.size.getounces());
}

www.javaeasy2all.com 121 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
Output
compile time error

193. Program
enum Coffeesize
{
BIG(8), HUGE(10), OVERWHELMING(16);
int ounces;
Coffeesize(int ounces)
{
this.ounces=ounces;
}
int getounces()
{
return ounces;
}
}
public class Coffee
{
Coffeesize size;
public static void main(String[] args)
{
Coffee drink1 = new Coffee();
drink1.size=Coffeesize.BIG;
Coffee drink2 = new Coffee();
drink2.size=Coffeesize.HUGE;
System.out.println(drink1.size.getounces());
System.out.println(drink2.size.getounces());
}

Output
8
10

194. Program
enum Animals
{
DOG("woof"),CAT("meow"),FISH("burble");
String sound;
Animals(String s)
{
sound = s;
}
}
public class TestEnum
{
static Animals a;

www.javaeasy2all.com 122 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

public static void main(String[] args)


{
System.out.println(a.DOG.sound+""+a.FISH.sound);

}
What is the result?
A. woofburble.
B. compilation fails.
C. compilation fails due to an error in line 3,string sound.
D. compilation fails due to an error in line 4,Animals(string s).
Answer:
A

195. Program
public enum A
{
A
}
class E2
{
enum B
{
B
}
void C()
{
enum D
{
D
}
}
}
Which statements are ture?(Choose two)
A. The code compiles.
B. If only line1(enum A{A}) is removed the code compiles.
C. If only line3(enum B{B}) is removed the code compiles.
D. If only line5(enum D{D}) is removed the code compiles.
E. If line1(enum A{A}),line3(enum B{B}),line5(enum D{D}) are removed
the code compiles.
Answer:
D
E

196. Program
public class Exam
{
enum Result
{

www.javaeasy2all.com 123 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

public static void main(String[] args)


{

}
}
}
Output
compilation time error

197. Program
enum Result
{
public static void main(String[] args)
{

}
public class Exam
{

}
}
Output
compile time error

198. Program
Which are valid enum identifers?(Choose two)
A. abc.
B. #divide
C. %xyz
D. *dim
E. abc1
Answer:
A
E

199. Program
enum Example
{
ONE, TWO, THREE
}
Which is true?
A. The expression(ONE==TWO)and ONE.equals(ONE)are both guaranteed
to be true.
B. The expression(ONE<TWO)is guaranteed to be true and
ONE.compareTo(TWO)is guaranteed to be less than one.
C. The Example values cannot be used in a raw java.util.HashMap;insted,the
programmer must use a java.util.EnumMap.
D. The Example values cannot be used in java.util.SortedSet,but the set will
not be sorted because enumerated types do not implement
java.lang.comparable.

www.javaeasy2all.com 124 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Answer:
A

200. Program
public class Tester
{
abstract enum En5
{
a, b, c
}
}
Output
compile time error

201. Program
public class Tester
{
final enum En5
{
a, b, c
}
}
Output
compile time error

202. Program
enum En2
{
a, b, c, d;
}
public class Manager
{
public static void main(String[] args)
{
En2 e1 = En2.valueOf("c");
System.out.println(e1);
En2 e2 = En2.valueOf("p");
System.out.println(e2);
}
}
Output
C
Run time exception

203. Program
enum En2
{
a, b, c, d;
}
public class Manager

www.javaeasy2all.com 125 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
public static void main(String[] args)
{

En2 e1 = En2.valueOf("c");
System.out.println(e1);
En2 e2 = En2.valueOf("d");
System.out.println(e2);
}
}

Output
C
D

www.javaeasy2all.com 126 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

EXCEPTION HANDLING

204. Program
public class A
{
public static void main(String[] args)
{
System.out.println(1);
String s1 = args[0];
System.out.println(s1);
}
}
Output
case1: classes>java A
1
Array Index Out OfBoundsException
case2: classes>java A abc
1
abc

205. Program
public class A
{
public static void main(String[] args)
{
System.out.println(1);
String s1 = args[0];
System.out.println(2);
int i = Integer.parseInt(s1);
System.out.println(i);
}
}
Output
case1: classes>java A abc
1
2
NumberFormatException
case2: classes>java A 9
1
2
9

206. Program
public class A
{

www.javaeasy2all.com 127 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

public static void main(String[] args)


{
System.out.println(1);
String s1 = args[0];
System.out.println(2);
int i = Integer.parseInt(s1);
System.out.println(3);
int k = i/(i-9);
System.out.println(4);
}
}
Output
case1: classes>java A 9
1
2
3
ArithmeticException
case2: classes>java A 18
1
2
3
4

207. Program
public class A
{
public static void main(String[] args)
{
try
{
System.out.println(1);
String s1 = args[0];
System.out.println(2);
int i = Integer.parseInt(s1);
System.out.println(3);
int k = i/(i-9);
System.out.println(4);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("enter CmdLineArgs");
}
System.out.println(5);
}

www.javaeasy2all.com 128 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
Output
case1: classes>java A
1
Enter CmdLineArgs
5
case2: classes>java A abc
1
2
NFException
case3: classes>java A 9
1
2
3
ArithmeticException
case4: classes>java A 18
1
2
3
4
5

208. Program
public class A
{
public static void main(String[] args)
{
try
{
System.out.println(1);
String s1 = args[0];
System.out.println(2);
int i = Integer.parseInt(s1);
System.out.println(3);
int k = i/(i-9);
System.out.println(4);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("enter CmdLineArgs");
}
catch (NumberFormatException e)
{
System.out.println("enter integer number");

www.javaeasy2all.com 129 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
catch (ArithmeticException e)
{
System.out.println("enter except 9");
}
System.out.println(5);
}
}
Output
case1: classes>java A
1
enter CmdLineArgs
5
case2: classes>java A abc
1
2
enter integer Number
5
case3: classes>java A 9
1
2
3
enter except 9
5
case4: classes>java A 18
1
2
3
4
5

209. Program
public class B
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
}
}
}

www.javaeasy2all.com 130 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Output
compile time error (only try block is not possible)

210. Program
public class B
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
catch (NumberFormatException e)
{
System.out.println(e);
}
}
}
Output
case1: classes>java B
java.lang.ArrayIndexOutOfBoundsException: 0
case2: classes>java B abc
java.lang.NumberFormatException

211. Program
public class B
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
}
catch (ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
}
Output
classes>java B

www.javaeasy2all.com 131 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

java.lang.ArrayIndexOutOfBoundsException: 0
at B.main(B.java:6)

212. Program
public class B
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
}
System.out.println(“catch”);
catch (ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
}
Output
Compile Time Error

213. Program
public class B
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
}
catch (ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
int k;
catch (NumberFormatException e)
{
e.printStackTrace();
}
}
}
Output
Compile Time Error

www.javaeasy2all.com 132 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

214. Program
public class B
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
}
catch (int j)
{
System.out.println(j);
}
catch (NumberFormatException e)
{
System.out.println(e);
}
}
}
Output
Compile Time Error

215. Program
public class B
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
}
catch (ArrayIndexOutOfBoundsException e,NumberFormatException
ex)
{
System.out.println("done");
}
}
}
Output
Compile Time Error

216. Program
public class B
{
public static void main(String[] args)

www.javaeasy2all.com 133 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
try
{
int i = Integer.parseInt(args[0]);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(i);
}
}
}
Output
Compile Time Error

217. Program
public class B
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
}
catch (Exception ex)
{
System.out.println(ex);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
}
Output
Compile Time Error

218. Program
public class B
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
System.out.println(i);

www.javaeasy2all.com 134 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
catch (ArrayIndexOutOfBoundsException e1)
{
System.out.println(e1);
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}
Output
Compile time success
classes>java B 10
10

219. Program
public class B
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
System.out.println(i);
}
finally
{
System.out.println("finally block");
}

}
}
Output
case1: classes>java B
finally block
AIOBException
case2: classes>java B abc
finally block
NFException
case3: classes>java B 3
3
finally block

www.javaeasy2all.com 135 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

220. Program
public class B
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
System.out.println(i);
}
int k;
finally
{
System.out.println("finally block");
}
}
}
Output
Compile Time Error

221. Program
public class B
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
System.out.println(i);
}
finally
{
System.out.println("finally block");
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
}
Output
Compile Time Error

222. Program
public class B

www.javaeasy2all.com 136 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
System.out.println(i);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
int m = 20;
finally
{
System.out.println("finally block");
}
}
}
Output
Compile Time Error

223. Program
public class B
{
public static void main(String[] args)
{
try
{
String s1 = args[0];
int i = Integer.parseInt(s1);
System.out.println(1);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(2);
}
finally
{
System.out.println("finally block");
}
}
}

www.javaeasy2all.com 137 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Output
case1: classes>java B
2
Finally block
case2: classes>java B abc
finally block
NFException
case3: classes>java B 34
1
finally block

224. Program
public class Manager
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
int k = i/(i-9);
System.out.println(“try end”);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
catch (NumberFormatException e)
{
System.out.println(e);
}
finally
{
System.out.println("finally");
}
System.out.println("end");
}
}
Output
case1: classes>java Manager
AIOBException
finally
end
case2: classes>java Manager abc
NFException

www.javaeasy2all.com 138 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

finally
end
case3: classes>java Manager 9
finally
ArithmeticException
case4: classes>java Manager 18
try end
finally
end

225. Program
public class Manager1
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
try
{
int l = i/(i-9);
}
catch (ArithmeticException e)
{
System.out.println("inner catch");
}
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("outer catch");
}
}
}
Output
case1: classes>java Manager1
outer catch
case2: classes>java Manager1 abc
NFException
case3: classes>java Manager1 9
inner catch

226. Program
public class Manager
{
public static void main(String[] args)

www.javaeasy2all.com 139 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
try
{
int i = Integer.parseInt(args[0]);
try
{
int l = i/(i-9);
}
catch (NumberFormatException e)
{
System.out.println("inner catch");
}
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("outer catch");
}
catch (ArithmeticException e)
{
System.out.println("outer catch one");
}
}
}
Output
case1: classes>java Manager
outer catch
case2: classes>java Manager abc
NFException
case3: classes>java Manager 9
outer catch one

227. Program
public class Manager
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
try
{
int l = i/(i-9);
}
catch (NumberFormatException e)

www.javaeasy2all.com 140 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
System.out.println("inner catch");
}
finally
{
System.out.println("inner final");
}
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("outer catch");
}
catch (ArithmeticException e)
{
System.out.println("outer catch one");
}
}
}
Output
case1: classes>java Manager
outer catch
case2: classes>java Manager abc
NFException
case3: classes>java Manager 9
inner final
outer catch one

228. Program
public class Manager1
{
public static void main(String[] args)
{
int i = test(args[0]);
System.out.println(i);
}
static int test(String s1)
{
try
{
int i = Integer.parseInt(s1);
return i;
}
catch (NumberFormatException e)
{

www.javaeasy2all.com 141 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

return 0;
}
finally
{
return 1000;
}
}
}
Output
case1: classes>java Manager1 abc
1000
case2: classes>java Manager 5
1000

229. Program
public class Manager1
{
public static void main(String[] args)
{
int i = test(args[0]);
System.out.println(i);
}
static int test(String s1)
{
try
{
return 1;
}
catch (NumberFormatException e)
{

}
}
}
Output
compile time error

230. Program
public class Manager1
{
public static void main(String[] args)
{
int i = test(args[0]);
System.out.println(i);
}

www.javaeasy2all.com 142 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

static int test(String s1)


{
try
{
return 1;
}
catch (NumberFormatException e)
{
return 2;
}
}
}
Output
Case1 : java Manager1 1
1
231. Program
public class Compile
{
static int test(String s1)
{
try
{
return 1;
}
catch (NumberFormatException e)
{
return 2;
}
return 3;
}
}
Output
CTE

232. Program
public class Compile
{
static int test(String s1)
{
try
{
return 1;
}
catch (NumberFormatException e)

www.javaeasy2all.com 143 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
return 3;
}
}
Output
CTS

233. Program
public class Compile
{
static int test(String s1)
{
try
{}
catch (NumberFormatException e)
{}
return 3;
}
}
Output
CTS

234. Program
public class Compile
{
static int test(String s1)
{
try
{
}
catch (NumberFormatException e)
{
return 6;
}
catch (ArithmeticException e) {}
return 3;
}
}
Output
CTS

235. Program
public class Compile

www.javaeasy2all.com 144 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
static int test(String s1)
{
try
{
return 3;
}
catch(NumberFormatException e)
{
return 6;
}
catch(ArithmeticException e)
{
return 4;
}
return 3;
}
}
Output
CTE

236. Program
public class Compile
{
static int test(String s1)
{
try
{
return 3;
}
catch (NumberFormatException e) {}
catch (ArithmeticException e)
{
return 4;
}
}
}
Output
CTE

237. Program
public class Compile
{
static int test(String s1)
{

www.javaeasy2all.com 145 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

try
{}
catch (NumberFormatException e) {}
catch (ArithmeticException e) {}
finally
{
return 6;
}
}

}
Output
CTS

238. Program
public class Compile
{
static int test(String s1)
{
try
{
return 3;
}
catch (NumberFormatException e) {}
catch (ArithmeticException e) {}
finally
{
return 6;
}
return 7;
}

}
Output
CTE

239. Program
public class A
{
static int test(String s1)
{
try
{
return 7;
}

www.javaeasy2all.com 146 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

catch (NumberFormatException e)
{
return 4;
}
catch (ArithmeticException e) {}
finally
{
return 6;
}
}

}
Output
CTS

240. Program
public class B
{
public static void main(String[] args)
{
for(int x = 1;x<args.length;x++)
{
System.out.print(args[x]+"");
}
}
}
Output
Case1: java B
No output
Case1: java B 1 2 3 4
23 4

241. Program
public class B
{
public void main(String[] args)
{
System.out.println("hello");
}
}
Output
NoSuchMethodError : main

242. Program
public class B

www.javaeasy2all.com 147 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
int i;
public static void main(String[] args)
{
B b1 = null;
try
{
System.out.println(b1.i);
}
catch (NullPointerException e)
{
System.out.println("a");
}
catch (RuntimeException e)
{
System.out.println("b");
}
finally
{
System.out.println("c");
}
}
}
Output
a
c

243. Program
public class C
{
public static void main(String[] args)
{
try
{
args = null;
args[0] = "test";
System.out.println(args[0]);
}
catch(Exception e)
{
System.out.println(1);
}
catch (NullPointerException e)
{

www.javaeasy2all.com 148 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(2);
}
}
}
Output
CTE

244. Program
public class D
{
public static void main(String[] args)
{
try
{
test();
}
catch (Exception e)
{
System.out.println("Exception");
}
}
static void test()
{
try
{
String x = null;
System.out.println(x.toString());
}
finally
{
System.out.println("finally");
}
}
}
Output
finally
Exception

245. Program
public class Final
{
public static void main(String[] args)
{
try
{

www.javaeasy2all.com 149 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

int i = Integer.parseInt(args[0]);
}
catch (NumberFormatException e)
{
}
finally
{
System.out.println("finally");
}
finally
{
System.out.println("finally2");
}

}
}
Output
CTE

246. Program
public class Final
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt(args[0]);
}
catch (NumberFormatException e)
{
}
finally
{
return;
}
System.out.println("done");
}
}
Output
CTE

247. Program
public class E
{
static int a[];

www.javaeasy2all.com 150 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

static
{
a[0] = 2;
}
public static void main(String[] args) {}
}
Output
Java.lang.ExceptionInInitializerError

248. Program
public class E
{
Integer i;
int x;
public E(int y)
{
x = i + y;
System.out.println(x);
}
public static void main(String[] args)
{
new E(new Integer(4));
}
}
Output
Java.lang.NullPointerException

249. Program
public class F
{
public static void main(String[] args)
{
F f1 = new F();
f1.count(3);
}
void count(int i)
{
count(++i);
}
}
Output
java.lang.StackOverflowError

www.javaeasy2all.com 151 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

250. Program
public class Manager
{
public static void main(String[] args)
{
int i = test(args[0]);
System.out.println(i);
}
static int test(String s1)
{
try
{
int i = Integer.parseInt(s1);
return i;
}
catch (NumberFormatException e)
{
return 0;
}
finally
{
return 500;
}
}

}
Output
Case1: Java Manager abc
500
Case1: Java Manager 10
500

251. Program
class G
{
int test(String s1)
{
try
{
int i = Integer.parseInt(s1);
int k = i/(i-9);
return k;
}
catch (ArithmeticException e)

www.javaeasy2all.com 152 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
return 0;
}
catch (NumberFormatException e)
{
return 200;
}
finally
{
return 1000;
}
}

}
public class Manager1
{
public static void main(String[] args)
{
G g1 = new G();
int i = g1.test("abc");
System.out.println(i);
i = g1.test("9");
System.out.println(i);
i = g1.test("18");
System.out.println(i);
}
}
Output
1000
1000
1000

252. Program
public class Manager2
{
public static void main(String[] args)
{
int i = 0;
try
{
i = Integer.parseInt(args[0]);
i++;
}
catch (NumberFormatException e)

www.javaeasy2all.com 153 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
i+=10;
System.out.println(i);
}
finally
{
i+=30;
System.out.println(i);
}
System.out.println(i);
}
}
Output
Case1: java Manager2
30
AIOBException
Case2: java Manager2 abc
10
40
40
Case3: java Manager2 1
32
32

253. Program
public class G1
{
public static void main(String[] args)
{
int i = test(args[0]);
System.out.println(i);
}
static int test(String s1)
{
try
{
int i = Integer.parseInt(s1);
return i;
}
catch (NumberFormatException e)
{
return 0;
}
finally

www.javaeasy2all.com 154 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
System.out.println("finally");
}
}
}
Output
Case1:java G1 abc
finally
0
Case2:java G1 9
finally
9

254. Program
public class Manager7
{
public static void main(String[] args)
{
main(args);
}
}
Output
java.lang.StackOverflowError

255. Program
public class Manager7
{
public static void main(String[] args)
{
Manager7 all[] = new Manager7[1000637836];
for(int i = 0; i<all.length;i++)
{
all[i] = new Manager7();
}
}
}
Output
java.lang.OutOfMemoryError

256. Program
public class Manager7
{
public static void main(String[] args)
{
int i = 10/0;

www.javaeasy2all.com 155 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
}
Output
java.lang.ArithmeticException

257. Program
public class Manager7
{
void test()
{
System.out.println("test");
}
}
Output
java.lang.NoSuchMethodError : main

258. Program
public class Manager7
{
public static void main(String[] args)
{
int all[] = new int[5];
System.out.println(all[5]);
}
}
Output
Java.lang.ArrayIndexOutOfBoundsException

259. Program
public class Manager7
{
public static void main(String[] args)
{
String s1 = null;
System.out.println(s1.length());
}
}

Output
java.lang.NumberFormatException

260. Program
public class Manager7
{
public static void main(String[] args)
{

www.javaeasy2all.com 156 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Object o1 = new Object();


Manager7 m = (Manager7)o1;
}
}

Output
. Java.lang.ClassCastException

261. Program
public class H
{
public static void main(String[] args)
{
System.out.println(1);
test1();
System.out.println(2);
}
static void test1()
{
System.out.println(3);
test2();
System.out.println(4);
}
static void test2()
{
System.out.println(5);
int i = 10/0;
System.out.println(6);
}
}
Output
1
3
5
Java.lang.ArithmeticException

262. Program
public class H
{
public static void main(String[] args)
{
System.out.println(1);
test1();
System.out.println(2);
}

www.javaeasy2all.com 157 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

static void test1()


{
System.out.println(3);
test2();
System.out.println(4);
}
static void test2()
{
System.out.println(5);
try
{
int i = 10/0;
}
catch (ArithmeticException e)
{
e.printStackTrace();
}
System.out.println(6);
}
}
Output
1
3
5
6
4
2
java.lang.ArithmeticException: / by zero
at H.test2(H.java:20)
at H.test1(H.java:12)
at H.main(H.java:6)

263. Program
public class H
{
public static void main(String[] args)
{
System.out.println(1);
test1();
System.out.println(2);
}
static void test1()
{
System.out.println(3);

www.javaeasy2all.com 158 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

try
{
test2();
}
catch (ArithmeticException e)
{
System.out.println(e);
}
System.out.println(4);
}
static void test2()
{
System.out.println(5);
int i = 10/0;
System.out.println(6);
}
}
Output
1
3
5
java.lang.ArithmeticException: / by zero
4
2

264. Program
public class H
{
public static void main(String[] args)
{
System.out.println(1);
try
{
test1();
}
catch (ArithmeticException e)
{
e.printStackTrace();
}
System.out.println(2);
}
static void test1()
{
System.out.println(3);

www.javaeasy2all.com 159 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

test2();
System.out.println(4);
}
static void test2()
{
System.out.println(5);
int i = 10/0;
System.out.println(6);
}
}
Output
1
3
5
2
java.lang.ArithmeticException: / by zero
at H.test2(H.java:24)

265. Program
public class Final
{
public static void main(String[] args)
{
System.out.println(1);
try
{
System.out.println(2);
int i = Integer.parseInt(args[0]);
System.exit(0);
}
catch (NumberFormatException e)
{
System.out.println(3);
System.exit(0);
}
finally
{
System.out.println(4);
}
System.out.println(5);
}
}

Output

www.javaeasy2all.com 160 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Case1:java Final
1
2
4
AIOBException
Case2:java Final abc
1
2
3
Case3:java Final 15
1
2

266. Program
public class Z
{
public static void main(String[] args)
{
Class.forName("-");
}
}
Output
CTE

267. Program
public class Z
{
public static void main(String[] args)
{
try
{
Class.forName("-");
}
catch (ClassNotFoundException e)
{
System.out.println(e);
}
}
}
Output
CTS

268. Program
public class Z
{

www.javaeasy2all.com 161 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

public static void main(String[] args)


{
Z z1 = new Z();
Z z2 = (Z)z1.clone();
}
}
Output
CTE

269. Program
public class Z
{
public static void main(String[] args)
{
Z z1 = new Z();
try
{
Z z2 = (Z)z1.clone();
}
catch (CloneNotSupportedException e)
{
System.out.println(e);
}
System.out.println("handled");
}
}
Output
java.lang.CloneNotSupportedException: Z
handled

270. Program
import java.sql.SQLException;
public class Z
{
public static void main(String[] args)
{
try
{
java.sql.DriverManager.getConnection("-");
}
catch (SQLException e)
{
System.out.println(e);
}
System.out.println("handled");

www.javaeasy2all.com 162 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
}
Output
java.sql.SQLException: No suitable driver
handled

271. Program
public class Z
{
public static void main(String[] args)
{
Thread t1 = new Thread();
t1.sleep(190);
}
}
Output
CTE

272. Program
public class Z
{
public static void main(String[] args)
{
Thread t1 = new Thread();
try
{
t1.sleep(190);
}
catch (InterruptedException e)
{
System.out.println("handled");
}
System.out.println("done");
}
}
Output
done

273. Program
public class Manager4
{
public static void main(String[] args)
{
try{}
catch(ArithmeticException e)

www.javaeasy2all.com 163 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{}

}
}
Output
CTS

274. Program
public class Manager4
{
public static void main(String[] args)
{
try{}
catch(InterruptedException e)
{}

}
}
Output
CTE

275. Program
public class Manager4
{
public static void main(String[] args)
{
try
{
int i = 10/0;
}
catch(RuntimeException e)
{
}
catch (ArithmeticException e) {}
}
}
Output
CTE

276. Program
public class Manager4
{
public static void main(String[] args)
{
try

www.javaeasy2all.com 164 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
int i = 10/0;
}
catch (ArithmeticException e) {}
catch(RuntimeException e)
{
}
}
}
Output
CTS

277. Program
public class Manager4
{
public static void main(String[] args)
{
parse("invalid");
}
public static void parse(String str)
{
try
{
float f = Float.parseFloat(str);
}
catch(NumberFormatException e)
{
f = 0;
}
finally
{
System.out.println(f);
}
}
}
Output
CTE

278. Program
public class Checked
{
public static void main(String[] args)
{
System.out.println(1);
test1();

www.javaeasy2all.com 165 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(2);
}
static void test1()
{
System.out.println(3);
test2();
System.out.println(4);
}
static void test2()
{
System.out.println(5);
Class.forName("-");
System.out.println(6);
}
}
Output
CTE

279. Program
public class Checked
{
public static void main(String[] args)
{
System.out.println(1);
test1();
System.out.println(2);
}
static void test1()
{
System.out.println(3);
test2();
System.out.println(4);
}
static void test2()throws ClassNotFoundException
{
System.out.println(5);
Class.forName("-");
System.out.println(6);
}
}

Output
CTE

www.javaeasy2all.com 166 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

280. Program
public class Checked
{
public static void main(String[] args) throws ClassNotFoundException
{
System.out.println(1);
test1();
System.out.println(2);
}
static void test1()throws ClassNotFoundException
{
System.out.println(3);
test2();
System.out.println(4);
}
static void test2()throws ClassNotFoundException
{
System.out.println(5);
Class.forName("-");
System.out.println(6);
}
}
Output
1
3
5
CNFException(printStackTrace)

281. Program
public class Checked
{
public static void main(String[] args)
{
System.out.println(1);
test1();
System.out.println(2);
}
static void test1()
{
System.out.println(3);
test2();
System.out.println(4);
}
static void test2()

www.javaeasy2all.com 167 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
System.out.println(5);
try
{
Class.forName("----");
}
catch (ClassNotFoundException e)
{
System.out.println(e);
}
System.out.println(6);
}
}
Output
1
3
5
java.lang.ClassNotFoundException: ----
6
4
2

282. Program
public class Checked
{
public static void main(String[] args)
{
System.out.println(1);
test1();
System.out.println(2);
}
static void test1()
{
System.out.println(3);
try
{
test2();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
System.out.println(4);
}

www.javaeasy2all.com 168 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

static void test2()throws ClassNotFoundException


{
System.out.println(5);
Class.forName("----");
System.out.println(6);
}
}
Output
1
3
5
4
2
CNFException(printStackTrace)

283. Program
public class Checked
{
public static void main(String[] args)
{
System.out.println(1);
try
{
test1();
}
catch (ClassNotFoundException e)
{
System.out.println(e);
}
System.out.println(2);
}

static void test1()throws ClassNotFoundException


{
System.out.println(3);
test2();
System.out.println(4);
}
static void test2()throws ClassNotFoundException
{
System.out.println(5);
Class.forName("----");
System.out.println(6);
}

www.javaeasy2all.com 169 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
Output
1
3
5
java.lang.ClassNotFoundException: ----
2

284. Program
public class Checked
{
public static void main(String[] args)
{
System.out.println(1);
test1();
System.out.println(2);
}
static void test1()
{
System.out.println(3);
try
{
test2();
}
catch (ClassNotFoundException e)
{}
System.out.println(4);
}
static void test2()
{
System.out.println(5);
try
{
Class.forName("----");
}
catch (ClassNotFoundException e)
{
System.out.println(e);
}
System.out.println(6);
}
}
Output
CTE

www.javaeasy2all.com 170 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

285. Program
public class Checked
{
public static void main(String[] args)
{
System.out.println(1);
test1();
System.out.println(2)
}
static void test1()
{
System.out.println(3);
try
{
test2();
}
catch (ClassNotFoundException e)
{
System.out.println("class not found");
}
System.out.println(4);
}
static void test2()throws ClassNotFoundException
{
System.out.println(5);
try
{
Class.forName("----");
}
catch (ClassNotFoundException e)
{
System.out.println(e);
}
System.out.println(6);
}
}
Output
1
3
5
java.lang.ClassNotFoundException: ----
6
4
2

www.javaeasy2all.com 171 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

286. Program
class Y
{
void foo()throws NullPointerException
{
System.out.println("Y");
}
}
class X extends Y
{
void foo()
{
System.out.println("X");
}
}
public class Checked1
{
public static void main(String[] args)
{
Y y1 = new X();
y1.foo();
}
}
Output
X

287. Program
class Y
{
void foo()
{
System.out.println("Y");
}
}
class X extends Y
{
void foo()throws NumberFormatException
{
System.out.println("X");
}
}
public class Checked1
{
public static void main(String[] args)

www.javaeasy2all.com 172 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
Y y1 = new X();
y1.foo();
}
}
Output
X

288. Program
class Y
{
void foo()throws ClassNotFoundException
{
System.out.println("Y");
}
}
class X extends Y
{
void foo()
{
System.out.println("X");
}
}
public class Checked1
{
public static void main(String[] args) throws ClassNotFoundException
{
Y y1 = new X();
y1.foo();
}
}
Output
X

289. Program
class Y
{
void foo()
{
System.out.println("Y");
}
}
class X extends Y
{
void foo()throws ClassNotFoundException

www.javaeasy2all.com 173 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
System.out.println("X");
}
}
public class Checked1
{
public static void main(String[] args)
{
Y y1 = new X();
y1.foo();
}
}
Output
CTE

290. Program
class Y
{
void foo()throws Exception
{
System.out.println("Y");
}
}
class X extends Y
{
void foo()throws ClassNotFoundException
{
System.out.println("X");
}
}
public class Checked1
{
public static void main(String[] args)
{
Y y1 = new X();
try
{
y1.foo();
}
catch (Exception e)
{
System.out.println("Exception not rised");
}
}

www.javaeasy2all.com 174 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
Output
X

291. Program
class Y
{
void foo()throws ClassNotFoundException
{
System.out.println("Y");
}
}
class X extends Y
{
void foo()throws ClassNotFoundException
{
System.out.println("X");
}
}
public class Checked1
{
public static void main(String[] args)
{
Y y1 = new X();
try
{
y1.foo();
}
catch (ClassNotFoundException e)
{
System.out.println("Exception not rised");
}
}
}
Output
X

292. Program
import java.io.IOException;
public class Checked1
{
public static void main(String[] args)
{
try
{

www.javaeasy2all.com 175 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Class.forName("---");
}
catch (ClassNotFoundException e) {}
catch (IOException e) {}
}
}

Output
CTE

293. Program
public class Checked1
{
public static void main(String[] args)
{
try
{
Class.forName("---");
}
catch (ArithmeticException e) {}
catch (ClassNotFoundException e) {}
}
}
Output
CTS

294. Program
class L
{
public L() throws ClassNotFoundException
{
Class.forName("---");
}
}
class K extends L
{
public K(int i)
{
System.out.println("hello");
}
}
public class Checked1
{
public static void main(String[] args)
{

www.javaeasy2all.com 176 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

K k1 = new K(10);
}
}
Output
CTE

295. Program
class L
{
public L() throws ClassNotFoundException
{
Class.forName("---");
}
}
class K extends L
{
public K(int i)
{
try
{
super();
}
catch (ClassNotFoundException e) {}
System.out.println("hello");
}
}
public class Checked1
{
public static void main(String[] args)
{
K k1 = new K(10);
}
}

Output
CTE

296. Program
class L
{
public L() throws ClassNotFoundException
{
Class.forName("---");
}
}

www.javaeasy2all.com 177 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

class K extends L
{
public K(int i)throws ClassNotFoundException
{
System.out.println("hello");
}
}
public class Checked1
{
public static void main(String[] args) throws ClassNotFoundException
{
K k1 = new K(10);
}
}
Output
CTS

297. Program
import java.io.IOException;
class L
{
void doSomeThing()throws Exception
{

}
}
class K extends L
{
void doSomeThing()throws ClassNotFoundException,IOException
{

}
}
public class Checked1
{
public static void main(String[] args)
{}
}
Output
CTS

298. Program
import java.io.FileNotFoundException;
import java.io.IOException;
class L

www.javaeasy2all.com 178 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
void doSomeThing()throws FileNotFoundException
{
}
}
class K extends L
{
void doSomeThing()throws IOException
{
}
}
public class Checked1
{
public static void main(String[] args)
{
}
}
Output
CTE

299. Program
public class Checked2
{
static void main(String[] args)
{
try
{
}
catch (Throwable e) {}
catch(Exception e){}
catch (RuntimeException e) {}
catch (ArithmeticException e) {}
}
}
Output
CTE

300. Program
public class Checked2
{
public static void main(String[] args)
{
try
{
}

www.javaeasy2all.com 179 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

catch (ArithmeticException e) {}
catch (RuntimeException e) {}
catch(Exception e){}
catch (Throwable e) {}
}
}
Output
CTS

301. Program
import java.io.IOException;
class X1
{
public void process()
{
System.out.println("A,");
}
}
class Y1 extends X1
{
public void process()throws IOException
{
super.process();
System.out.println("B,");
throw new IOException();
}
}
public class Example
{
public static void main(String[] args)
{
try
{
new Y1().process();
}
catch (IOException e)
{
System.out.println("Exception");
}
}
}
Output
CTE

www.javaeasy2all.com 180 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

302. Program
public class Example
{
public static void main(String[] args)
{
String s1 = args[0];
int age = Integer.parseInt(s1);
if(age<0)
{
throw new ArithmeticException("age should be greater than
zero");
}
}
}
Output
Case1:java Example -9
Java.lang.ArithmeticException:age should be greater than zero

303. Program
class AgeNegativeException extends RuntimeException
{
AgeNegativeException()
{
}
AgeNegativeException(String s)
{
super(s);
}
}
public class Example
{
public static void main(String[] args)
{
String s1 = args[0];
int age = Integer.parseInt(s1);
if(age<0)
{
throw new AgeNegativeException("age should not be lesser
than zero");
}
}
}
Output
Case1:java Example -9

www.javaeasy2all.com 181 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

AgeNegativeException: age should not be lesser than zero

304. Program
class TestException extends Exception
{
}
class U
{
public String sayHello(String name)throws TestException
{
if(name == null)
{
throw new TestException();
}
return "Hello"+name;
}
}
public class Example
{
public static void main(String[] args)
{
U u1 = new U();
System.out.println(u1.sayHello("john"));
}
}
Output
CTE

305. Program
public class A
{
void test()throws ClassNotFoundException
{}
}
class B extends A
{
void test()throws NumberFormatException,NullPointerException
{}
}
Output
CTS

306. Program
public class A

www.javaeasy2all.com 182 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
void test()throws ClassNotFoundException
{
System.out.println("there is no atleast one stmt which is causing
CNFException");
}
}
class B
{
public static void main(String[] args)throws ClassNotFoundException
{
A a1 = new A();
a1.test();
}
}

Output
there is no atleast one stmt which is causing CNFException
307. Program
class TestException extends Exception
{
}
class U
{
public String sayHello(String name)throws TestException
{
if(name == null)
{
throw new TestException();
}
return "Hello"+name;
}
}
public class Example
{
public static void main(String[] args)throws TestException
{
U u1 = new U();
System.out.println(u1.sayHello("John"));
}
}
Output
HelloJohn

www.javaeasy2all.com 183 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

308. Program
class U
{
public void process()
{
System.out.println("A");
}
}
public class Example extends U
{
public void process()throws RuntimeException
{
super.process();
if(true)
throw new RuntimeException();
System.out.println("B");
}
public static void main(String[] args)
{
try
{
((U)new Example()).process();
}
catch (Exception e)
{
System.out.println("Exception");
}
}
}
Output
A
Exception

309. Program
class U
{
void process()throws Exception
{
throw new Exception();
}
}
public class Example extends U
{
void process()

www.javaeasy2all.com 184 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
System.out.println("B");
}
public static void main(String[] args)
{
U u1 = new Example();
u1.process();
}
}
Output
CTE

310. Program
class C
{
public C() throws ClassNotFoundException
{
Class.forName("---");
}
}
public class Manager
{
public static void main(String[] args)
{
try
{
C c1 = new C();
}
catch (ClassNotFoundException e)
{
System.out.println("there is no class like ---");
}
System.out.println("done");
}
}
Output
there is no class like ---
done

311. Program
class U
{
void process()throws Exception
{

www.javaeasy2all.com 185 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

throw new Exception();


}
}
public class Example extends U
{
void process()
{
System.out.println("B");
}
public static void main(String[] args)
{
U u1 = new Example();
try
{
u1.process();
}
catch (Exception e)
{
System.out.println("Exception rised");
}
}
}
Output
B

312. Program
class U
{
void process()throws Exception
{
throw new Exception();
}
}
public class Example extends U
{
void process()
{
System.out.println("Example");
}
public static void main(String[] args)
{
new Example().process();
}
}

www.javaeasy2all.com 186 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Output
Example

313. Program
public class Example
{
static void test()throws Error
{
if(true)
{
throw new AssertionError();
}
System.out.println("test");
}
public static void main(String[] args)
{
try
{
test();
}
catch (Exception e)
{
System.out.println("exception");
}
System.out.println("end");
}
}
Output
Java.lang.AssertionError

314. Program
public class Example
{
static void test()throws Error
{
if(true)
{
throw new AssertionError();
}
System.out.println("test");
}
public static void main(String[] args)
{
try
{

www.javaeasy2all.com 187 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

test();
}
catch (Throwable e)
{
System.out.println("Throwable");
}
System.out.println("end");
}
}
Output
Throwable
end

315. Program
public class Example
{
static void test()throws RuntimeException
{
try
{
System.out.println("test");
throw new RuntimeException();
}
catch (Exception e)
{
System.out.println("exception");
}
}
public static void main(String[] args)
{
try
{
test();
}
catch (RuntimeException e)
{
System.out.println("runtime");
}
System.out.println("end");
}
}
Output
test
exception

www.javaeasy2all.com 188 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

end

ASSERTIONS

316. Program
public class Manager
{
public static void main(String[] args)
{
System.out.println(1);
int i = Integer.parseInt(args[0]);
assert i < 0;
System.out.println(2);
}
}
Output
case1:java Manager 100
1
2
case2:java -ea Manager 100
1
AssertionError
317. Program
public class Manager7
{
static void test(int i)
{
System.out.println(1);
assert i > 0;
System.out.println(2);
}
public static void main(String[] args)
{
test(-5);
}
}
Output
case 1 : java Manager7
1
2
Case2 : java -ea Manager7
1
AssertionError
318. Program
class D
{
void test(int i)

www.javaeasy2all.com 189 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
assert i > 0:"in class D";
System.out.println("end");
}
}
public class Manager8
{
public static void main(String[] args)
{
D d1 = new D();
int i = -25;
d1.test(i);
assert i > 0:"Manager class";
System.out.println("main end");
}
}
Output
case1 : java Manager8
end
main end
case2 : java -ea Manager8
AssertionError: in class D
case3 : java -ea : Manager8 Manager8
end
AssertionError: Manager class
case4 : java -ea:D Manager8
AssertionError: in class D

319. Program
public class Assert
{
public static void main(String[] args)
{
assert args.length != 0:"enter CMDline arguments";
String s = args[0];
System.out.println(s);
}
}
Output
case1 : java Assert
AIOBException
Case2 : java -ea Assert
AssertionError : enter CMDLine arguments
Case3 : java -ea Assert Lara
Lara
Case4 : java Assert Lara.com
Lara.com

www.javaeasy2all.com 190 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

320. Program
public class Manager1
{
public static void main(String[] args)
{
System.out.println(1);
test(Integer.parseInt(args[0]));
System.out.println(2);
}
static void test(int i)
{
System.out.println(3);
assert i<0:"argument should be +ve";
System.out.println(4);
}
}
Output
case1 : java Manager1 10
1
3
4
2
Case2 : java Manager1 -10
1
3
4
2
Case3 : java -ea Manager1 -10
1
3
4
2
Case4 : java -ea Manager1 10
1
3
AssertionError : argument should be +ve
Case5 : java -da Manager1 10
1
3
4
2
321. Program
class A
{
static void test(int i)
{
System.out.println(1);
assert i>0:getMessage();

www.javaeasy2all.com 191 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(2);
}
static String getMessage()
{
return "value should be +ve";
}
}
public class Manager2
{
public static void main(String[] args)
{
System.out.println(3);
A.test(Integer.parseInt(args[0]));
System.out.println(4);
}
}
Output
case1 : java Manager2 10
3
1
2
4
Case2 : java Manager2 -10
3
1
2
4
Case3 : java -ea Manager2 10
3
1
2
4
Case4 : java -disableassertions Manager2 -10
3
1
2
4
Case5 : java -enableassertions Manager2 -10
3
1
AssertionError : value should be +ve

322. Program
class B
{
static void test(int i)
{
System.out.println(1);

www.javaeasy2all.com 192 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

assert i >10:i;
System.out.println(2);
}
}
public class Manager3
{
public static void main(String[] args)
{
System.out.println(3);
int k = Integer.parseInt(args[0]);
B.test(k);
System.out.println(4);
assert k > 15:true;
System.out.println(5);
}
}
Output
case1 : java -ea:B Manager3 12
3
1
2
4
5
Case2 : java -ea Manager3 12
3
1
2
4
AssertionError : true
Case3 : java -ea:B Manager3 8
3
1
AssertionError : 8
Case4 : java -ea -da:Manager3 Manager3 12
3
1
2
4
5
323. Program
public class Manager4
{
public static void main(String[] args)
{
int assert = 10;
System.out.println(assert);
}
}

www.javaeasy2all.com 193 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Output
case1: javac -source 1.3 Manager4.java
CTS with warnings
warning: from jdk1.4 onwards assert is a keyword ,assert should
not be used as a identifier
case2 : javac -source 1.4 Manager4.java
CTE
324. Program
public class Manager5
{
public static void main(String[] args)
{
assert args.length == 1:"length not equal to 1";
System.out.println("done");
}
}
Output
case1 : java Manager5
done
case2: java -ea Manager5
AssertionError : length not equal to 1
Case3 : java -ea Manager5 10
done
case4 : java -ea Manager5 25 23
AssertionError : length not equal to 1
Case5: java -da Manager5 10 40
done
325. Program
public class Puppy
{
public static void main(String[] args)
{
Puppy p = null;
someMethod(p);
}
static void someMethod(Object value)
{
System.out.println("method");
assert value != null:"NPException will arise";
System.out.println(value.getClass());
}
}
Output
case1: java Puppy
method
java.lang.NullPointerException
case2 : java -ea Puppy

www.javaeasy2all.com 194 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

method
AssertionError:NPException will arise

326. Program
public class AssertStuff
{
public static void main(String[] args)
{
int y = 7;
assert (5 > y):"stuff";
System.out.println("passed");
}
}
Output
case1: java AssertStuff
passed
case2 : java -enableassertion AssertStuff
Unrecognized option: -enableassertion
Could not create the Java virtual machine.
Case3: java -enableassertions AssertStuff
AssertionError : stuff

327. Program
public class Test
{
public static void main(String[] args)
{
boolean assert = true;
if(assert)
{
System.out.println(assert);
}
}
}
Output
case1 : javac -source 1.3 Test.java
CTS with warnings
warning: from jdk1.4 onwards assert is a keyword ,assert should
not be used as identifier

328. Program
public class Stuff
{
public static void main(String[] args)
{
int i = Integer.parseInt(args[0]);
assert i > 0:"enter +ve number";
switch(i)

www.javaeasy2all.com 195 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
case 1 :
System.out.println("case1");
break;
case 2 :
System.out.println("case2");
break;
case 3 :
System.out.println("case3");
break;
default:
assert false :"enter the number b/n 1 to 3";
System.out.println("done");
}
}
}
Outputs
case1 : java Stuff 5
done
case2 : java -ea Stuff 4
AssertionError : enter the number b/n 1 to 3
Case3 : java -ea Stuff -3
AssertionError : enter +ve number
Case4 : java -ea Stuff 3
case3
case5 : java -da Stuff 5
done

329. Program
public class Manager6
{
public static void main(String[] args)
{
int i = -10;
assert i > 0:message();
}
static void message()
{
return;
}
}
Output
CTE

330. Program
public class Manager6
{
public static void main(String[] args)

www.javaeasy2all.com 196 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

{
int i = -10;
assert i > 0:message();
System.out.println("done");
}
static String message()
{
return "value is -ve";
}
}
Output
case1 : java Manager6
done
case2 : java -ea Manager6
AssertionError : value is -ve

331. Program
package pack2;
public class B
{
public int j;
public static String getMessage()
{
return "i value is -ve";
}
}

package pack1;
import pack2.B;
public class A
{
public static void test(int i)
{
assert i > 0:B.getMessage();
System.out.println("done");
}
}

package pack3;
import pack1.A;
import pack2.B;
public class C
{
public static void main(String[] args)
{
A.test(-10);
B b1 = null;
assert b1 != null:"b1 is pointing to null";

www.javaeasy2all.com 197 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

System.out.println(b1.j);
}
}
Output
case1: java pack3. C
done
Null PoinerException
case2: java -ea pack3.C
AssertionError : i value is -ve
case3: java -ea:pack1... pack3.C
AssertionError : i value is -ve
case4: java -ea:pack3... pack3.C
done
AssertionError : b1 is pointing to null
case5: java -da:pack3... pack3.C
done
case6: java -da:pack3... -ea:pack1... pack3.C
AssertionError : i value is -ve
case7: java -ea:pack3... -da:pack1... pack3.C
done
AssertionError : b1 is pointing to null
case8: java -da:pack3... -da:pack1... pack3.C
done
Null PoinerException
332. Program
public class Manager
{
public static void main(String[] args)
{
String s = "assertion";
assert s.length()>10:"length is less than 10";
System.out.println(s);
}
}
Output
Case1: java Manager
assertion
Case2: java -ea Manager
AssertionError: length is less than 10
333. Program
public class Manager1
{
static
{
System.out.println("SIB");
int i = -20;
assert i > 0:"negative value";
}

www.javaeasy2all.com 198 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

public static void main(String[] args)


{
System.out.println("main");
}
}

Output
Case1: java Manager1
SIB
main
Case2: java -ea Manager1
SIB
AssertionError: negative value
Exception in thread “main”
334. Program
public class Manager3
{
public static void main(String[] args)
{
int i = args.length;
assert i > 0:Print();
String s = args[0];
System.out.println(s);
}
static String Print()
{
return "enter atleast one CMDLine argument";
}
}
Output
Case1: java Manager3
AIOBException
Case2: java -ea Manager3
AssertionError: enter atleast one CMDLine argument
Case3: java -ea Manager3 String
String
Case4: java -ea Manager3 S t r i n g
S
335. Program
public class Manager4
{
static Integer i ;
public static void main(String[] args)
{
System.out.println("start");
assert i != null: "i pointing to Null";
System.out.println(i.intValue());
}

www.javaeasy2all.com 199 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

}
Output
Case1: java Manager4
start
NPException
Case2: java -ea Manager4

AssertionError: i pointing to Null

336. Program
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Bank
{
public static void main(String[] args) throws IOException
{
System.out.println("enter the amount to withdraw");
BufferedReader b1 = new BufferedReader(new
InputStreamReader(System.in));
String s1 = b1.readLine();
int amount = Integer.parseInt(s1);
assert amount <= 5000:"does not have sufficient balance";
System.out.println("success");
}
}

Output
Case1: java Bank
enter the amount to withdraw
6000
success
Case2: java -ea Bank
enter the amount to withdraw
6000
AssertionError : does not have sufficient balance
Case3: java -ea Bank
enter the amount to withdraw
5000
Success
Case4: java -da Bank
enter the amount to withdraw
6000
success

www.javaeasy2all.com 200 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Classpath

dev1

src

com

lara
class

com Person.java

dev2
lara

src
Person.class

com

lara

class Manager.java

com

lara

Manager.class

www.javaeasy2all.com 201 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Person.java Manager.java

Person package com.lara;


import com.lara.Person;
package com.lara;
package com.lara;
public class Manager
public class
public class Person
Person
{
{{ public void static void main(String args[])
void test()
void test()
{
{{ Person p1 = new Person();
}
P1.test();
} System.out.println(“hello”); }
} }
}

IN WINDOW OPERATING SYSTEM

In the above example,we are developing 2 applications.one is dev1 and 2nd one is dev2 in the
same drive.We are creating 2 packages one is com and other one is lara inside com .and
developing a class “Person.java” inside lara package.

Compilation of Person class


dev1\src>javac –d ../class com/lara/*.java
Compilation successs.
Whenever we execute above command in command prompt,the javac command will go to
com.lara package and looks for all .java files then compiler will compile and generate as it
independent of another user defined classes.

If at all for compilation of any java file,there must be either class or interface declared in it
and then if we are using any 3rd party class in any of the class then it requires or looks for the
.class file of that class or that classname.java file in the current source stream.Person class file
is generated as it is independent class.

Compilation of Manager.java
dev2\src\javac – d ../class com/lara/*.java
Compile Time Error

www.javaeasy2all.com 202 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Whenever we execute the above command ,the javac command will go to com.lara package
in the dev2 application and looks for all java files and compiler tries to compile all
files.When it finds that there is a dependency of a class called Person in com.lara package for
compiling Manager class,it will be searching inside the com.lara package in the same
application development i.e dev2,but neither Person.class files nor the Person.java file is
available in the current source stream.

Compiler when it finds class dependency for a class and when it is not available it will look
for another system environment variable like path which is named ‘CLASSPATH’ entries
with the delimiter ‘;’ for multiple values.and hence we have to update ‘CLASSPATH’
variable.

There is 3 ways to develop classpath

1.COMMAND WISE
For Compilation:

dev2\src>javac –d ../class –classpath ../../dev1/class com/lara/*.java


Compilation success.and it has generated the .class file in the class location.

For Execution:
dev2\class>java com.lara.Manager
NoClassDefinitionFoundError:Person
With the above command,execution will not be successful because for execution also it
requires the .class file to be loaded into the memory while executing.
dev2\class>java –classpath ../../dev1/class com.lara.Manager
NoClassDefinitionFoundError:Manager
The above command will not be compiled even though the ‘classpath’ is updated to the
Person class file location,because when the classpath is updated then it will look for all the
files in the updated ‘classpath’ entries,if it doesn’t find it will not be compiled successfully
and says “NoClassDefinitionFoundError:Manager”
dev2\class>java –classpath ../../dev1/class;. com.lara.Manager
Execution success
o/p:- hello
2.COMMAND WINDOW WISE

dev2\class>set classpath = ../../dev1/class


dev2\class>java com.lara.Manager
NoClassDefinitionFoundError:Manager

For Successful Execution:


dev2\class>set classpath = ../../dev1/class;.
dev2\class>java com.lara.Manager
OR

www.javaeasy2all.com 203 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

dev2\class>set classpath = .;../../dev1/class;


dev2\class>java com.lara.Manager
the above execution is run successfully,but if at all some other developer of the same system
has already set with the other application and if it is necessary for us to run with our current
execution this will not be successful because set command will not append new location to
existing variable’s value but it replace with new entry.so we should retrieve the existing
values and we have to update it like

dev2\class>set classpath =%classpath%; ../../dev1/class;.


dev2\class>java com.lara.Manager
OR
dev2\class>set classpath = .; ../../dev1/class; %classpath%;
dev2\class>java com.lara.Manager
3.SYSTEM WISE
a)Right click on my computer folder on the desktop of our computer or go to control panel
and double click on system folder,if you don’t find mycomputer on the desktop.
b)Select the advanced tab.
c)Click on the button which is named Environment variables.
d)Click on the new button if there is no ‘classpath’variable existing already or if it is existing
click on edit button.
e)Give ‘classpath’ in the variable name textfield.
f)Give all entries delimited with ‘;’ in the variable value textfield.
g)Open the new window for changes to be applied.
NOTE:-In this way of updating ‘classpath’ also it is necessary for mentioning ‘.’ Either as the
first entry or last entry.

dev1

src

com

lara
class

Hello.java
com

dev2
lara

src Hello.class

www.javaeasy2all.com com204 08041310124

lara

class Manager.java
RS TECHNOLOGIES LARA TECHNOLOGIES

Hello.java Manager.java

package com.lara; package com.lara;


public class Hello import com.lara.Hello;
{ public class Manager
public String sayHello() {
{ public static void main(String args[])
return “HelloToAll”; {
} Hello h1 = new Hello();
} String s1 = h1.sayHello()
System.out.println(s1);
}
}

COMMAND WISE
For Compilation:
dev2\src>javac –d ../class –classpath ../../dev1/class com/lara/*.java
For Execution:
dev2\class>java –classpath ../../dev1/class;. com.lara.Manager
o/p:- HelloToAll

COMMAND WINDOW WISE


dev2\class>set classpath = ../../dev1/class;.
dev2\class>java com.lara.Manager
OR
dev2\class>set classpath = .;../../dev1/class;
dev2\class>java com.lara.Manager

SYSTEM WISE
Its not necessary every time.system wise we will set one time only.

www.javaeasy2all.com 205 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

dev1

src

Pack1

A.java
class

Pack1
1
dev2
A.class
src

Pack2

B.java

class

Pack2
B.class

A.java B.java

package pack2;
package pack1; class B
public class A {
www.javaeasy2all.com 206 08041310124
{ public static void main(String args[])
public static int i = 10; {
} System.out.println(pack1.A.i);
RS TECHNOLOGIES LARA TECHNOLOGIES

Compilation of class A:
dev1\src>javac –d ../class pack1/A.java
compilation success
Compilation of B class:
dev2\src>javac – d ../class –classpath ../../dev1/class pack1/B.java
compilation success
For Execution:
dev2\class>java pack2.B
NoClassDefinitionFoundError
dev2\class>java –classpath ../../dev1/class;. pack2.B
o/p:- 10
CLASSPATH

dev

src

com

lara
class

A.java
com

dev
lara

src A.class

com

rst

class Manager.java

com

www.javaeasy2all.com 207 rst 08041310124

Manager.class
RS TECHNOLOGIES LARA TECHNOLOGIES

Both dev are in different drive.

A.java Manager.java

package com.lara; package com.rst;


public class A class Manager
{ {
public String hello() public static void main(String args[])
{ {
return “abc”; com.lara.A a1 = new com.lara.A();
} System.out.println(a1.hello());
} }
}

D:\dev\src\javac –d ../class com/rst/*.java


CTE
D:\dev\src>javac – d ../class –classpath C:/dev/class com/rst/*.java
Compilation success
D:\dev\class>java com.rst.Manager
NCDFE:com/lara/A
D:\dev\class>java -classpath C:\dev\class;. com.rst.Manager
o/p: abc

JAR FILE
Jar stands for “java Archive” file.using a command called ‘jar’ only it is possible to make a
jar file.jar content will be only class folder content .
Jar file is a portable file.it is like a zip file.you can send the jar file from one location to other
location.

www.javaeasy2all.com 208 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Jar command should be triggered from classes folder only,because we require class files only
to be in the jar file.jar file is meant for packing all .class files in to a file.
Jar command options
Jar cf filename.jar
C-creation of the jar file
F-file name with jar as extension with absolute path or relative path location where jar file is
to be generated and kept(if we don’t mention relative or absolute path it will generate in the
classes folder only.

There is 2 file .All are in different drive.A.java is inside C drive with package name
pack1.B.java is in D drive with package name package name pack2.Drive C containing dev1
folder.inside dev1 3 folder is there,that folder is src,class and lib.Inside src A.java file.Drive
D also containing these 3 folder.inside src B.java file.

A.java B.java

package com.pack1;
package com.pack2;
public class A
import com.pack1;
{
public class B
public void test1()
{
{
public static void main (String args[])
System.out.println(“A.test1”);
{
}
A a1 = new A();
}
a1.test1();
}
}

C:\dev1\src>javac –d ../class com/pack1/*.java


C:\dev1\class>jar cf ../lib/a.jar *
D:\dev2\src>javac – d ../class com/pack2/*.java
NCDFE
D:\dev2\src>javac –d ../class –classpath C:\dev1\class
Compilation success.
D:\dev2\src>javac –d ../class –classpath C:/dev1/lib/a.jar com/pack2*.java
D:\dev2\src cd ../class
D:\dev2\class>java com.pack2.B
CTE:-NCDFE
D:\dev2\class>java –classpath C:/dev1/class;. com.pack2.B
o/p:- A.test1

www.javaeasy2all.com 209 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

D:\dev2\class>java –classpath C:/dev1/lib/a.jar;. com.pack2.B


o/p:- A.test1

IF WE WILL NOT UPDATE CLASSPATH ANY WHERE THEN GO


FOR THIS WAY
Jdk6.0
jre
lib
ext(copy all jar file inside ext folder like a.jar)
then go to cmd:
D:\dev2\class>java com.pack2.Manager
o/p:- A.test1().
NOTE:-But not advisible to use this method.

IN LINUX

Classpath1

root

Desktop

kiran

classpath

src

Pack1 A.java

classes

Pack1
A.class

www.javaeasy2all.com 210 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

Program
package pack1;
class A
{
public int i=10;
}
public class Design
{
public static void main(String[] args)
{
A a1=new A();
System.out.println(a1.i);
}
}
/root/Desktop/kiran/classpath/src> javac -d ../classes pack1/Design.java
/root/Desktop/kiran/classpath/src> cd ../classes
/root/Desktop/kiran/classpath/classes> java pack1.Design
10
Program
package pack1;
class A
{
int i=20;
}
package pack2;
public class B
{
public static void main(String[] args)
{
A a1=new A();
System.out.println(a1.i);
}
}
/root/Desktop/kiran/classpath/src> javac -d ../classes pack1/A.java
/root/Desktop/kiran/classpath/src> javac -d ../classes pack2/B.java
compile time error
Program
package pack1;
public class A
{

www.javaeasy2all.com 211 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

public int i=20;


}
package pack2;
public class B
{
public static void main(String[] args)
{
A a1=new A();
System.out.println(a1.i);
}
}
/root/Desktop/kiran/classpath/src> javac -d ../classes pack1/A.java
/root/Desktop/kiran/classpath/src> javac -d ../classes pack2/B.java
compile time error
Program
package pack1;
public class A
{
public int i=20;
}
package pack2;
import pack1.A;
public class B
{
public static void main(String[] args)
{
A a1=new A();
System.out.println(a1.i);
}
}
/root/Desktop/kiran/classpath/src> javac -d ../classes pack1/A.java
/root/Desktop/kiran/classpath/src> javac -d ../classes pack2/B.java
/root/Desktop/kiran/classpath/src> cd ../classes
/root/Desktop/kiran/classpath/classes> java pack2.B
20
Program
inside dev1
package pack1;
public class C
{
public int i=20;
public int j=30;
}
inside dev2
www.javaeasy2all.com 212 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

package pack2;
import pack1.C;
public class D
{

public static void main(String[] args)


{
C c1=newCA();
System.out.println(c1.i);
System.out.println(c1.j);
}
}
[root@localhost Desktop]# cd /root/Desktop/kiran/classpath/dev1/src
[root@localhost src]# javac -d ../classes pack1/C.java
[root@localhost src]# cd ..
[root@localhost dev1]# cd ..
[root@localhost classpath]# cd dev2
[root@localhost dev2]# cd src
[root@localhost src]# javac -d ../classes -classpath ../../dev1/classes pack2/D.java
[root@localhost src]# cd ..
[root@localhost src]# cd ../classes
[root@localhost classes]# java -classpath ../../dev1/classes:. pack2.D
20
30
Program
package com.lara.calsi;
public class Calculator
{
public int add(int a,int b)
{
return a+b;
}
}
package com.rst.calsi;
public class Calculator
{
public static void main(String[] args)
{
Calculator intcalci=new Calculator();
Calculator strcalci=new Calculator();
int sum= intcalci.add(4,5);
String strsum=strcalci.add(“ja”,”va”);
System.out.println(sum);
System.out.println(strsum);
www.javaeasy2all.com 213 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
}
[root@localhost src]# pwd
/root/Desktop/kiran/classpath/dev1/src
[root@localhost src]# javac -d ../classes com/lara/calci/Calculator.java
[root@localhost src]# javac -d ../classes com/rst/calci/Manager.java
[root@localhost src]# cd ../classes
[root@localhost classes]# java com.rst.calci.Manager
9
java
Program
package com.lara.calsi;
public class Calculator
{
public int add(int a,int b)
{
return a+b;
}
}
package com.rst.calsi;
public class Calculator
{
public static void main(String[] args)
{
Calculator intcalci=new Calculator();
Calculator strcalci=new Calculator();
int sum= intcalci.add(4,5);
String strsum=strcalci.add(“ja”,”va”);
System.out.println(sum);
System.out.println(strsum);
}
}
command wise
[root@localhost classes]# cd /root/Desktop/kiran/classpath/app1/src
[root@localhost src]# javac -d ../classes com/lara/calci/Calculator.java
[root@localhost src]# pwd
/root/Desktop/kiran/classpath/app1/src
[root@localhost src]# cd ../../app2/src
[root@localhost src]# javac -d ../classes -cp ../../app1/classes
com/rst/calci/Manager.java
[root@localhost src]# cd ../classes
[root@localhost classes]# java -cp ../../app1/classes:. com.rst.calci.Manager
9

www.javaeasy2all.com 214 08041310124


RS TECHNOLOGIES LARA TECHNOLOGIES

java

commandwindow wise
[root@localhost classes]# export CLASSPATH="../../app1/classes:."
[root@localhost classes]# java com.rst.calci.Manager
9
Java
System wise
1.Double click on computer.
2.Go to file system.
3.Go to etc.
4.Go to profile.txt(open with gedit).
5.At last go to bottom and set like this:-
CATALINA_HOME=/root/Desktop/linux-sw/tomcat/tomcat;export
CATALINA_HOME PATH=$CATALINA_HOME/bin:$PATH;export
CLASSPATH=/root/Desktop/kiran/classpath/app1/classes:. CLASSPATH

STATIC IMPORTS
1. package com.lara;
import static java.lang.System.out;
public class Manager
{
public static void main(String[] args)
{
out.println("Hello");
out.println("world");
}
}
Output
Hello
world
2. package com.lara;
import static java.lang.System.*;
import static java.lang.Math.*;
public class Manager
{
public static void main(String[] args)
{
double d = sqrt(24);
out.println(d);
}
www.javaeasy2all.com 215 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

}
Output
4.898979485566356

3. package com.lara;
import static java.lang.System.*;
import static java.lang.Math.*;
public class Manager
{
public static void main(String[] args)
{
int i = 5;
int j = 6;
double d = pow(i,2) + pow (j,3);
out.println(d);
}
}
Output
241.0
4. package com.lara;
import static java.lang.System.*;
public class Manager
{
public static void main(String[] args)
{
out.println("Lara");
exit(0);
out.println("RST");
}
}
Output
Lara
5. package com.lara;
public class Manager
{
public static void main(String[] args)
{
System.out.println(1);
System.exit(0);
System.out.println(2);
}
}
Output
1
www.javaeasy2all.com 216 08041310124
RS TECHNOLOGIES LARA TECHNOLOGIES

www.javaeasy2all.com 217 08041310124

You might also like