• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I believe it is down the hall and to the right.

Mark
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Naseem....


Regards
Swapnil
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic