Author Topic:   Am I wrong in understanding this MindQ qstn?
maha anna
bartender
posted March 03, 2000 12:34 PM             
I was taking the MindQ test and found 4 Qstns have given wrong answers.
(well.. may be 3 if I am wrong about this qstn. ) This is Qstn no 34.First of all these gives statements will not compile because the 'super' keyword is used as an identifier. Assuming this is a typo, I think the answer is 'c'. But the given ans was 'a'.
What I think is whether we shd prove the other way or just take for granted that these are the only statements we compile and run in this context. Any thoughts will be appreciated.
Other wrong ones are Q33,Q44,Q47 for obvious reasons.
Thank you.
The foll code explains my reasoning
code:
class Super {}
class Sub1 extends Super {}
class Sub2 extends Super {}
class test {
public static void main(String[] args) {
Super sup = new Super();
Sub1 sub1 = new Sub1();
Sub2 sub2 = new Sub2();

sup = sub1;
Object obj = new String("maha");
sub1 = (Sub1)obj;// explains Qstn 34 wrong
//Ok at compile, ClasscastException at runtime
}
}


Assume that Sub1 and Sub2 are both subclasses of class Super.

Given the declarations:
Super super = new Super();
Sub1 sub1 = new Sub1();
Sub2 sub2 = new Sub2();
Which statement best describes the result of attempting to compile and execute the following statement:
super = sub1;
a) Compiles and definitely legal at runtime
b) Does not compile
c) Compiles and may be illegal at runtime


[This message has been edited by maha anna (edited March 03, 2000).]

Tony Alicea
sheriff
posted March 03, 2000 12:55 PM             
It compiles and executes. Any subclass object can be denoted by any super class object reference variable.

Jim Yingst
sheriff
posted March 03, 2000 01:01 PM             
I wouldn't worry too much about what other code might be present that they didn't mention - that probably isn't the intent of the question. In any event, I don't think you'll ever get an error from the line they ask about, "sup = sub1;". If you add other code, you might get errors from the added lines, but not from that one.

Jane Rozen
ranch hand
posted March 03, 2000 01:18 PM             
As for
sup = sub1; // this is perfectly legal

But look carefully at the code you added:

Object obj = new String("maha"); //OK.
sub1 = (Sub1)obj; //?

Your obj is referring to a totally unrelated class String, not to Sub1, so in this case you'll get runtime error.

Manju Swamy
greenhorn
posted March 03, 2000 04:25 PM         
Also you are getting confused with Super class name and super keyword.

Careful reading always helps !!!

[This message has been edited by Manju Swamy (edited March 03, 2000).]

Ramana Namuduri
greenhorn
posted March 03, 2000 04:55 PM             
"Super" and "super" are not one and the same.
Also, obj1 and Sub1 are no where related to each other. Hence it gives a runtime error altough it compiles successfully
Thanks

Jim Yingst
sheriff
posted March 03, 2000 09:41 PM             
Manju & Ramana- As Maha pointed out, the original question illegally used super as an identifier - she corrected it in her code, changing it to "sup". "Super" is used as the name of a class, and "sup" is the name of a variable. I don't see any place in her code that "super" is used, so there's no problem.

Jane- yup, exactly.

|