Wednesday, January 22, 2014

APCS chap 4 test

AP Computer Science                   Test 4 again 

Multiple Choice Questions

 

For questions 1-3, use the Student class definition

 

1)    Which of the following could be used to instantiate a new Student s1?

a)  Student s1 = new Student( );

b)  s1 = new Student( );

c)   Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33);

d)  new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33);

e)   new Student(s1);

 

2)    Assume that another method has been defined that will compute and return the student’s class rank (Freshman, Sophomore, etc).  It is defined as:

public String getClassRank( )

     Given that s1 is a student, which of the following would properly be used to get s1’s class rank?

a)  s1 = getClassRank( );

b)  s1.toString( );

c)   s1.getHours( );

d)  s1.getClassRank( );

e)   getClassRank(s1);


 

 

3)    Another method that might be desired is one that updates the Student’s number of credit hours.  This method will receive a number of credit hours and add these to the Student’s current hours.  Which of the following methods would accomplish this?

a)  public int updateHours( )

{

         return hours;

}

 

b)  public void updateHours( )

    {

             hours++;

    }

 

c)   public updateHours(int moreHours)

    {

             hours += moreHours;

    }

 

d)  public void updateHours(int moreHours)

    {

             hours += moreHours;

    }

 

e)   public int updateHours(int moreHours)

    {

             return hours + moreHours;

    }

 

The Coin class, as defined in Chapter 4, consists of a constructor, and methods flip, isHeads and toString.  The method isHeads returns true if the last flip was a Heads, and false if the last flip was a Tails.  The toString method returns a String equal to “Heads” or “Tails” depending on the result of the last flip.  Using this information, answer questions 4-5.


 

 

4)    A set of code has already instantiated c to be a Coin and has input a String guess, from the user asking whether the user guesses that a coin flip will result in “Heads” or “Tails”.  Which of the following sets of code will perform the coin flip and see if the user’s guess was right or wrong?

a)  c.flip( );

if(c.isHeads( ).equals(guess)) System.out.println("User is correct");

 

b)  if(c.flip( ).equals(guess)) System.out.println("User is correct");

 

c)   if(c.isHeads( ).equals(guess)) System.out.println("User is correct");

 

d)  c.flip( );

if(c.toString( ).equals(guess)) System.out.println("User is correct");

 

e)   c.flip( ).toString( );

    if(c.equals(guess)) System.out.println("User is correct");

 

 

5)    What does the following code compute?

      int num = 0;

      for(int j = 0; j < 1000; j++)

      {

          c.flip( );

          if(c.isHeads()) num++;

      }

      double value = (double) num / 1000;

a) the number of Heads flipped out of 1000 flips

b) the number of Heads flipped in a row out of 1000 flips

c)  the percentage of heads flipped out of 1000 flips

d)  the percentage of times neither Heads nor Tails were flipped out of 1000 flips

e)  nothing at all

 

6)    In the Rational class, defined in chapter 4, the methods reduce and gcd are declared to be private.  Why?

a)  Because they will never be used

b)  Because they will only be called from methods inside of Rational

c)   Because they will only be called from the constructor of Rational

d)  Because they do not use any of Rational’s instance data

e)   Because it is a typo and they should be declared as public

 

Use the following information to answer questions 7 - 8.  The Die class from chapter 4 has two constructors defined as follows. Assume MIN_FACES is an int equal to 4.

public Die( )                                      public Die(int faces)

{                                              {

          numFaces = 6;                                   if(faces < MIN_FACES) numFaces = 6;

          faceValue = 1;                                   else numFaces = faces;

}                                                        faceValue = 1;

                                                }

 

7)    The instruction Die d = new Die(10); results in

a)  The Die d having numFaces = 6 and faceValue = 1

b)  The Die d having numFaces = 10 and faceValue = 1

c)   The Die d having numFaces = 10 and faceValue = 10

d)  The Die d having numFaces = 6 and faceValue = 10

e)   A syntax error

 

8)    The instruction Die d = new Die(10, 0); results in

a)  The Die d having numFaces = 6 and faceValue = 1

