Skip to main content
Logo image

Section 3.14 Unit 3 Test

The following questions are similar to what you might see on the AP CSA exam. You may only take this test once while logged in. There are no time limits, but it will keep track of how much time you take. Click on the finish button after you have answered all the questions, and the number correct and feedback on the answers will be displayed.
We estimate that a score of about 50% on this test would correspond to the passing grade of 3 on the AP exam, a score of 65% to a 4, and a score of 80% and above to a 5 on the AP exam. These are just estimates and may not correspond to individual scores.

Exercises Exercises

    1.

    Consider the following Cat class that has an age attribute of type int. The getAge method is intended to allow methods in other classes to access a Cat object’s age value; however, it does not work as intended. Which of the following best explains why the getAge method does NOT work as intended?
    public class Cat
    {
        private int age;
    
        public Cat(int a)
        {
            age = a;
        }
    
        public int getAge()
        {
            return a;
        }
    }
    
    • The ``getAge()`` method should be declared as private.
    • The method should be public so it can be accessed outside of the class.
    • The return type of the ``getAge()`` method should be void.
    • The method’s return type should be int.
    • The ``getAge()`` method should have at least one parameter.
    • The getAge method should not take any parameters.
    • The variable ``age`` is not declared inside the ``getAge()`` method.
    • This is an instance variable and should be declared outside of the method.
    • The instance variable ``age`` should be returned instead of a, which is local to the constructor.
    • Correct! The accessor method getAge should return the instance variable age.

    2.

    Which of the following statements are TRUE about local variables?
    1. Local variables can be declared in the body of constructors and methods.
    2. Local variables may only be used within the constructor or method and cannot be declared to be public or private.
    3. When there is a local variable with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable.
    • I only
    • It’s true that the local variables can be declared in the body of constructors and methods, but there are other options that are also true about local variables.
    • I and II only
    • Both I and II are true but III is also true regarding local variables.
    • I and III only
    • Both I and III are true but II is also true regarding local variables.
    • I, II, and III
    • Correct! All of the above are true.
    • II and III only
    • Both of these are true but I is also true.

    3.

    Which of the following statements are TRUE about static methods?
    1. Static methods and variables include the keyword static before their name in the header or declaration and can be public or private.
    2. Static methods can access or change the values of instance variables.
    3. Static methods are associated with the class, not objects of the class.
    • I and II only
    • Static methods cannot acccess instance variables. They can only access static variables.
    • I, II, and III
    • Static methods cannot acccess instance variables. They can only access static variables.
    • I and III only
    • Correct! I and III are true, but static methods cannot acccess instance variables. They can only access static variables.
    • I only
    • I is true, but there is another option that is true too.
    • III only
    • III is true, but there is another option that is true too.

    4.

    Consider the following declaration for a class that will be used to represent points in time. Which of these options correctly implement addMinutes()?
    public class Timer
    {
       private int hours; // number of hours
       private int minutes; // 0 <= minutes < 60
    
       void addHours(int addition)
       {
          hours = hours + addition;
       }
    
       /** addMinutes adds the given argument to the time stored in hours and minutes.
       The argument additionMinutes is between 0 and 119.  **/
       void addMinutes(int additionMinutes)
       {
          // implementation not shown
       }
    
       // ... other methods not shown
    }
    
    
    //Proposed Implementations:
    I.   public void addMinutes(int additionMinutes)
         {
             minutes = minutes + additionMinutes;
         }
    II.  public void addMinutes(int additionMinutes)
         {
             minutes += additionMinutes;
             if (minutes >= 60)
             {
                hours += minutes / 60;
                minutes = minutes % 60;
             }
         }
    III. public void addMinutes(int additionMinutes)
         {
             minutes += additionMinutes;
             while (minutes >= 60)
             {
                hours++;
                minutes -= 60;
             }
         }
    IV.  public void addMinutes(int additionMinutes)
         {
             if (additionMinutes + minutes >= 60)
             {
                minutes = additionMinutes + minutes - 60;
                hours += 1;
             }
         }
    
    • I only
    • This implementation of ``addMinutes`` does not account for values of additionMinutes that push the minute count above 60.
    • II only
    • Implementation II works, but implementation III also works.
    • IV only
    • Implementation IV does not work for situations where additionMinutes + minutes does not go above 60.
    • II and III
    • Correct!
    • I, II, and III
    • Implementations II and III are correct, but implementation I is not. Implementation I does not account for values of additionMinutes that push the minute account above 60.

    5.

    What does the method mystery do?
    public void mystery(String tester)
    {
       for (int i = tester.length() - 1; i >= 0; i--)
       {
          System.out.print(tester.substring(i,i+1));
       }
       System.out.println("");
    }
    
    • Prints the string in reverse order
    • Correct! This method prints the reversed string.
    • Deletes the second half of the string
    • Incorrect, this method prints the parameter reversed.
    • Prints string normally
    • Incorrect, this method prints the parameter reversed.
    • Compile-time error occurs
    • Incorrect, this method prints the parameter reversed.
    • Prints alternating characters from beginning and end of the string.
    • Incorrect, this method prints the parameter reversed.

    6.

    Which of the following code segments correctly creates an instance of a new Party object?
    public class Party
    {
    
       private int numInvited;
       private boolean partyCancelled;
    
       public Party()
       {
          numInvited = 1;
          partyCancelled = false;
       }
    
       public Party(int invites)
       {
          numInvited = invites;
          partyCancelled = false;
       }
    }
    
    I.   Party myParty;
    II.  int classSize = 20;
         Party ourParty = new Party(classSize);
    III. int numOfFriends = 6;
         Party yourParty = new Party(numOfFriends + 3.0);
    
    • I only
    • I contains incorrect syntax. Try again!
    • I and II
    • I contains incorrect syntax. Try again!
    • II only
    • Correct! II is the only correct option.
    • II and III
    • III is incorrect due to a problem with the constructor argument. Try again!
    • I, II, and III
    • Two of these options are incorrect. Take a closer look at the syntax of I and parameters of III.

    7.

    Consider the following class, which uses the instance variable dollars to represent the money in a wallet in dollars.
    public class Wallet
    {
          private double dollars;
    
          public double putMoneyInWallet(int amount)
          {
             /* missing code */
          }
    }
    
    The putMoneyInWallet method is intended to increase the dollars in the wallet by the parameter amount and then return the updated dollars in the wallet. Which of the following code segments should replace missing code so that the putMoneyInWallet method will work as intended?
    • amount += dollars;
      return dollars;
      
    • dollars should be incremented by amount.
    • dollars = amount;
      return amount;
      
    • dollars should be incremented by amount.
    • dollars += amount;
      return dollars;
      
    • Correct.
    • dollars = dollars + amount;
      return amount;
      
    • amount is returned instead of dollars.
    • amount = dollars + amount;
      return dollars;
      
    • dollars should be incremented by amount.

    8.

    Consider the Liquid class below.
    public class Liquid
    {
          private int currentTemp;
          private int boilingPoint;
    
          public Liquid(int ct, int bp)
          {
             currentTemp = ct;
             boilingPoint = bp;
          }
    
          public boolean isBoiling(int amount)
          {
             /* missing code */
          }
    }
    
    The isBoiling method is intended to return true if increasing the currentTemp by the parameter amount is greater than or equal to the boilingPoint, or otherwise return false. Which of the following code segments can replace missing code to ensure that the isBoiling method works as intended?
    I.   if (currentTemp + amount < boilingPoint)
          {
             return false;
          }
          else
          {
             return true;
          }
    II.  if (amount > currentTemp)
          {
             return false;
          }
          else
          {
             return currentTemp;
          }
    III. if (amount + currentTemp >= boilingPoint)
          {
             return true;
          }
          else
          {
             return false;
          }
    
    • I only
    • I would work but it is not the only code that would work.
    • II only
    • II does not check against the boilingPoint and does not return only boolean values.
    • III only
    • III would work but it is not the only code that would work.
    • I and III only.
    • Correct!
    • I, II, III
    • II does not check against the boilingPoint and does not return only boolean values.

    9.

    Consider the following class definition.
    public class Liquid
    {
        private int currentTemp;
        private int boilingPoint;
    
        public Liquid(int ct, int bp)
        {
            currentTemp = ct;
            boilingPoint = bp;
        }
    
        public void changeTemp(int newTemp)
        {
            currentTemp = newTemp;
        }
    
        public void increaseTemp(int howMuch)
        {
            currentTemp = newTemp + howMuch;
        }
    }
    
    Which of the following best explains why the class will not compile?
    • The class is missing an accessor method.
    • The class does not necessarily need an accessor method.
    • The instance variables currentTemp and boilingPoint should be public instead of private.
    • Instance variables are usually private.
    • The Liquid constructor needs a return type.
    • Constructors do not have return types.
    • The Liquid class is missing a constructor.
    • The class includes a constructor.
    • The variable newTemp is not defined in the increaseTemp method.
    • Correct! newTemp is defined in a different method. The instance variable currentTemp should be used instead.

    10.

    Consider the following class definition for Party. The following code segment appears in a method in a class other than Party. The code segment is intended to print the value 30, but does not print the correct value because of an error in the Party class. Which of the following best explains why the correct value isn’t printed?
    Party p = new Party(20, 15);
    p.orderMoreFood(20);
    p.eatFood(5);
    System.out.println(p.getBoxesOfFood());
    
    public class Party
    {
        private int boxesOfFood;
        private int numOfPeople;
    
        public Party(int people, int foodBoxes)
        {
            numOfPeople = people;
            boxesOfFood = foodBoxes;
        }
    
        public void orderMoreFood(int additionalFoodBoxes)
        {
            int updatedAmountOfFood = boxesOfFood + additionalFoodBoxes;
            boxesOfFood = updatedAmountOfFood;
        }
    
        public int getNumOfPeople() {
            return numOfPeople;
        }
    
        public int getBoxesOfFood() {
            return boxesOfFood;
        }
    
        public void eatFood(int eatenBoxes)
        {
            boxesOfFood = updatedAmountOfFood - eatenBoxes;
        }
    }
    
    • The private variables boxesOfFood and numOfPeople are not properly initialized.
    • The private variables boxesOfFood and numOfPeople are initialized by the constructor.
    • The private variables boxesOfFood and numOfPeople should have been declared public.
    • Instance variables are usually private.
    • The public method getBoxesOfFood should have been declared private.
    • Methods are usually public.
    • The variable updatedAmountOfFood in the eatFood method is not declared in this method.
    • The variable updatedAmountOfFood in the eatFood method is not declared in this method. It could be replaced by the boxesOfFood instance variable.
    • The variables boxesOfFood and numOfPeople in the updatedAmountOfFood method are local variables.
    • The variables boxesOfFood and numOfPeople are instance variables.

    11.

    Consider the following class definitions.
    public class Party
    {
        private String partyHost;
        private int monthOfParty;
        private int partyStartTime;
    
        public Party(String h, int month, int startTime)
        {
            partyHost = h;
            monthOfParty = month;
            partyStartTime = startTime;
        }
    
        public int getMonth()
        {
            return monthOfParty;
        }
    
        public int getStartTime()
        {
            return partyStartTime;
        }
    
        public String getHost()
        {
            return partyHost;
        }
    
        public void addToOptions(PartyOptions o)
        {
            o.addParty(this);
        }
    }
    
    public class PartyOptions
    {
        private int onlyThisMonth;
    
        public PartyOptions(int month)
        {
            onlyThisMonth = month;
        }
    
        /* A Party should only be added to this PartyOption if the party’s month matches onlyThisMonth */
        public void addParty(Party p)
        {
            if (p.getMonth() == onlyThisMonth)
            {
                System.out.print("Party by " + p.getHost() + " accepted; ");
            }
            else
            {
                System.out.print("Party by " + p.getHost() + " rejected; ");
            }
        }
    }
    
    Consider the following code segment, which appears in a class other than Party or PartyOptions.
    Party p1 = new Party("Kerry", 10, 7);
    Party p2 = new Party("Jules", 9, 6);
    
    PartyOptions options = new PartyOptions(10);
    p1.addToOptions(options);
    p2.addToOptions(options);
    
    • Party by Kerry rejected; Party by Jules rejected;
    • Kerry’s party should be accepted because it is in the 10th month.
    • Party by Kerry rejected; Party by Jules accepted;
    • Kerry’s party should be accepted because it is in the 10th month. Jules’ party should be rejected because it is not in the 10th month.
    • Party by Kerry accepted; Party by Jules rejected;
    • Kerry’s party is accepted because it is in the 10th month, and Jules’ party is not.
    • Party by Kerry accepted; Party by Jules accepted;
    • Jules’ party should be rejected because it is not in the 10th month.
You have attempted of activities on this page.