Nik Nguyen
Quiz by , created more than 1 year ago

Missed questions on the APCS test, with explanations.

818
0
0
Nik Nguyen
Created by Nik Nguyen over 8 years ago
Close

Missed Questions on Inheritance/Polymorphism Test

Question 1 of 16

1

Which of the following statements is true about both an interface and an abstract class?

Select one of the following:

  • Both can only have a default (no parameter) constructor.

  • Both can not have any instance variables.

  •  Both can have methods that are not implemented.

  • Both can have only public instance variables.

  •  Both can extend another abstract class.

Explanation

Question 2 of 16

1

Consider the following instance variable and method from a class.
 

ArrayList<String> nameList;
 

public String getItem(int loc)
{
 if(loc == nameList.size() – 1)
 
return nameList.get(loc);
 
else
 {
 String temp = getItem(loc+1);
 
String thisOne = nameList.get(loc);
 
if(temp.compareTo(thisOne) < 0)
 return temp;
 else
 return thisOne;
 }
}


Assume that nameList has been initialized as shown below.


 
nameList --> | Harry | Chris | Barbara | Peter | John |
 
 
Which of the following is returned by the call getitem(0)?

Select one of the following:

  • Harry

  • Chris

  • Barbara

  • Peter

  • John

Explanation

Question 3 of 16

1

Consider the incomplete method ChoiceOfThree.
 


public int choiceOfThree()
{
 // missing code
}
 


Method choiceOfThree is intended to return one of the values 1, 2, 3, each with probability 1/3. Consider the following replacements for//missing code.
 

I. return (int)(Math.random() * 3);

II. return (int)(Math.random() * 3) + 1;

III. if(Math.random() < 1.0/3.0)
  return 1;
 
else if (Math.random() < 2.0/3.0)
  
return 2;
 else
  
return 3;


Which if these replacements for //missing code will make choiceOfThree work as intended?

Select one of the following:

  • I

  • II

  • III

  • I and II

  • II and III

Explanation

Question 4 of 16

1

The following code segment is intended to sum compute the product of the first five odd integers.
 

product = 1;

for(int k = 0; k < 5; k++)
 
product = product * (2*k + 1);
 

Which of the following best describes the error, if any, in this code.

Select one of the following:

  • The segment works as intended

  • The segment computes the first of four odd integers.

  • The loop is incorrect. "k < 5" should be replaced with "k <= 5".

  • The loop is incorrect. "k = 0" should be replaced with "k = 1" and "k < 5" should be replaced with "k <= 5".

  • The variable product is incorrectly initialized. The first line should be "product = 1;".

Explanation

Question 5 of 16

1

Consider the following declarations.
 

public class Book
{

 private String myTitle;
 
private int myNumPages;
 
 
public int numPages()
 {
 return myNumPages;
 }
 // constructors and other methods not shown
}
 

ArrayList<Book> readingList;


 
Assume that readingList has had several books added to it. Consider the following code segments.
 
 
I. int sum = 0;
 for(Book bk: readingList)
  sum += bk.numPages();
 
 

II. int sum = 0;
 for(int k = 0; k < readingList.size(); k++)
  sum += readingList.get(k).numPages();
 

 
III. int sum = 0;
 int k = 0;
 

while(k < readingList.size())
 {
  sum += readingList.get(k).numPages();
  k++;
 }
 


Which of these code segments correctly calculates sum to be the total number of pages of books in readingList?

Select one of the following:

  • I

  • I & II

  • I & III

  • II & III

  • I, II, III

Explanation

Question 6 of 16

1

Consider the following partial declarations. 

public class Direction

{ 
private int degrees;
public Direction(int deg)
{degrees = deg;
System.out.println("direction "); }
                                         
  ...
                  }
                                                    
public class Location
                   
{
  private double xLoc, yLoc;            
 
public Location(double x, double y)   
 
{                                       
   
xLoc = x;                             
   
yLoc = y; 
                                           
    System.out.println("location ");      
  }                                         
  ...                                       

}                                                                                                                                                                

public class Car
{
 
private Direction dir;
 
private Location loc;
 
public Car(Direction d , Location l)
  {
    dir = d;
    loc = l;
    System.out.println("car");
  }

public class RaceCar extends Car
 {
 
private double speed;
 
public RaceCar(Direction d, Location l, double sp)
   {
   
super(d, l);
    speed = sp;
   
System.out.println("race-car");
   }
 
...

}

Consider the statement
 

RaceCar TSX = new RaceCar(new Direction(90), new Location(5, 8), 160);
 

What is printed when this statement is executed?

Select one of the following:

  • location direction race-car car

  • direction location race-car car

  • direction location car race-car

  • race-car car direction location

  • car race-car direction location 

Explanation

Question 7 of 16

1

A class Tach has a method rpm() that returns an integer. Consider the following method from the same class. 

// precondition: 0 < caution < danger

// postcondition: return "green" if rpm is less than caution;

// return "yellow" if rpm is greater than or equal to caution and
// less than danger

// return "red" if rpm is greater than or equals to danger

public string indicator(int caution, int danger)
{
 // missing code
}


The method indicator is intended to return "green" if rpm() returns a value less than caution, to return "yellow" if rpm()returns a value greater than or equal to caution and less than danger, to return "red" if rpm()returns a value greater or equal to danger.
Consider the following replacement for//missing code.
 
 

I.  if(rpm() < caution)
  return "green";
 else if(caution <= rpm() < danger)
  return "yellow";
 else
  return "red";
 
 
II. if(rpm() < caution)
  return "green";
 if(rpm() >= danger)
  return "red";
 return "yellow";
 
 
III. if(rpm() < caution)
  return "green";
 else if(rpm() >= danger)
  return "red";
 else
  return "yellow";
 


Which of these replacements for //missing code would make the method indicator work as intended?


Select one of the following:

  • I

  • II

  • III

  • I & II

  • II & III

Explanation

Question 8 of 16

1

Consider the following declaration
 
public class CD
{
 
private String[] songs;
 
 
public getSong(int k)
 { return songs[k]; }
 
 // constructor and other methods not shown
}
 

ArrayList<CD> CDCollection;

 
Assume that CDCollection has been initialized and several CD 's have been added to it. Which of the following can be used in a client program to reference the third song on the fifth CD in the collection?

Select one of the following:

  • CDCollection[4].songs[2]

  • CDCollection.get(4).songs[2]

  • CDCollection[4].getSong(2)

  • CDCollection.get(4).getSong(2)

  • getSong(2).get(4).CDCollection

Explanation

Question 9 of 16

1

"class Aggregate" is incorrect.  Choose the correct line so that this program prints
    
Granite: weight=25.0 value=4 numKind=7
      
public class Inherit
     {

            abstract class Stone

           {

                   protected float weight = 13;

                   protected int  value  = 4;

                   abstract public String toString( );

           }
            class Aggregate
           {

                   protected int numKind;
          
}
            class Granite extends Aggregate

           {
                   Granite( )

                   {
                           weight = 25; numKind = 7;
                   }

                   public String toString( )

                   {
                           return "Granite: weight="

                              + weight + " value="

                              + value  + " numKind="
 
                             + numKind;

                   }

           }
            Inherit( )
           {

                   Granite g = new Granite( );

                   System.out.println(g);

           }
            
public static void main(String[ ] args)
           {
                   new Inherit( );
           }
     }

Select one of the following:

