Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Object Reference Conversion

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw this question in a mock exam. I ran the code and it ran with any errors but the exam answer differs, can someone please explain to me the logic behind it. Or is the answer wrong.
What happens when the following code executes?
1:class Base{
2:// legal code
3:}
4:class Derived1 extends Base{
5: // legal code
6:}
7:class Derived2 extends Base{
8:// legal code
9:}
10 ublic class Test
11:{
12: static public void main(String [] args)
13: {
14: Base b = new Derived1 ();
15: Derived1 d1 = new Derived1();
16: Derived2 d2 = new Derived2();
17: b = (Base) d1;
18: b = (Base) d2;
}
}
A. Compile time error at line 14
B. Run time error at line 18
C. Compile time error at line 17
D. NO ERRORS

Question Help.
B. Run time error at line 18
b is of type Base and points to d1 and d2 is neither so at run
time it fails
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Er... it looks like the given answer "b" is just plain wrong. Their explanation makes no sense. Don't worry about it - "d" is correct.
 
Jaishanker Vijayakumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
Er... it looks like the given answer "b" is just plain wrong. Their explanation makes no sense. Don't worry about it - "d" is correct.[/QUOTE
I am not able to give myself an logical explanation to it. Please can you explain it to me why it should work.

 
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
Since I now see that you have posted the same question here, I will close this topic here and answer the question there instead. I prefer not to have the same question being dicussed in multiple places at the same time - it just wastes time as people in one forum don't realize if the question has already been answered in another forum.
 
He baked a muffin that stole my car! And this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic