1) Choose the option to fill in the blank so that this code computes the balance at the end of 10 years when you put $1000 in a savings account at the end of each year and earn 5% interest annually. Use only one arithmetic operator in your answer.
int k = 1;
double balance = 1000;
while (k <= 10)
{ k = k + 1;
balance = ________________ + 1000;
}
System.out.println ("saving $1000 per year gives $" + balance + " after 10 years.");
A) balance*= 1.05; B) balance += .05 C) balance++ D) balance * 1.05 E) none of the above
2) The following code prints how many numbers?
for (int x = 500; x > 1; x /= 2)
System.out.println (x);
A) 1 B) 20 C) 8 D) 16 E) none of the above
3) Select the option that fills in the blank such that the code computes the gross pay that a person receives when she gets time-and-a-half for all hours over 40 worked in a week.
if (hoursWorked <= 40)
grossPay = hoursWorked * ratePerHour;
else
grossPay = 40 * ratePerHour + ________________ * ratePerHour * 1.5;
A) (hoursWorked > 40) B) (hoursWorked * 40) C) hoursWorked * 1.5 D) hoursWorked – 40 E) none of the above
4) The first number that this code prints is:
for (x = 1; x < 100; x *= 2);
System.out.println (x);
System.out.println (24);
A) 1 B) 24 C) 128 D) 2 E) none of the above
5) Which of the following options fills in the blank such that this statement rounds a given positive double value to the nearest integer value.\
int rounded = ________________ (given + 0.5);
A) math.Roundit B) (Integer) C) (int) D) 1 * E) none of the above
6) Complete this for-loop to count all capital letters in the first 10 characters of a String named s.
for (int k = 0; k < 10 ; k++)
if (s.charAt (k) <= ________________ && s.charAt (k) >= ________________ )
count++;
A) 'A' and 'Z' in that order B) 'Z' and 'A' in that order C) A and Z in that order D) Z and A in that order E) none of the above
7) Complete this coding so that it prints the number 20.
double [ ] d = {30, 25, 20, 15};
System.out.println ( ________________ );
A) d[1] B) d[2] C) d[3] D) d[4] E) none of the above
8) Complete this coding to create a new array of doubles containing 100 values.
double [ ] item = ________________ ;
A) new item[100]; B) new double[100]; C) new [100]; D) new double(100); E) none of the above
9) Complete this coding so that it doubles five of the numbers stored in an array.
for (int k = 3; k <= 7; k++)
item[k] = ________________ ;
A) item[k]++; B) item[k] * item[k]; C) 2 * k; D) item[k] * 2; E) none of the above
10) Complete this coding so that it prints the sum of five of the numbers stored in an array.
double sum = 0;
for (int k = 3; k <= 7; k++) ________________ ;
System.out.println (sum);
A) sum = k; B) sum = array[k]; C) sum = sum + array[5]; D) sum = sum + array[k]; E) none of the above
11) The Car class has a method with the heading "public double getMiles()" that tells the number of miles that a particular Car object has been driven. We create an array of Cars using auto = new Car[10]. Complete this coding to add up the miles driven by all the Cars in the array.
double sum = 0;
for (int k = 0; k < 10; k++)
sum += ________________ ;
A) auto[k].getMiles() B) auto.getMiles() C) k.getMiles() D) [k].getMiles(); E) none of the above
12) Given the following code, what Exception type is likely to be generated by this method?
public static int atoi(String s) {
return Integer.parseInt(s);
}
A) ClassCastException B) NullReferenceException C) NumberFormatException D) B & C E) B only
13) Assume there is a method called randomThing() that can generate exceptions. You need to call, it but if there is an exception generated you need to make sure the program does not stop running. What keyword declares the clause you should you surround the call to randomThing() with?
A) catch B) try C) on error goto D) error E) none of the above
14) Assuming you're in a section of code where you are handling an Exception, and you have a variable of type Exception called Frodo. Which of the following lines will print to the user what the nature of that Exception was?
A) System.out.println(Frodo); B) System.out.println(Integer.parseInt(Frodo)); C) System.out.println(Frodo.getError()); D) System.out.println(Frodo.getMessage()); E) none of the above
15) If a method is declared in an interface
A) all classes that extend that interface must provide code for that method B) all subclasses of that interface inherit the method C) all classes that implement that interface inherit the method D) all classes that implement that interface must provide code for that method E) none of the above
16) Assume you have an interface called Jeep and a variable of that type called Jerry, and you have a variable of a type of a class that implements that Jeep called Tom. How would you assign Tom to Jerry?
A) you can't B) Jerry = Tom; C) Jerry = (Jeep) Tom; D) Jerry = Tom.parseJeep(); E) none of the above
17) You have a variable of a class that you know nothing about, except that it implements Comparable. What method can you safely call on that variable?
A) toString() B) parseInt() C) compareTo() D) compareTo(Object o); E) none of the above
18) Which of the following describes a difference between an interface and an abstract class?
A) an interface can include actual code, while an abstract class cannot B) you cannot create a variable of the type of an interface, while you can for an abstract class C) you cannot include any code in an interface, while you may for an abstract class D) you do not inherit methods from an abstract class, while you do for an interface E) none of the above