Page 13 of 55 FirstFirst ... 3111213141523 ... LastLast
Results 241 to 260 of 1093

Thread: Quiz Time

  1. #241
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, here is another question form me,

    Java Code:
    class Test {
        
        public void method(String s) {
            System.out.println("String version");
        }
        
        public void method(Object o) {
            System.out.println("Object version");
        }
        
        public static void main(String[] args) {
            Test t = new Test();
            t.method(null);
        }
    
    }
    What's the output of this class?

  2. #242
    DevzAbhi's Avatar
    DevzAbhi is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    aNSWER WOULD BE .....Object version


    hopefully if i am correct

    null is an object in java .

  3. #243
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  4. #244
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    by elimination technique :cool:

    Object version can't be the answer
    answer could be either
    > String version
    > or ambiguity in methods.

    i will go with the first One, "string version" . :eek:
    i am the future

  5. #245
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, the answer is "string version". Just think about the class hierarchy. ;) You can find the solution easily.

  6. #246
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    Guess the output, remember the RULE.

    Java Code:
    List list = new ArrayList();
    list.add ( "rakesh" );
    list.add ( "java" );
    list.add ( "puzzle" );
    for ( Iterator it = list.iterator; it.hasNext(); ){
      s.o.p ( it.next());
      list.add ( "showstopper" );
    }
    i am the future

  7. #247
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Seems it's a tricky question in a greater way. I hope there is a compilation and a runtime error.

    Compilation error is on the iterator. Missing () at the iterator method. It's a method, not a instance variable for the list. It's easy to remember because lots of people mess with length in arrays and length() in strings. :)

    Runtime error, hmm actually I'm not sure this, but I hope it is, after display the first element of the list you can't add an element again to the list. There should be concurrent exception, because concurrently values are modified. :(

  8. #248
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    Compilation and runtime exception :eek: . Never knew is it possible.
    Vaise, again Eranga you are correct :) .


    Missing () at the iterator method
    Sorry for this, i forgot to write '()' :p
    i am the future

  9. #249
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Hurray :D I'm correct.

    See that pal, what I said is proof here too. Mess with () signs in some cases. Actually this can't be an issue if a developer use a Java IDE. Because it's simply notify such errors.

    Since my concurrent issue is correct, there is another possibility to clash with. Exceeding the memory size on heap. concurrent issue is take place since you print a value. Actually you get the first result is printed. Then try to adding a new value to the list. That's the issue. Say I'm not print any value to the console. Then what happened is keep adding values to the list in limitless way. So in a few second you get the heap size exception too.

  10. #250
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    almost correct :D

    --

    you will get ConcurrentModificationException in all cases, whether you print it or not. Once you use list.iterator(). the list will be locked for modification till last element is iterated.
    i am the future

  11. #251
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    rjuyal, I'm confusing with one thing. What you mean list is locked. I hope JVM not allowed to modify the list, if so how? Do you know what happened there?

  12. #252
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    guess the output

    Java Code:
    Set set = new LinkedHashSet();
    set.add ("rakesh");
    set.add ("juyal");
    System.out.println(set.get(2));
    i am the future

  13. #253
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    this is the code of ArrayList >> AbstractList actually :rolleyes:

    Java Code:
    public void add(E o) {
                checkForComodification();
    
    	    try {
    		AbstractList.this.add(cursor++, o);
    		lastRet = -1;
    		expectedModCount = modCount;
    	    } catch(IndexOutOfBoundsException e) {
    		throw new ConcurrentModificationException();
    	    }
    	}

    Java Code:
    final void checkForComodification() {
    	    if (modCount != expectedModCount)
    		throw new ConcurrentModificationException();
    	}

    Now does it make sense.
    i am the future

  14. #254
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Seems I'm mes with this pal. :(

    I think there is a null pointer exception in index referring. That is in index 2, there is no element in set. But I can't confirm it, because I'm not sure that Set has a get() method or it's only valid with TreeSet. I'm really mess here pal.

  15. #255
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by rjuyal View Post
    this is the code of ArrayList >> AbstractList actually :rolleyes:

    Java Code:
    public void add(E o) {
                checkForComodification();
    
    	    try {
    		AbstractList.this.add(cursor++, o);
    		lastRet = -1;
    		expectedModCount = modCount;
    	    } catch(IndexOutOfBoundsException e) {
    		throw new ConcurrentModificationException();
    	    }
    	}

    Java Code:
    final void checkForComodification() {
    	    if (modCount != expectedModCount)
    		throw new ConcurrentModificationException();
    	}

    Now does it make sense.
    Yep, I got the point. Now it's clear. Thanks buddy.

  16. #256
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    I'm not sure that Set has a get() method or it's only valid with TreeSet
    :)

    Set Interface don't have get method, not even TreeSet.
    but i don't know what's the reason behind this :eek:
    i am the future

  17. #257
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Seems that I'm mess with theories on collections. ;)

    Let read some docs and try to have an idea what happened there.

  18. #258
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    Please let me know if you got the answer why Set don't have get method. or if you have some link please post here.
    i am the future

  19. #259
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Sure pal. I'll try to refer some docs tonight. I'll let you know if I found something new on that case.

  20. #260
    suprabha is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

Page 13 of 55 FirstFirst ... 3111213141523 ... LastLast

Similar Threads

  1. Hello, first time here.
    By ludragon in forum Introductions
    Replies: 2
    Last Post: 01-03-2008, 06:03 AM
  2. Help pls with a quiz
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 07:09 AM
  3. Time method
    By carderne in forum New To Java
    Replies: 5
    Last Post: 11-05-2007, 10:34 AM
  4. DataObject with the time given by me
    By garinapavan in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 07:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •