- (20 points) In the following, assume that
r is an instance of the java.util.Random class, n is an integer with value 7, and s is a string with value "Hello".
- Give the value of the expression
!Character.isUpperCase('c')
- Give the value of the expression
n > 3 && n < 5
- Give the possible values of the expression
r.nextInt(3);
- Give the value of
s.equalsIgnoreCase("hello").
- (10 points) What will the following loop print if the
int x has value 44 when the loop is entered?
while (x > 0) {
x = x/2;
System.out.print(x + " "); }
- (10 points) What will the value of the
String s be at the end of the loop below if it has value "X" at the beginning of the loop?
for (char c = 'a'; c <= 'c'; c++)
s = s + c;
- (10 points)
- What instruction(s) will import the
Vector class from the java.util package?
- Give one way of invoking the zero-argument constuctor of this class if the class is not imported.
- Give two ways of invoking this constructor if the class is imported.
In the last two cases, you don't have to say what is done with the value returned by the constructor.
- (10 points) What instruction(s) will assign
y the value 0.0 if x is negative, and the square root of x if y is nonnegative? Assume that x and y are of type double.
- (10 points) The code below will not execute as suggested by the indentation -- that an incorrect password is always handled, while an insufficient balance is handled if (and only if) the password is correct.
- Why will the code not execute as suggested by the indentation?
- Fix the code so that it does execute as suggested by the indentation. Do not change the indentation. Do not worry about an insufficient balance if the password is incorrect.
if (isPasswordCorrect())
if (balance < amount)
handleInsufficientBalance();
else
handleIncorrectPassword();
- (10 points) Write a method for the
BankAccount class of the text that will deduct a service charge from the account if its balance is below a certain threshold. Assume that the amount of the service charge is given by the value of the instance field SERVICE_CHARGE and that the value of the threshold is given by the value of the instance field SERVICE_CHARGE_THRESHOLD. Don't worry about the case where the service charge is larger than the balance.
- (10 points) Define a static method that will take a
String and return the String that would be obtained by replacing all of the vowels by asterisks. You may assume that there is a static method isVowel(char) that returns true if and only if its input character is a vowel.
- (10 points) Why should the
== operator normally not be used to compare strings?