sample questions from the AP COMPSCI course description

<p>i am stumped on a couple of the ap compsci sample questions. please help!</p>

<h1>15 - why is it ABDC? why does it print D?</h1>

<h1>20 - why does it print world and not worldpeace?</h1>

<p>Here is a link to the questions:
<a href="http://apcentral.collegeboard.com/repository/ap04_cd_compsci_0405__4315.pdf%5B/url%5D"&gt;http://apcentral.collegeboard.com/repository/ap04_cd_compsci_0405__4315.pdf&lt;/a>
they begin on page 36.</p>

<p>Because of polymorphism. </p>

<p>Base b = new Derived()</p>

<p>The actual class we are talking about is an instance of derived. Thus, calling b.methodOne() invokes methodOne of the derived class. The derived class calls the superclass's methodOne which prints A. Then the superclass methodOne calls methodTwo. methodTwo is declared for class Derived, so it calls methodTwo in Derived, which calls the superclass methodTwo which prints B. Then D is printed since program control reverts to the proper method in Derived. Then C is printed because program control reverts back to Derived's method one which prints C last.</p>

<p>because Strings and primitive types are passed by value to a method and not by reference like objects. Thus, any changes made to a string or integer remain only in the method and not outside of it unless the values are returned from function using a return statement.</p>

<p>thx shahein, i get it.</p>

<p>just to make sure, for question #20: When the superclass methodOne(); calls methodTwo();, it calls the subclass method because the object being worked on is actually Derived right? so because of polymorphism, it chooses the methodTwo(); from derived right?</p>

<p>yes, exactly.</p>

<p>My second post was an explanation to question 20... I seem to have neglected to mention that.</p>

<p>i was aware of that. on that question i was concentrating on how the String wouldn't be changed b/c Strings are Immutable. but yea i realized the variables x and y would be destroyed after the method call anyways. thx MUCHOS!</p>