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

Am I wrong in understanding this MindQ qstn?

 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It compiles and executes. Any subclass object can be denoted by any super class object reference variable.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic