• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

On the Same liness... as Bill... yet another one from Khalid  RSS feed

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q20) 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 C{}
public class Q3ae4
{
public static void main(String args[])
{
A x = new A();
B y = new B();
C z = new C();
// insert statment here
}
}
Select all valid answers.
a) x = y;
b) z = x;
c) y = (B)x;
d) z = (C)y;
e) y = (A)y;
BOL
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sivaram,
I think line 3 has a typo. And if it is
class C extends B{} Then, these are the effects.
a) x = y;
// is valid & no Compilation or runtime Errors

b) z = x;
// Compilation Err, explicit cast needed.
c) y = (B)x;
// No Compilation Err but Runtime err... ClassCastException
d) z = (C)y;
// No Compilation Err but Runtime err... ClassCastException
e) y = (A)y;
// Compilation Error Explicit cast needed.
Aruna
 
Sivaram Ghorakavi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry C extends A
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sivram
All the other answers except d given by Aruna will be applicable even if "class C extends A "
d) z= (C)y;
// gives a Compiler error "Invalid Cast from B to C"
This is because any two Subclass (from a Single Parent) cannot communicate, We cannot "Cast" them.
I think now ur doubt is cleared
satheesh

Originally posted by Sivaram Ghorakavi:
Q20) 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 C{}
public class Q3ae4
{
public static void main(String args[])
{
A x = new A();
B y = new B();
C z = new C();
// insert statment here
}
}
Select all valid answers.
a) x = y;
b) z = x;
c) y = (B)x;
d) z = (C)y;
e) y = (A)y;
BOL


 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sivaram, thanks for the more posts. This was a good one also, and I think another one that I missed. Here are my answers. The short of it is C is the only one that cause a runtime error.
A. Compiles and runs fine
B. Compile time error
C. Runtime Error
D. Compile time error
E. Compile time error
Here is my reasoning for the above.
A false - this is a legal conversion since you are going up the chain, you are putting B into A and B extends A, so valid.
B false - this need an explicit cast in order to get by the compiler, doesn't have one, so compile error.
C true - You are casting x to a B which the compiler allows, but there is a runtime exception because x never was a B, so you can't cast it.
D false - You can't cast two subclasses to each other, the compiler will check this and throw an error. This is a compile time error.
E false - The cast is legal, but you end up with y, which is a B being casted to an A, and then you trying to force it back into a B. With y now being an A, you can't convert it back to a B without the cast. Of course then you cast would look like this y = (B)((A)y); which is redundant.

Sivaram, I haven't checked with Khalid, so let me know what is correct.
Bill
 
Sivaram Ghorakavi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
It's the C that gives the runtime error. Here is the explanation..
a) no problem. Auto casting up in the chain...
b) this won't compile.
c) Runtime Error. At compilation time explicit casting makes it valid. But @ run time the instance x is not of Class B.
d) Compilation error.
e) Compilation error.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!