  • abstract class Aggregate {

  • abstract class Aggregate extends Granite {

  • abstract class Aggregate extends Stone {

  • class Aggregate extends Stone {

  • none of the above

Explanation

Question 10 of 16

1

Assume the following partial declarations have been made, with default (no parameter) constructors for the classes.
 
public abstract class Player

public class ComputerPlayer extends Player;

public class SmartComputerPlayer extends ComputerPlayer;
 


Consider the following declarations.
 

I. ComputerPlayer playerA = new SmartComputerPlayer();

II. Player playerB = new SmartComputerPlayer();

III. Player playerC = new Player();
 


Which of these declarations will compile correctly?

Select one of the following:

  • I

  • II

  • III

  • I & II

  • II & III

Explanation

Question 11 of 16

1

private ArrayList<String> list;
 

public void change()
{
 String str = list.get(list.size() – 1);
 

 for(int k = list.size() - 1; k > 0; k--)

 list.set(k, list.get(k-1));
 
 
list.set(0, str);
}
 


Consider following replacements for the entire body of method change.
 


I. list.add(list.remove(0));
 


II. list.add(0, list.remove(list.size() – 1));
 


III. String str = list.get(0);
 for(int k = 1; k < list.size() – 1; k++)
  list.set(k, list.get(k+1));
 list.set(list.size() -1, str);


Which of these replacements would result in an equivalent method?

Select one of the following:

  • I

  • II

  • III

  • I & III

  • II & III

Explanation

Question 12 of 16

1

Consider the following data field and partially defined method.
 

private ArrayList<String> people;

public void removeDups(String target)
{ // code not shown }
 


The method removeDups is intended to remove all instances of target that occur in people except the first, leaving the first instance in the same relative position. Which of the following correctly implements the method removeDups?

Select one of the following:

  •   for(int k = 0; k < people.size(); k++)
 {
  if(people.get(k).equals(target))
  people.remove(k);
 }

  • int k = 0;
 while(k < people.size())
 {
  if(people.get(k).equals(target))
  people.remove(k);
  else
  k++;
 }


  • int k = 0;
 while(k < people.size() && !people.get(k).equals(target))
  k++;
 
 for(int j = k+1; j < people.size(); j++)
 {
  if(people.get(k).equals(target))
  people.remove(k);


  • int k = 0;
 while(k < people.size() && !people.get(k).equals(target))
  k++;
 
 while(k < people.size())
 {
  if(people.get(k).equals(target))
  people.remove(k);
  else
  k++;
 }


  • int k = 0;
 while(k < people.size() && !people.get(k).equals(target))
  k++;
 k++;
 
 while(k < people.size())
 {
  if(people.get(k).equals(target))
  people.remove(k);
  else
  k++;
 }


Explanation

Question 13 of 16

1

Assume that isHigh(), isBack(), and isLeft() are three boolean methods. Consider the following expression.
 
 
(isHigh() && isBack()) || (isHigh() && isLeft())
 


Which of the following is equivalent to the given expression?

Select one of the following:

  • !( (isHigh() || isBack()) && (isHigh() || isLeft() )

  • (!(isHigh() || !isBack()) && (!isHigh() || !isLeft())

  • isHigh() && isBack() && isLeft()

  • isHigh() && (isBack() || isLeft())

  • isBack() || isLeft()

Explanation

Question 14 of 16

1

public void sort(double[] list)
{
 
for(int start = 1; start < list.length; start++)
 
{
 double temp = list[start];

 int k = start;
 
while(k > 0 && list[k-1] > temp)
 
{
 list[k] = list[k-1];
 k++;
 }
 list[k] = temp;
 
}

}

Assume the array values is initialized as shown below.


values | 5.0 | 1.0 | 3.0 | 8.0 | 4.0 | 6.0 | 2.0 |



Which of the following best represents the array word after the fourth pass through the outer loop in the call sort(values)?

Select one of the following:

  • | 1.0 | 2.0 | 3.0 | 4.0 | 8.0 | 6.0 | 5.0 |

  • | 1.0 | 2.0 | 3.0 | 4.0 | 5.0 | 8.0 | 6.0 |

  • | 1.0 | 3.0 | 5.0 | 8.0 | 4.0 | 6.0 | 2.0 |

  • | 1.0 | 3.0 | 4.0 | 5.0 | 8.0 | 6.0 | 2.0 |

  • | 2.0 | 1.0 | 3.0 | 4.0 | 5.0 | 6.0 | 8.0 |

Explanation

Question 15 of 16

1

Consider the following description. A Kennel has animals. Animals come and go from the kennel, so the number there varies. A dog is an animal. A cat is an animal. Which of the following partial declarations is the best choice for representing the relationships among these things?


Select one of the following:

  • public class Dog
 public class Cat
 public class Kennel
 {
  private ArrayList<Dog> myDogs;
  private ArrayList<Cat> myCats;
  ...
 }


  •  public class Dog
 public class Cat
 public class Kennel
 {
  private Animal[] myAnimals;
  private int count;
  ...
 }

  •  public class Dog
 public class Cat
 public class Kennel
 {
  private ArrayList<Animal> myAnimals;
  ...
 }

  •  public abstract class Animal
 public class Dog extends Animal
 public class Cat extends Animal
 public class Kennel
 {
  private Animal[] myAnimals;
  private int count;
  ...
 }

  •  public abstract class Animal
 public class Dog extends Animal
 public class Cat extends Animal
 public class Kennel
 {
  private ArrayList<Animal> myAnimals;
  ...
 }

Explanation

Question 16 of 16

1

A certain application needs to store three types of object. Each type has a common method, rating(), that returns a double between 0.0 and 1.0. Otherwise the different types of object have different characteristics and behavior. The application needs to store many objects of these three types so that they can easily be accessed in order of their ratings. Assume that the three types will be defined by three classes: TypeA, TypeB, TypeC. Which of the following is the best design for this application?


Select one of the following:

  • Store the objects in three ArrayLists: ArrayList<TypeA>, ArrayList<TypeB>, ArrayList<TypeC>, each maintained in order by rating when items are stored.

  • Declare a class TypeCollection that has three unordered ArrayLists as instance variables: ArrayList<TypeA>, ArrayList<TypeB>, ArrayList<TypeC>.

  • Declare a class TypeCollection that has three ArrayLists as instance variables: ArrayList<TypeA>, ArrayList<TypeB>, ArrayList<TypeC>, each maintained in order by rating when items are stored.

  • Store the objects in a single ArrayList (of Object) ordered by rating.

  • Declare an Interface Rateable that specifies one method, rating(), that returns a double. Define each of these classes TypeA, TypeB, TypeC to implement this interface. Store the objects in an ArrayList<Rateable> ordered by rating. 

Explanation