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

Cast Question

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone.I got a question like that
Which statements, when inserted at the
indicated position in the following code,
will cause a runtime exception when
attempting to run the program?
class A {}
class B extends A {}
class C extends A {}
public class Q3ae4 {
public static void main(String
args[]) {
A x = new A();
B y = new B();
C z = new C();
// insert statement here
}
}
[Select All]
[1]x = y;
[2]z = x;
[3]y = (B) x;
[4]z = (C) y;
[5]y = (A) y;
The given answer is

[Correct Choices]
[1] false
[2] false
[3] true
[4] false
[5] false
but when i run the program.its ok and no Exceptions throws.
Who can explain it ?
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the question asked for runtime exception.
If you tried each of these, you should find that options [2], [4], [5] will result in compile time exceptions . Option [1] compiles and runs. Which only leaves option [3] as your choice.
[1] runs because y, an instance of B, isaA.
[2] x is not a C, so no implicit conversion occurs. You have to explicitly cast it
[3] similar to [2], x is not a B, however an explicit cast is used in this case, so this will compile. Although because x is actually not a B, a ClassCastException will be thrown at runtime
[4] classes B and C both extend A, which means that they are sibling classes. Conversion between them is disallowed by the compiler

[5] Obviously, y is of type B, the statement is trying to assign an type of A to a type of B, which means the compiler will complain abou it.
HTH
 
guo mark
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3x chafule razgul.I got you idears. .
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm.. I'm having some issues with your answers for this one chafule..
Rewrote the code so its easier to read :
class Alpha {}
class Beta extends Alpha {}
class Chi extends Alpha {}
public class CastTest {
public static void main(String [] args) {
Alpha a = new Alpha();
Beta b = new Beta();
Chi c = new Chi();
a=b;
//b=c; //need explicit cast
b = (Beta)a; //should be OK to downcast
//b = (Beta)c; //can't cast to sibling classes
//c = (Alpha)c; //same as saying c=a
}
}
This is what I think:
1) will work because b is a subclass of a so its implicitly cast.
2) needs explicit cast since its a downcast (compile error)
3) should be ok since it is explicitly downcasted (no runtime error for me)
4) can't cast to sibling classes (compile error)
5) same as 2.. can't make c=a since that is a downcast
So it looks like there are no runtime errors here.. only compile time errors? What do you think?
 
We're being followed by intergalactic spies! Quick! Take this 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