• 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

Very hard question for you Java Experts

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Mughal test:
Which statements , when inserted at the indicated
postion, 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 here
}
}
////////////////////////////////////////////
Select All valid Answers:
(A) x= y;
(B) z=x;
(C) y=(B) x;
(D) z=(C) y;
(E) y=(A) y;
( I do not understand why ( the right answer or answers ) is
correct )
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer is (C) y=(B) x. Only A and C can be compiled. But C gets a runtime exception when runs, because x is an instance of A, not B.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frank is right,the answer should ne a. bcoz u are it will be implicitly cast towards the upside of hierachy, as we are assigning object y to x.Correct me if i am wrong.

--Shallender
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shallender:
Frank is right,the answer should ne a. bcoz u are it will be implicitly cast towards the upside of hierachy, as we are assigning object y to x.Correct me if i am wrong.

--Shallender


do u mean the answer should be c? I think it is c
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello every body,
there is a lot of doubt regarding object casting.in the question
b),d) and e) will give compilation error.
always remember derived class object can be assigned to base class object but vice versa is is not correct . for this casting is required.
class A{}
class B extends A {}
class C extends A {}
public class Test {
public static void main(String[]s) {

A x =new A();
B y =new B();
C z =new C();

x = y; //no error
x = z; //no error
y = z; //error becoz there is no relation between class B and class c.

y =(B)x; //no error
z =(C)x; //no error
y =(B)z; // error becoz there is no relation between class B and class c. even casting cannot help.
z =(C)y; // error becoz there is no relation between class B and class c. even casting cannot help.
y =(A) y; //error
z =(A) z ;//error

}}
hoping things will be more clear now if i am wrong please write me. waiting for response.

gautam

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Sanjay and Frank, but I want to add that even though you can get a class to compile by adding a cast, you will still get the RuntimeException when trying to cast from a base class to a derived class. Even with casts, you cannot move "down" the heirarchy tree (right?). Casting just gives you more flexibility when compiling. Generally speaking, if an operation would fail conversion rules, it will fail to cast at runtime as well. For instance this will compile, but gives ClassCastException at runtime:
Object o = new Object();
String s; //Derived from object
s = (String) o;
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No the answer is C.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all!
the answer is c
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for saying this but, this is not a question for experts, is a very basic one, you have to study this topic in deep before taking the exam, it�s very important.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have try to compile all the situation. the Result is show following. but I don't know why the (c) compile good, runtime is error.
==============================
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();
// x = y; //compile and run good
// z = x; //compile error: Q3ae4.java: 18:
//incopatible types
//found : A
//required: C
//z = x;
/* y = (B)x;//compile good, runtime error
exception in thread "main" java.lang.ClassCastException: A
*/
/* z = (C)y; //inconvertible types
// found : B
// required : C

*/

/* y = (A)y; // incompatible types
// found : A
// required: B
*/
}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic