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
<PRE>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
}
}
</pre>
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).]