b)  The Die d having numFaces = 10 and faceValue = 1

c)   The Die d having numFaces = 10 and faceValue = 10

d)  The Die d having numFaces = 6 and faceValue = 10

e)   A syntax error

 

For questions 9-10, use the Swapper class definition:

 

9)    If the instruction Swapper s = new Swapper(0, "hello", 0); is executed followed by s.toString( ); what value is returned from s.toString( )?

a)  "hello"

b)  "hello00"

c)   "00"

d)  "0"

e)   0

 

 

10)           If we have Swapper r = new Swapper (5, "no", 10); then r.swap( ); returns which of the following?

a)  nothing

b)  "no"

c)   "no510"

d)  "510"

e)   "15"

 

11)           Consider a method defined with the header:  public void foo(int a, int b). 
 Which of the following method calls is legal?

a)  foo(0, 0.1);

b)  foo(0 / 1, 2 * 3);

c)   foo(0);

d)  foo( );

e)   foo(1 + 2, 3 * 0.1);

 

12)           Consider a method defined with the header:   public void doublefoo(double x).
 Which of the following method calls is legal?

a)  doublefoo(0);

b)  doublefoo(0.555);

c)   doublefoo(0.1 + 0.2);

d)  doublefoo(0.1, 0.2);

e)   all of the above are legal except for d

 

Question 13 refers to the following method:

 

          public int dogYears (int age)

          {

                   if (age >= 0)

                             return age * 7;

                   else

                             return 0;

          }

 

13)           What would be an appropriate precondition for the method?

a)  // Precondition: tells how old your dog is

b)  // Precondition: age is someone’s age

c)   // Precondition: age >= 0

d)  // Precondition: age is an int

e)   // Precondition: returns age * 7

 

14)           What is a mutator method?

a)  A method that modifies a value.

b)  A method that provides read-only access to a value.

c)   A method that has the same name, but different parameters, as another method.

d)  A method that is called when an object is first created.

e)   A method that does not return a value.

 

15)           The behavior of an object is defined by the object’s

a)     instance data

b)    constructor

c)     visibility modifiers

d)    methods

e)     all of the above

 


 

 

True/False Questions:

 

 

16)  Every class definition must include a constructor.

 

17)  While multiple objects of the same class can exist, there is only one version of the class.

 

18)  A class may contain methods but not variable declarations.

 

19)  A constructor is a method that gets called automatically whenever an object is created, for example with the new operator.

 

20)   A constructor must have the same name as its class.

 

21)  A constructor must always return an int.

 

22)  A class’s instance data are the variables declared in the main method.

 

23)  The methods in a class should always be made public so that those outside the class can use them

 

24)  The body of a method may be empty.

 

25)  The return statement must be followed a single variable that contains the value to be returned.


 

 

Free-form Questions:

 

For question 26, write the requested portion of a class called Employee. 

 

26)  An Employee will have a name, social security number, position, and hourly wages.  Define the instance data for this class.

 

 

 

 

 

 

 

 

 

 

 

27)  Assume a class has instance data String x, y, z which were initialized in the class’ constructor.  Write a method that will determine if any of the three Strings store the same value as any of the other Strings.  Return true if this is so, false otherwise.


 

import java.text.DecimalFormat;

public class Student

{

          private String name;

          private String major;

          private double gpa;

          private int hours;

 

          public Student(String newName, String newMajor, double newGPA, int newHours)

          {

                   name = newName;

                   major = newMajor;

                   gpa = newGPA;

                   hours = newHours;

          }

 

          public String toString( )

          {

                   DecimalFormat df = new DecimalFormat("xxxx");             // xxxx needs to be replaced

                   return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours

          }

}

 


 

public class Swapper

          {

                             private int x;

                   private String y;

                   public int z;

                  

                   public Swapper(int a, String b, int c)

                   {

                             x = a;

                             y = b;

                             z = c;

                   }

 

                   public String swap( )

                   {

                             int temp = x;

                             x = z;

                             z = temp;

                             return y;

                   }

 

                   public String toString( )

                   {

                             if (x < z) return y;

                             else return "" + x + z;

                   }

          }