0% found this document useful (0 votes)
17 views

Unit_5-MCQ1

Uploaded by

ret39301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Unit_5-MCQ1

Uploaded by

ret39301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

AP COMPUTER SCIENCE A Test Booklet

Unit 5 MCQ 1

1. A rectangular box fits inside another rectangular box if and only if the height, width, and depth of the smaller box
are each less than the corresponding values of the larger box. Consider the following three interface declarations
that are intended to represent information necessary for rectangular boxes.

Which of the interfaces, if correctly implemented by a Box class, would be sufficient functionality for a user of the
Box class to determine if one Box can fit inside another?
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III

AP Computer Science A Page 1 of 25


Test Booklet

Unit 5 MCQ 1

2. Consider the following class declarations.

Which of the following replacements for /* missing code */ will correctly implement the Circle
constructor?

Page 2 of 25 AP Computer Science A


Test Booklet

Unit 5 MCQ 1

(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III

AP Computer Science A Page 3 of 25


Test Booklet

Unit 5 MCQ 1

3. Consider the following class that stores information about temperature readings on various dates.

public class TemperatureReading implements Comparable

private double temperature; private int month, day, year;

public int compareTo(Object obj)

TemperatureReading other = (TemperatureReading) obj;

/* missing code */

// There may be instance variables, constructors, and methods that are not shown.

Consider the following code segments that are potential replacements for /* missing code */.

I. Double d1 = new Double(temperature); Double d2 = new Double(other.temperature);

return d1.compareTo(d2);

II. if (temperature < other.temperature)

return -1;

else if (temperature == other.temperature)

Page 4 of 25 AP Computer Science A


Test Booklet

Unit 5 MCQ 1

return 0;

else

return 1;

III. return (int) (temperature - other.temperature);

Which of the code segments could be used to replace /* missing code */ so that compareTo can be used to order
TemperatureReading objects by increasing temperature value?
(A) II only
(B) I and II only
(C) I and III only
(D) II and III only
(E) I, II, and III

AP Computer Science A Page 5 of 25


Test Booklet

Unit 5 MCQ 1

4. Consider the following two methods, which appear within a single class.

What is printed as a result of the call start() ?


(A) 0 0 0 0 0 0 black
(B) 0 0 0 0 0 6 blackboard
(C) 1 2 3 4 5 6 black
(D) 1 2 3 4 5 0 black
(E) 1 2 3 4 5 6 blackboard

Page 6 of 25 AP Computer Science A


Test Booklet

Unit 5 MCQ 1

5. Consider the following class definition.

public class FishTank


{
private double numGallons;
private boolean saltWater;
public FishTank(double gals, boolean sw)
{
numGallons = gals;
saltWater = sw;
}
public double getNumGallons()
{
return numGallons;
}
public boolean isSaltWater()
{
if (saltWater)
{
return "Salt Water";
}
else
{
return "Fresh Water";
}
}
}

Which of the following best explains the reason why the class will not compile?
(A) The variable numGallons is not declared in the getNumGallons method.
(B) The variable saltWater is not declared in the isSaltWater method.
(C) The isSaltWater method does not return the value of an instance variable.
(D) The value returned by the getNumGallons method is not compatible with the return type of the method.
(E) The value returned by the isSaltWater method is not compatible with the return type of the method.

AP Computer Science A Page 7 of 25


Test Booklet

Unit 5 MCQ 1

6. Consider the following class definition. The class does not compile.

public class Player


{
private double score;
public getScore()
{
return score;
}
// Constructor not shown
}

The accessor method getScore is intended to return the score of a Player object. Which of the following
best explains why the class does not compile?
(A) The getScore method should be declared as private.
(B) The getScore method requires a parameter.
(C) The return type of the getScore method needs to be defined as double.
(D) The return type of the getScore method needs to be defined as String.
(E) The return type of the getScore method needs to be defined as void.

Page 8 of 25 AP Computer Science A


Test Booklet

Unit 5 MCQ 1

7. Consider the following class definition.

public class SomeClass


{
private int x = 0;
private static int y = 0;
public SomeClass(int pX)
{
x = pX;
y++;
}
public void incrementY()
{ y++; }
public void incrementY(int inc)
{ y += inc; }
public int getY()
{ return y; }
}

The following code segment appears in a class other than SomeClass.

SomeClass first = new SomeClass(10);


SomeClass second = new SomeClass(20);
SomeClass third = new SomeClass(30);
first.incrementY();
second.incrementY(10);
System.out.println(third.getY());

What is printed as a result of executing the code segment if the code segment is the first use of a SomeClass
object?
(A) 0
(B) 1
(C) 11
(D) 14
(E) 30

AP Computer Science A Page 9 of 25


Test Booklet

Unit 5 MCQ 1

8. Consider the following while loop. Assume that the int variable k has been properly declared and
initialized.

while (k < 0)
{
System.out.print("*");
k++;
}

Which of the following ranges of initial values for k will guarantee that at least one "*" character is printed?

I. k < 0
II. k = 0
III. k > 0
(A) I only
(B) III only
(C) I and II only
(D) II and III only
(E) I, II, and III

Page 10 of 25 AP Computer Science A


Test Booklet

Unit 5 MCQ 1

9. Consider the following class declaration.

public class IntCell

private int myStoredValue;

// constructor not shown

public int getValue()

return myStoredValue;

public String toString ()

return "" + myStoredValue;

Assume that the following declaration appears in a client class.

IntCell m = new IntCell();

Which of these statements can be used in the client class?

I. System.out.println(m.getValue());

II. System.out.println(m.myStoredValue);

III. System.out.println(m);
(A) I only
(B) II only
(C) III only
(D) I and II
(E) I and III

AP Computer Science A Page 11 of 25


Test Booklet

Unit 5 MCQ 1

10. Consider the following class definitions.

public class Item


{
private int ID;
public Item (int id)
{
ID = id;
}
public int getID()
{
return ID;
}
public void addToCollection (ItemCollection c)
{
c.addItem(this);
}
}
public class ItemCollection
{
private int last_ID;
public void addItem(Item i)
{
if (i.getID() == last_ID)
{
System.out.print("ID " + i.getID() + " rejected; ");
}
else
{
last_ID = i.getID();
System.out.print("ID " + i.getID() + " accepted; ");
}
}
// Constructor not shown.
}

Consider the following code segment, which appears in a class other than Item or ItemCollection.

Item i = new Item(23);


Item j = new Item(32);
ItemCollection c = new ItemCollection();
i.addToCollection(c);
j.addToCollection(c);
j.addToCollection(c);
i.addToCollection(c);

What is printed as a result of executing the code segment?

Page 12 of 25 AP Computer Science A


Test Booklet

Unit 5 MCQ 1

(A) ID 23 accepted; ID 32 accepted; ID 32 rejected; ID 23 accepted;


(B) ID 23 accepted; ID 32 accepted; ID 32 rejected; ID 23 rejected;
(C) ID 23 accepted; ID 32 rejected; ID 32 rejected; ID 23 accepted;
(D) ID 23 accepted; ID 32 rejected; ID 32 rejected; ID 23 rejected;
(E) ID 23 accepted; ID 32 rejected; ID 32 accepted; ID 23 rejected;

AP Computer Science A Page 13 of 25


Test Booklet

Unit 5 MCQ 1

11. The question refer to the following declarations.

public class Point

private double myX;

private double myyY;

// postcondition: this Point has coordinates (0,0)

public Point ()

{ /* implementation not shown */ }

// postcondition: this Point has coordinates (x,y)

public Point(double x, double y)

{ /* implementation not shown */ }

// other methods not shown

public class Circle

private Point myCenter;

private double myRadius;

// postcondition: this Circle has center at (0, 0) and radius 0.0

public Circle()

{ /* implementation not shown */ }

// postcondition: this Circle has the given center and radius

public Circle(Point center, double radius)

{ /* implementation not shown */ }

// other methods not shown

Page 14 of 25 AP Computer Science A


Test Booklet

Unit 5 MCQ 1

In a client program which of the following correctly declares and initializes Circle circ with center at (29.5, 33.0)
and radius 10.0 ?
(A) Circle circ = new Circle(29.5, 33.0, 10.0);
(B) Circle circ = new Circle((29.5, 33.0), 10.0);
(C) Circle circ = new Circle(new Point (29.5, 33.0), 10.0);
Circle circ = new Circle();
(D) circ.myCenter = new Point(29.5, 33.0);
circ.myRadius = 10.0;
Circle circ = new Circle();
circ.myCenter = new Point();
(E) circ.myCenter.myX = 29.5;
circ.myCenter.myY = 33.0;
cire.myRadius = 10.0;

AP Computer Science A Page 15 of 25


Test Booklet

Unit 5 MCQ 1

12. The question refer to the following declarations.

public class Point

private double myX;

private double myyY;

// postcondition: this Point has coordinates (0,0)

public Point ()

{ /* implementation not shown */ }

// postcondition: this Point has coordinates (x,y)

public Point(double x, double y)

{ /* implementation not shown */ }

// other methods not shown

public class Circle

private Point myCenter;

private double myRadius;

// postcondition: this Circle has center at (0, 0) and radius 0.0

public Circle()

{ /* implementation not shown */ }

// postcondition: this Circle has the given center and radius

public Circle(Point center, double radius)

{ /* implementation not shown */ }

// other methods not shown

Page 16 of 25 AP Computer Science A


Test Booklet

Unit 5 MCQ 1

Which of the following would be the best specification for a Circle method isInside that determines whether a
Point lies inside this Circle?
(A) public boolean isInside()
(B) public void isInside(boolean found)
(C) public boolean isInside(Point p)
(D) public void isInside(Point p, boolean found)
(E) public boolean isInside(Point p, Point center, double radius)

13. The following method is intended to return a string containing the character at position n in the string str. For
example, getChar("ABCDE", 2) should return "C".

/* missing precondition */
public String getChar(String str, int n)
{
return str.substring(n, n + 1);
}

Which of the following is the most appropriate precondition for the method so that it does not throw an exception?
(A) /* Precondition: 0 < n < str.length() - 1 */
(B) /* Precondition: 0 <= n <= str.length() - 1 */
(C) /* Precondition: 0 <= n <= str.length() */
(D) /* Precondition: n > str.length() */
(E) /* Precondition: n >= str.length() */

14. In the Toy class below, the raisePrice method is intended to increase the value of the instance variable
price by the value of the parameter surcharge. The method does not work as intended.

public class Toy


{
private String name;
private double price;
public Toy(String n, double p)
{
name = n;
price = p;
}
public void raisePrice(double surcharge) // Line 12
{
return price + surcharge; // Line 14
}

Which of the following changes should be made so that the class definition compiles without error and the method
raisePrice works as intended?

AP Computer Science A Page 17 of 25


Test Booklet

Unit 5 MCQ 1

(A) Replace line 14 with surcharge += price;.


(B) Replace line 14 with price += surcharge;.
(C) Replace line 14 with return price += surcharge;.
(D) Replace line 12 with public raisePrice (double surcharge).
(E) Replace line 12 with public double raisePrice (double surcharge).

Directions: Select the choice that best fits each statement. The following question(s) refer to the following information.

Consider the following partial class declaration.

15. Which of the following changes to SomeClass will allow other classes to access but not modify the value of myC ?
(A) Make myC public.

(B)

(C)

(D)

(E)

Page 18 of 25 AP Computer Science A


Test Booklet

Unit 5 MCQ 1

16. Consider the following class definition.

public class Something


{
private static int count = 0;
public Something()
{
count += 5;
}
public static void increment()
{
count++;
}
}

The following code segment appears in a method in a class other than Something.

Something s = new Something();


Something.increment();

Which of the following best describes the behavior of the code segment?
The code segment does not compile because the increment method should be called on an object of the
(A)
class Something, not on the class itself.
The code segment creates a Something object s. The class Something’s static variable count is
(B)
initially 0, then increased by 1.
The code segment creates a Something object s. The class Something’s static variable count is
(C)
initially 0, then increased by 5, then increased by 1.
The code segment creates a Something object s. After executing the code segment, the object s has a
(D)
count value of 1.
The code segment creates a Something object s. After executing the code segment, the object s has a
(E)
count value of 5.

AP Computer Science A Page 19 of 25


Test Booklet

Unit 5 MCQ 1

17. Consider the following class definition.

public class Tester


{
private int num1;
private int num2;
/* missing constructor */
}

The following statement appears in a method in a class other than Tester. It is intended to create a new
Tester object t with its attributes set to 10 and 20.

Tester t = new Tester(10, 20);

Which of the following can be used to replace /* missing constructor */ so that the object t is correctly
created?
public Tester(int first, int second)
{
(A) num1 = first;
num2 = second;
}
public Tester(int first, int second)
{
(B) num1 = 1;
num2 = 2;
}
public Tester(int first, int second)
{
(C) first = 1;
second = 2;
}
public Tester(int first, int second)
{
(D) first = 10;
second = 20;
}
public Tester(int first, int second)
{
(E) first = num1;
second = num2;
}

Page 20 of 25 AP Computer Science A


Test Booklet

Unit 5 MCQ 1

18. Consider the following class definitions.

public class MenuItem


{
private double price;
public MenuItem(double p)
{
price = p;
}
public double getPrice()
{
return price;
}
public void makeItAMeal()
{
Combo meal = new Combo(this);
price = meal.getComboPrice();
}
}
public class Combo
{
private double comboPrice;
public Combo(MenuItem item)
{
comboPrice = item.getPrice() + 1.5;
}
public double getComboPrice()
{
return comboPrice;
}
}

The following code segment appears in a class other than MenuItem or Combo.

MenuItem one = new MenuItem(5.0);


one.makeItAMeal();
System.out.println(one.getPrice());

What, if anything, is printed as a result of executing the code segment?


(A) 1.5
(B) 5.0
(C) 6.5
(D) 8.0
(E) Nothing is printed because the code will not compile.

AP Computer Science A Page 21 of 25


Test Booklet

Unit 5 MCQ 1

19. Consider the following class definitions.

public class Class1


{
private int val1;
public Class1()
{
val1 = 1;
}
public void init ()
{
Class2 c2 = new Class2();
c2.init(this, val1);
}
public void update(int x)
{
val1 -= x;
}
public int getVal()
{
return val1;
}
}
public class Class2
{
private int val2;
public Class2()
{
val2 = 2;
}
public void init(Class1 c, int y)
{
c.update(val2 + y);
}
}

The following code segment appears in a method in a class other than Class1 or Class2.

Class1 c = new Class1();


c.init();
System.out.println(c.getVal());

What, if anything, is printed as a result of executing the code segment?

Page 22 of 25 AP Computer Science A


Test Booklet

Unit 5 MCQ 1

(A) 2
(B) 1
(C) 0
(D) -2
(E) Nothing is printed because the code segment does not compile.

AP Computer Science A Page 23 of 25


Test Booklet

Unit 5 MCQ 1

20. Consider the BankAccount class below.

public class BankAccount


{
private final String ACCOUNT_NUMBER;
private double balance;
public BankAccount(String acctNumber, double beginningBalance)
{
ACCOUNT_NUMBER = acctNumber;
balance = beginningBalance;
}
public boolean withdraw(double withdrawAmount)
{
/* missing code */
}
}

The class contains the withdraw method, which is intended to update the instance variable balance under
certain conditions and return a value indicating whether the withdrawal was successful. If subtracting
withdrawAmount from balance would lead to a negative balance, balance is unchanged and the
withdrawal is considered unsuccessful. Otherwise, balance is decreased by withdrawAmount and the
withdrawal is considered successful.

Which of the following code segments can replace /* missing code */ to ensure that the withdraw method
works as intended?

I.
if (withdrawAmount > balance)
{
return "Overdraft";
}
else
{
balance -= withdrawAmount;
return true;
}

II.
if (withdrawAmount > balance)
{
return false;
}
else
{
balance -= withdrawAmount;
return balance;
}

Page 24 of 25 AP Computer Science A


Test Booklet

Unit 5 MCQ 1

III.
if (withdrawAmount > balance)
{
return false;
}
else
{
balance -= withdrawAmount;
return true;
}
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III

AP Computer Science A Page 25 of 25

You might also like