Missed Questions on Inheritance/Polymorphism Test

Beschreibung

Missed questions on the APCS test, with explanations.
Nik Nguyen
Quiz von Nik Nguyen, aktualisiert more than 1 year ago
Nik Nguyen
Erstellt von Nik Nguyen vor mehr als 8 Jahre
818
0

Zusammenfassung der Ressource

Frage 1

Frage
Which of the following statements is true about both an interface and an abstract class?
Antworten
  • 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.

Frage 2

Frage
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)?
Antworten
  • Harry
  • Chris
  • Barbara
  • Peter
  • John

Frage 3

Frage
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?
Antworten
  • I
  • II
  • III
  • I and II
  • II and III

Frage 4

Frage
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.
Antworten
  • 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;".

Frage 5

Frage
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?
Antworten
  • I
  • I & II
  • I & III
  • II & III
  • I, II, III

Frage 6

Frage
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?
Antworten
  • 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 

Frage 7

Frage
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?

Antworten
  • I
  • II
  • III
  • I & II
  • II & III

Frage 8

Frage
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?
Antworten
  • CDCollection[4].songs[2]
  • CDCollection.get(4).songs[2]
  • CDCollection[4].getSong(2)
  • CDCollection.get(4).getSong(2)
  • getSong(2).get(4).CDCollection

Frage 9

Frage
"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( );
           }
     }
Antworten
  • abstract class Aggregate {
  • abstract class Aggregate extends Granite {
  • abstract class Aggregate extends Stone {
  • class Aggregate extends Stone {
  • none of the above

Frage 10

Frage
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?
Antworten
  • I
  • II
  • III
  • I & II
  • II & III

Frage 11

Frage
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?
Antworten
  • I
  • II
  • III
  • I & III
  • II & III

Frage 12

Frage
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?
Antworten
  •   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++;
 }


Frage 13

Frage
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?
Antworten
  • !( (isHigh() || isBack()) && (isHigh() || isLeft() )
  • (!(isHigh() || !isBack()) && (!isHigh() || !isLeft())
  • isHigh() && isBack() && isLeft()
  • isHigh() && (isBack() || isLeft())
  • isBack() || isLeft()

Frage 14

Frage
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)?
Antworten
  • | 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 |

Frage 15

Frage
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?

Antworten
  • 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;
  ...
 }

Frage 16

Frage
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?

Antworten
  • 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. 
Zusammenfassung anzeigen Zusammenfassung ausblenden

ähnlicher Inhalt

DNA Basics
Sarah Juliette B
Translations and transformations of functions
Christine Laurich
Abstract Classes & Interfaces & Polymorphism
Nik Nguyen
Waves
kate.siena
Computer science quiz
Ryan Barton
AQA Biology B1 Questions
Bella Statham
AQA Biology B2 Questions
Bella Statham
AQA Physics P1 Quiz
Bella Statham
GCSE AQA Biology 1 Quiz
Lilac Potato
AQA GCSE Product Design Questions
Bella Statham
General Knowledge Quiz
Andrea Leyden