Professional Documents
Culture Documents
Midterm Review Answers
Midterm Review Answers
Multiple Choice
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
int i = Integer.MAX_VALUE + 1;
a. compile-time error
b. NumberOverflowException
c. StackOverflowException
d. NumberFormatException
e. the value of i will silently overflow
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
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
What would be the output if the user entered the words "dog", "cat", "bear",
"goat", and "fish"?
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?
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?
a. I
b. II
c. III
d. IV
e. V
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.
a. I only
b. II only
c. I and II only
d. II and III only
e. I, II, and III
Page 3 of 11
AP Computer Science Midterm Review
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;
}
}
I. interestRate = rate;
super(initialBalance);
Page 4 of 11
AP Computer Science Midterm Review
deposit(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
String s = "\"\'\\\n\t\'\"";
System.out.println(s.length());
a. 14
b. 9
c. 7
d. 5
e. compile-time error
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
Page 5 of 11
AP Computer Science Midterm Review
a. true
b. false
c. unknown
d. run-time error
e. compile-time error
a. toString
b. hashCode
c. clone
d. compareTo
e. equals
Page 6 of 11
AP Computer Science Midterm Review
a. ABCE
b. ABDE
c. ABDCE
d. infinite recursion
e. compile-time error
19. Which of the following will correctly declare and initialize an array of ints that
stores the numbers 1 to 10?
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?
a. I only
b. I and II only
c. I and III only
d. II and III only
e. I, II, and III
I. Object num = 5;
System.out.println(num);
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.
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.
Page 9 of 11
AP Computer Science Midterm Review
if (min == 60)
{
hr++;
min = 0;
if (hr == 13)
{
hr = 1;
}
}
}
}
Page 10 of 11
AP Computer Science Midterm Review
}
public void displayTime()
{
String hr, min, sec;
Page 11 of 11