• 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

Usage of instanceof in generics

 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java. util.*;
class ForInstance {
public static void main( String[] args) {
Object o = new HashSet< Double>();
// (1) Insert code here.
}
}
Which code when inserted at (1) will result in the program to compile and execute normally? Select the one correct answer.

(a) System. out. println (o instanceof Set);
(b) System. out. println (o instanceof Set<?>) ;
(c) System. out. println (o instanceof Set< Double>) ;
(d) System. out. println (o instanceof Set<? extends Number>) ;
(e) System. out. println (o instanceof Set<? super Number>) ;

I was sure that the (a) and (b) were the right answers and confirmed myself right by compiling, but the question says "one" correct answer. So do I safely assume that the question is wrong? If not, which would be a better choice?
[ October 02, 2005: Message edited by: Barry Gaunt ]
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure both a) and b) would run as expected?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the source of this question? Usually mocks tell you the answers and give an explanation of the reason.
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Ernest, both of them print 'true' and comile without warnings/errors.

Barry, the question if from a presentation by Khalid Mughal, I just happened to bump into the pdf on the net. And unfortunately, it provides no answers. One more thing Barry, is there any way to shut off the graemlins? they're mocking the generics questions!!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic