Вы находитесь на странице: 1из 11

AP Computer Science Midterm Review

Multiple Choice

1. Random-access memory is a term for


a. primary memory
b. secondary memory
c. tertiary memory
d. a removable storage device
e. the central processing unit

2. A program in which you can write and test your programs is called
a. a compiler
b. an integrated development environment
c. an application programming interface
d. a shell window
e. a compile-execute application

3. What will result from the following code segment?

int i = Integer.MAX_VALUE + 1;

a. compile-time error
b. NumberOverflowException
c. StackOverflowException
d. NumberFormatException
e. the value of i will silently overflow

4. What is the output of the following code segment?

String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2);

a. true
b. false
c. unknown
d. compile-time error
e. run-time error

5. What is the output of the following code segment?

String s1 = "Hello";
String s2 = new String("Hello");
System.out.println(s1 == s2);

a. true
b. false

Page 1 of 11
AP Computer Science Midterm Review

c. unknown
d. compile-time error
e. run-time error

6. Consider the following code segment.

ArrayList<String> words = new ArrayList<String>();

//code to have user fill ArrayList

String first = words.get(0);

for (int i = 1; i < words.size(); i++)


{
if (words.get(i).compareTo(first) < 0)
words.remove(i);
}

for (String s : words)


System.out.print(s + " ");

What would be the output if the user entered the words "dog", "cat", "bear",
"goat", and "fish"?

a. dog goat fish


b. dog bear goat fish
c. dog cat bear goat fish
d. compile-time error
e. run-time error

7. Which of the following methods will correctly return the value of ab, given that
they are both non-negative ints and that ab Integer.MAX_VALUE?

I. public int power(int a, int b)


{
return (int) Math.pow(a, b);
}

II. public int power(int a, int b)


{
int product = 0;
for (int i = 0; i < b; i++)
product *= a;
return product;
}
III. public int power(int a, int b)
{
return (int) Math.round(Math.exp(b * Math.log(a)));
}

a. I only

Page 2 of 11
AP Computer Science Midterm Review

b. I and II only
c. I, II, and III
d. I and III only
e. none

8. Imagine you are creating the abstract class Shape. Which of the following would
be the best candidate to make an abstract method?

I. getNumSides() //returns the number of sides of the


//Shape

II. getName() //returns the Shapes name

III. getArea() //returns the Shapes area

IV. getLocation() //returns the coordinates of the Shapes


//top-left corner

V. getRegAngle()//returns the angle measure of a regular


//version of this Shape (circle,
//equilateral triangle, square, etc.)

a. I
b. II
c. III
d. IV
e. V

9. You should consider using an interface instead of an abstract class when

I. The subclass will need to extend a concrete class to which the abstract class
has no relation.

II. You are only using constants and abstract methods in the abstract class.

III. You want to express the commonality between classes.

a. I only
b. II only
c. I and II only
d. II and III only
e. I, II, and III

For questions 10 and 11 consider the following classes.

Page 3 of 11
AP Computer Science Midterm Review

public class BankAccount


{
private double balance;

public BankAccount()
{
balance = 0;
}
public BankAcccount(double initialBalance)
{
balance = initialBalance;
}
public void deposit(double amount)
{
balance += amount;
}
public void withdraw(double amount)
{
balance -= amount;
}
public double getBalance()
{
return balance;
}
public String toString()
{
return "balance: $" + balance;
}
}

public class SavingsAccount extends BankAccount


{
private double interestRate;

public SavingsAccount(double initialBalance, double rate)


{
//code to construct a SavingsAccount
}
public void addInterest()
{ ... }
public String toString()
{
//code to return a String representation
}
}

10. Which of the following could correctly replace //code to construct a


SavingsAccount?

I. interestRate = rate;
super(initialBalance);

II. interestRate = rate;

Page 4 of 11
AP Computer Science Midterm Review

deposit(initialBalance);

III. interestRate = rate;


balance = initialBalance;

a. I only
b. II only
c. III only
d. I and II only
e. I, II, and III

11. Which of the following could correctly replace //code to return a String
representation?

I. return "balance: $" + balance + ", interest rate: " + (interestRate * 100) +
"%";

II. return toString() + ", interest rate: " + (interestRate * 100) + "%";

III. return super.toString() + ", interest rate: " + (interestRate * 100) + "%";

a. III only
b. I and III only
c. II and III only
d. I, II, and III
e. none

12. What is the output of the following code segment?

String s = "\"\'\\\n\t\'\"";
System.out.println(s.length());

a. 14
b. 9
c. 7
d. 5
e. compile-time error

13. Static binding is to dynamic binding as

a. overriding is to overloading
b. overloading is to overriding
c. class method is to instance method
d. final is to abstract
e. this is to super

14. What is the output of the following code segment?

System.out.println(true || true && false);

Page 5 of 11
AP Computer Science Midterm Review

a. true
b. false
c. unknown
d. run-time error
e. compile-time error

15. The String class is final, which means that

a. all Strings are immutable


b. you cannot make a subclass of String
c. you can use String literals
d. it contains at least one final method
e. it has no superclass

16. Which of the following is not a method of the Object class?

a. toString
b. hashCode
c. clone
d. compareTo
e. equals

17. Consider the following classes.

public class ClassA


{
public void methodA()
{
System.out.print("A");
methodB();
}
public void methodB()
{
System.out.print("B");
}
}

public class ClassB extends ClassA


{
public void methodA()
{
super.methodA();
System.out.print("C");
}
public void methodB()
{
super.methodB();
System.out.print("D");
}
}

Page 6 of 11
AP Computer Science Midterm Review

public class ClassC extends ClassB


{
public void methodA()
{
super.methodA();
System.out.print("E");
}
}

What is the output of the following code segment?

ClassC c = new ClassC();


c.methodA();

a. ABCE
b. ABDE
c. ABDCE
d. infinite recursion
e. compile-time error

18. Which of the following statements about default values is false?

a. If you do not supply a constructor for a class, it automatically gets a


default constructor.
b. If you do not explicitly supply a superclass, then the class extends Object.
c. If you do not supply a call to the superclass constructor inside the subclass
constructor, a call to the default superclass constructor is made, if there is
one.
d. If you declare a local variable, but do not explicitly initialize it, its value is
set to 0 (for primitive types) or null (for objects).
e. All methods of an interface are automatically public abstract, and all
fields are automatically public static final.

19. Which of the following will correctly declare and initialize an array of ints that
stores the numbers 1 to 10?

I. int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

II. int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

III. int[] nums = new int[10];


for (int i = 0; i < nums.length; i++)
nums[i] = i;

Page 7 of 11
AP Computer Science Midterm Review

a. I only
b. III only
c. I and II only
d. I and III only
e. I, II, and III

20. Which of the following will correctly resize all the Rectangles in the ArrayList
quads?

I. for (int i = 0; i < quads.size(); i++)


quads.get(i).setSize(100, 200);

II. for (Rectangle r : quads)


r.setSize(100, 200);

III. for (Rectangle r : quads)


r = new Rectangle((int) r.getX(), (int) r.getY(), 100, 200);

a. I only
b. I and II only
c. I and III only
d. II and III only
e. I, II, and III

21. Which of the following code segments contains an error?

I. Object num = 5;
System.out.println(num);

II. Object str = "Hello";


System.out.println(str.equals("Hello"));

III. Object arr = new int[5];


System.out.println(arr);

a. I only
b. II only
c. I and III only
d. I, II, and III
e. none

Free Response

1. Write the Clock class. A Clock has an hour, a minute, and a second, all of which
are integer values. There should be one constructor that requires these values, and
accessor methods that return each value. The Clock class should also have an

Page 8 of 11
AP Computer Science Midterm Review

increment method, which increases the Clocks time by one second. All Clocks
have a displayTime method, which prints out the Clocks time. However, all
Clocks display their time differently.

Before increment After increment


hour: 5, minutes: 27, seconds: 39 hour: 5, minutes: 27, seconds: 40
hour: 11, minutes: 32, seconds: 59 hour: 11, minutes: 33, seconds: 0
hour: 12, minutes: 59, seconds: 59 hour: 1, minutes: 0, seconds: 0

2. Write the DigitalClock class. A DigitalClock is a Clock that displays its time as
hh:mm:ss, where hh represents the hour, mm represents the minutes, and ss
represents the seconds. There should be two digits in each section.

Time displayTime Output


hour: 11, minutes: 22, seconds: 13 11:22:13
hour: 6, minutes: 9, seconds: 1 06:09:01
hour: 7, minutes: 0, seconds: 0 07:00:00

Page 9 of 11
AP Computer Science Midterm Review

public abstract class Clock


{
private int hr, min, sec;

public Clock(int hour, int minute, int second)


{
hr = hour;
min = minute;
sec = second;
}

public int getHour()


{
return hr;
}

public int getMinute()


{
return min;
}

public int getSecond()


{
return sec;
}

public void increment()


{
sec++;
if (sec == 60)
{
min++;
sec = 0;

if (min == 60)
{
hr++;
min = 0;

if (hr == 13)
{
hr = 1;
}
}
}
}

public abstract void displayTime();


}

public class DigitalClock extends Clock


{
public DigitalClock(int hour, int minute, int second)
{
super(hour, minute, second);

Page 10 of 11
AP Computer Science Midterm Review

}
public void displayTime()
{
String hr, min, sec;

if (getHour() < 10)


hr = "0" + getHour();
else
hr = "" + getHour();

if (getMinute() < 10)


min = "0" + getMinute();
else
min = "" + getMinute();

if (getSecond() < 10)


sec = "0" + getSecond();
else
sec = "" + getSecond();

System.out.println(hr + ":" + min + ":" + sec);


}
}

Page 11 of 11

Вам также может понравиться