AP Computer Science Questions

<p>Hey everyone~! I have a couple questions about AP Computer Science review.</p>

<p>1) If on a free response question that used ArrayList if I used arr.length instead of arr.size() will the college board dock points? Is there a table or something that indicates which syntax errors will lead to penalties? For instance, if I leave out a ; at the end of an argument, how many points will the college board take off?</p>

<h2>2) This is Chapter 2, question 19 in Multiple Choice Questions on Classes and Objects in the Barron's book:</h2>

<p>Questions 18-20 refer to the Temperature class shown below:



public class Temperature
{
    private String myScale; //valid values are "F" or "C"
    private double myDegrees;</p>

<pre><code>//constructors
/*default constructor*/
public Temperature()
{ /* implementation not shown */ }

/* constructor with specified degrees and scale */
public Temperature(double degrees, String scale)
{ /* implementation not shown */ }

//accessors
/* Returns degrees for this temperature. */
public double getDegrees()
{ /* implementation not shown */ }

/* Returns scale for this temperature */
public String getScale()
{ /* implementation not shown */ }

//mutators
/* Precondition: Temperature is a valid temperature in degrees Celsius.
 * Postcondition: Returns this temperature, which has been
 * converted to degrees Fahrenheit */
public Temperature toFahrenheit()
{ /* implementation not shown */ }

/* Precondition: Temperature is a valid temperature in degrees Fahrenheit.
 * Postcondition: Returns this temperature, which has been
 * converted to degrees Celsius*/
public Temperature toCelsius()
{ /* implementation not shown */ }

/* Raise this temperature by amt degrees and return it. */
public Temperature raise(double amt)
{ /* implementation not shown */ }

/* Lower this temperature by amt degrees and return it. */
public Temperature lower(double amt)
{ /* implementation not shown */ }

/* Returns true if the number of degrees is a valid
 * temperature in the given scale, false otherwise. */
public static boolean isValidTemp(double degrees, String scale)
{ /* implementation not shown */ }

//other methods not shown...
</code></pre>

<p>}


  1. Consider the following code:

``` public class TempTest { public static void main(String[] args) { System.out.println("Enter temperature scale: "); String scale = IO.readString(); //read user input System.out.println("Enter number of degrees: "); double degrees = IO.readDouble(); //read user input

    /* code to construct a valid temperature from user input */

}

} ```

Which is a correct replacement for /* code to construct... /? I. ``` Temperature t = new Temperature(degrees, scale); if (!t.isValidTemp(degrees,scale)) / error message and exit program */ ```

II. ``` if (isValidTemp(degrees,scale)) Temperature t = new Temperature (degrees, scale); else /* error message and exit program */ ```

III.



if (Temperature.isValidTemp(degrees,scale))
    Temperature t = new Temperature(degrees, scale);
else
    /* error message and exit program */

</p>

<p>A) I only
B) II only
C) III only
D) I and II only
E) I and III only</p>

<p>My reasoning: </p>

<p>Choice III is clearly the most correct answer. It calls the static method correctly and if the parameters are valid it creates a new temperature object with those parameters. Choice II does not work because isValidTemp is not in the TempTest class -- it is in the Temperature class. However, I am confused by Choice I. Although you aren't supposed to call a static method with an instance of the class, technically this will not prevent your code from working, so the call t.isValidTemp(degrees,scale) should still work even though it defies conventional coding. The line above it, Temperature t = new Temperature (degrees, scale); will also work correctly -- even if the scale is invalid the way the Temperature class is defined (like if instead of "C" the user inputs "cow") it should still store the string inputted in the myScale instance variable of object t and not result in an error. Hence, I picked E, which says both I and III are valid. Barrons disagrees with my reasoning for I and says choice C is the correct answer. Thoughts???</p>

<p>1) I don’t actually know where to find information like that. :x</p>

<p>2) The biggest problem isn’t that you’re accessing a class method through an object; like you say, that’s bad form but it will work correctly. The problem is that, if you input an invalid temperature, a Temperature instance will still be constructed, even if it does throw an error immediately afterwards. So you haven’t fulfilled the requirement to “construct a valid temperature from user input”.</p>

<p>And this isn’t just nitpicking; thing is, you have no idea what is in the constructor. So how do you know that giving it an invalid temperature won’t cause problems? Maybe it adds each new temperature to the database, so you stick invalid data in there. Maybe something else goes wrong. You don’t know, so you have to stick to exactly following the specification.</p>

<p>You get added points on the AP exam, not deducted. So they might not give you 1 or 2 certain points because you used array.length instead of array.size(). Dont worry about it too much tho As for semicolon dont stress about it, usually most graders wouldnt do that. </p>

<p>All points are different based on the things you have to code.</p>

<p>Thanks guys, and I agree with that argument amarkov :)</p>

<p>I should have gone back and looked at the whole line that “code to construct…” refers to – it does require a valid input, so I agree, choice I is wrong.</p>

<p>In general is the AP exam going to ask questions with code that breaks conventions and have it be correct since it technically works or are they going to abide by general convention? I’m not talking about tabbing here – they often mess with that in nested if-statements to throw the reader off. But like things where you are accessing static methods through instance objects and variable names that break convention etc. can those answers be correct?</p>