Monday, March 3, 2008

SCJP 1.5 Dump questions

Q1
class test
{
public static void main(String[] args)
{
test inst_test = new test();
String pig[][] = { {"one little piggy"}, {"two little piggies"}, {"three little piggies"} };
for ( Object []oink : pig )
{
for ( Object piggy : oink )
{
System.out.println(piggy);
}
}
}
}

a. one little piggy two little piggies three little piggies
b. Compile Error incompatible types.
c. java.lang.String;@187c6c7 java.lang.String;@187c6c8 java.lang.String;@187c6c9
( or something like that )
d. Runtime Null Pointer Exception
e. Prints nothing

Answer1:
a. oink refers to every object reference in a one dimensional row of pig[][].
piggy refers to every object within that row.

--------------------------------------------------------------------------
Q2
class test
{
public static void main(String[] args)
{
test inst_test = new test();
int i1 = 2000;
int i2 = 2000;
int i3 = 2;
int i4 = 2;
Integer Ithree = new Integer(2); // 1
Integer Ifour = new Integer(2); // 2
System.out.println( Ithree == Ifour );
inst_test.method( i3 , i4 );
inst_test.method( i1 , i2 );

}
public void method( Integer i , Integer eye )
{
System.out.println(i == eye );
}
}

a. true false true
b. false true false
c. false false false
d. true true false
e. Compile error

Answer 2:
b: false true false. lthree and lfour are two seperate objects. if the lines 1 and 2 were
lthree = 2 and lfour = 2 the result would have been true. This is when the objects are created in the pool. When the references I and eye in the pool are compared 2==2 results in true and 2000==2000 is false since it exceeds 127.

-------------------------------------------------------------------------
Q3
enum cafe {
BIG ( 10 ) ,
SMALL ( 1 ),
MED ( 5 )
int mySize = 0;
cafe ( int size )
{
mySize = size;

}

}

What happens when this enum is in the code outside a class ?

a. Compiles fine
b. Compiler error
c. Runtime Exception occurs if mySize is accessed.

Answer 3:
a: Compile error: semicolon missing after MED ( 5 ). Watch out for that semicolon when an enum has variables and functions.

---------------------------------------------------------------------

Search Amazon for Best Books on Java J2EE

Blogarama

blogarama - the blog directory

Search your favourite topics

Google