aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can anybody help me!!!!!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can anybody help me!!!!!!!!!" Watch "Can anybody help me!!!!!!!!!" New topic
Author

Can anybody help me!!!!!!!!!

Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Hi guys!!,
Can u pls help me in coping up with following doubts these are from K & B book,Chapter 3,self test)

1:Given:

class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
} }


When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

Ans: C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

Doubt: C1 is referenced by C3 then how can it be eligible for GC. And if u say,that it is, than why not C2..


2:Given:

1. class Dims {
2. public static void main(String[] args) {
3. int[][] a = {{1,2,}, {3,4}};
4. int[] b = (int[]) a[1];
5. Object o1 = a;
6. int[][] a2 = (int[][]) o1;
7. int[] b2 = (int[]) o1;
8. System.out.println(b[1]);
9. }
10. }
What is the result?
A. 2
B. 4
C. An exception is thrown at runtime.
D. Compilation fails due to an error on line 4.
E. Compilation fails due to an error on line 5.
F. Compilation fails due to an error on line 6.
G. Compilation fails due to an error on line 7.

Ans:C is correct. A ClassCastException is thrown at line 7 because o1 refers to an int[][] not an int[]. If line 7 was removed, the output would be 4.

Doubt: We know that "If you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the reference you're assigning it to."
But in this Question at line 5 : Object o1 = a;Are we not assigning anything here???
Or, does this mean that we are creating an Object type of object which is referring to the same array object as 'a' on heap.

3: Given:

1. class Zippy {
2. String[] x;
3. int[] a [] = {{1,2}, {1}};
4. Object c = new long[4];
5. Object[] d = x;
6. }
What is the result?
A. Compilation succeeds.
B. Compilation fails due only to an error on line 3.
C. Compilation fails due only to an error on line 4.
D. Compilation fails due only to an error on line 5.
E. Compilation fails due to errors on lines 3 and 5.
F. Compilation fails due to errors on lines 3, 4, and 5.

Ans: A is correct, all of these array declarations are legal. Lines 4 and 5 demonstrate that arrays can be cast..

Doubt: How come line 4: Object c = new long[4];
is correct because there are no square brackets on left side of the expression...Is it the correct syntax for an array???


Please help me out....


Thanks
Swapnil
[ June 15, 2006: Message edited by: Swapnil Trivedi ]

SCJP 5.0<br />-----------<br />"Help Ever && Hurt Never"
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Q1, you can check thread 1 and thread 2.

Q3, array is an Object.
[ June 15, 2006: Message edited by: wise owen ]
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Hi,

Naseem explained it as: {Simple answer is>>> c3 was not pointing to any object before call to c1.go(c2). So setting it to null by method call will not hav ny effect on gc.}

I am sorry but I didn't get her point....does it mean that c3 is not refering to any of the object(C1 & C2).
And if it's so then why C2 is not eligible for GC. It's also null.

Thanks & regards
Swapnil
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Code 1:

Replace Short story=5; to Short story=200; See K&B Errata link at the top of SCJP forum home.

When story is 200 outside the range of (-128 to 127) then two Short object will be created.



When you set c1=null, then two objects will be garbage collected. CardBoard object and included Short object.

But if Short story=5;

then



Both refer to same Short object in this case.

So only one object is garbage collected here only CardBoard not Short because it is still referenced by CardBoard c2.

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

Code 2:

Originally posted by Swapnil Trivedi:
But in this Question at line 5 : Object o1 = a;Are we not assigning anything here???
Or, does this mean that we are creating an Object type of object which is referring to the same array object as 'a' on heap.



Correct. We are just assigning a which is already created on heap in o1 which is of type Object.
--------------------------------------------------------------------

Code 3:


Originally posted by Swapnil Trivedi:
Doubt: How come line 4: Object c = new long[4];
is correct because there are no square brackets on left side of the expression...Is it the correct syntax for an array???



java.lang.Object is mother of all. any object can be assigned to it.

Remember arrays in java are also Objects.

Regards

Naseem Khan
[ June 16, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809

Originally posted by Swapnil Trivedi

Naseem explained it as: {Simple answer is>>> c3 was not pointing to any object before call to c1.go(c2). So setting it to null by method call will not hav ny effect on gc.}

I am sorry but I didn't get her point....does it mean that c3 is not refering to any of the object(C1 & C2).
And if it's so then why C2 is not eligible for GC. It's also null.



When you are calling go(...) method by passing c2 as an argument, then cb will also start pointing to same CardBoard object which c2 is pointing.

Within go(...) method, cb is set to null. But still you have a reference to your second CardBoard object which is c2.

So second cardboard object will not be garbage collected.

Regards

Naseem
[ June 16, 2006: Message edited by: Naseem Khan ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Yes, I believe it is down the hall and to the right.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Thank you Naseem....


Regards
Swapnil
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can anybody help me!!!!!!!!!