Hi everybody,
I had been trying some mock
SCJP exam questions kindly provided by John Meyers (
http://www.badongo.com/file/1591104), and I need some clarification on some of the solutions. I will greatly appreciate it if someone can answer the questions I have below.
Question 4:
int[] a = null , b [] = null; //line 1
b = a;
System.out.println( b );
Solution states that b is a two dimension array. I am not used to the syntax on line 1. Is it equivalent to
int[] a = null;
int[] b [] = null; ?
Question 6:
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);
}
}
}
}
Solution states that "one little piggy two little piggies three little piggies" will be printed. However, the for loop has type of Object. Doesn't it need to be downcasted to string or overwrite the "toString" of test so that it can printed properly?
Question 41
package packed;
public class pack
{
static public int x1 = 7;
static protected int x2 = 8;
static int x3=9;
static private int x4 =10;
}
import packed.pack;
class test extends pack
{
public static void main( String args[] )
{
pack p = new pack();
System.out.println( pack.x2 );
}
}
Solution states that 8 will be printed. Since the variable is static it can still be accessed from another package. If it were not static you would get an error saying protected member access error. I don't understand the solution too well. How does the fact that the variable is static have to do with it being allowed to be accessed from another package? furthermore, if the variable is not static, I thought it can still be accessed. "Protected" modifier allows members to be accessed by subclass even if it is in a different package. Since test is a subclass of pack, why can it not access a protected member of pack?
Question 69
class test
{
test tester;
test( test t )
{
tester = t;
}
test()
{}
public static void main( String args[] )
{
test t = null;
t = new test(); // line 1
t = new test(t); // line 2
t = null; // line 3
}
}
Solution states that 2 objects will be eligible for garbage collection. I thought 4 objects will be eligible. After line 1, a test object is created with a variable tester (2 objects). After line 2, another test object is created with its own variable tester (another 2 objects). When reference is finally nulled on line 3, isn't there a total of 4 objects for garbage collection?