• 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

doubt in Generics question from javabeat.net

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
this is question is from javabeat mock exam

The code given as


what is the answer of below and why?


Question 6 :

which statement is true?

a)The lines 2 and 3 will cause a compile error.

b)The line 4 will cause a compile error.

c)The line 5 will cause a compile error because a cast is missing.

d)The source code will be compiled with warning(s). During the runtime a ClassCastException will be thrown in the line 5.

e)The soure code will be compiled with warning(s). No exception will be thrown during the runtime.



Exam answer is:

Answer6 : d

c) No, a cast is not necessary. In a type safe code bO can contain only an orange. But our code is not type safe, because we use the generic class Basket<E> without specifying the concrete type for the type variable E. That is why we will be warned by the compiler.



can anyone justify the answer

[ August 16, 2007: Message edited by: madhu v pe ]
edited the too long lines, Bu.
[ August 16, 2007: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think if at line no 5 if code is

then their would be no exception
because in

bO.getElement() returns object of type Apple not Orange thats why classCast Exception will be thrown.
Orange is the sibling of Apple and doesn't know about Apple
[ August 16, 2007: Message edited by: Ameen khan ]
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(d)is correct. Since we are assigning a non type-safe object "b" to the type-safe objects "bA" and "bO" so the compiler will warn us. Now both bA and bO are referencing the same object b. Here comes the problem. In line 4 we are setting the element with Apple type object but trying to assign the element in Orange type variable in line 5, hence causing the ClassCastException.

(c) is not correct. Cast is not necessary here because compiler already know that bO can contains only the object of type Orange and bO.getElement() will return an object of Orange type only.
[ August 16, 2007: Message edited by: Al Mamun ]
 
I found a beautiful pie. And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic