• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interview questions

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have faced these questions in an interview. Please give your answers.

-Difference between enumeration and iterator
-Why do you need ThreadLocal

I answered first one saying iterator is not synchronized and enumeration is synchronized.

I didn't answer second one. Please help.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Originally posted by avneesh singh:

I answered first one saying iterator is not synchronized and enumeration is synchronized.



Ugh. What do you think it means for an interface to be synchronized? The answer is "nothing" -- this has no meaning. The difference is that Iterator has (1) shorter method names, and (2) a method for removing the current item. Furthermore, Iterator is newer, and preferred. That's it.

Why do we need ThreadLocal? So that you can have a variable whose value is different in each thread. There are many possible uses for this, not just one reason.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q. Difference between enumeration and iterator.

A. Enumeration is a symbolic constant. Also used for the same purpose as an iterator some time in the previous century but I guess that's not relevant, so, symbolic constant. Final answer. An iterator allows you to traverse a collection.

Q. ThreadLocal?

This is where Java shows its Visual Basic roots. Takes a non-thread-safe object (that would be everything and anything in Visual Basic) and creates a thread-safe box by binding a shared static object to a specific thread. Even though it is not thread-safe, each thread gets its own copy. Veebee afficianados call that creating an "apartment". Only one veebee programmer ever understood what the heck that actually meant.

Interview Tip: don't ask to phone a friend or ask the audience if you get a particularly difficult question
[ September 02, 2005: Message edited by: Rick O'Shay ]
 
avneesh singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest and Rick, thank you for the replies. It will help me next time. I felt they were difficult questions when I was asked because I didn't prepare for such questions. Rick, your tip is nice
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Rick O'Shay]: A. Enumeration is a symbolic constant.

Ummm... are you referring to an enum? (As the term is used in C/C++.) I think that in this context, "Enumeration" refers to java.util.Enumeration, which is a somewhat different concept from either C/C++ enums, or Java enums. An Enumeration is almost certainly not a symbolic constant.

In my opinion, the differences between java.util.Enumeration and java.land.Iterator are these:
  • Enumeration has overly verbose method names
  • Enumeratio9n is totally unnecessary unless dealing with legacy code
  • Many (not all) implementations of Iterator check (with a small performance cost) against concurrent modifications - which would otherwise be rather difficult to detect. Checking for concurrent modification is well worth the additional effort.
  • Iterator is easier to use with an enhanced for loop. (Although you usually need ot use an Iterable, not an Iterator - but at least an Iteraor is closer.)
  • Enumerations are older
  • Enumerations suck
  • Enumerations should be avoided whenever possible


  • [ September 05, 2005: Message edited by: Jim Yingst ]
     
    Jim Yingst
    Wanderer
    Posts: 18671
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    [Rick]: Interview Tip: don't ask to phone a friend or ask the audience if you get a particularly difficult question

    No - if only because it's useful to know whether you're in an actual interview for a job in the real world, or a contestant on a TV show. I think if you are in an interview, in the real world, it's worthwhile remember that some interview questions are intended to reveal "how do you discover something you don't already know?" rather than "how do you explain something you already know?" In this context, it's worthwhile to be able to describe your strategy for discovering things you don't already know. I would much rather work with someone with a good problem-solving strategy (which often implies a good search strategy), rather than someone who was conviced he (or she) already knew all the answers.
    [ September 05, 2005: Message edited by: Jim Yingst ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic