1) When you have a series of && or || operations stop evaluation early once the result is certain. (eg: false && anything will stop evaluating before it sees the other parts of the expression) A) compound boolean expression B) assertion C) short-circuit evaluation D) decrementation E) none of the above

C - that's the definition of short circuit evaluation

2) The numeric value of  17 / 5 is A) 3 B) 0 C) 12 D) 2 E) none of the above

A - 17/5 is 3 with some change, but since 17 and 5 are ints, the result of dividing them is also an int, so we truncate everything after 3

3) Assume that read() is a method that obtains a single number that the user enters, and that the user chooses to enter the numbers 3 2 1 0 -1 -2 -3 in that order. What number does this coding print?


double sum = 0;

for (double x = read(); x != 0; x = read())

sum += x;

System.out.println (sum);


A) 6 B) 5 C) 3 D) 0 E) none of the above

A - This will continue to read numbers until we hit 0 as a number, and when we hit 0, we've read in 3, 2, 1 and 0.  The sum of those is 6.

4) The BankAccount class has an instance variable named balance. A method in another class has the two statements BankAccount mine; mine.set(); Which of the following statements could be written inside that set method to assign 12 to mine's balance? A) int balance = 12; B) balance = 12; C) this.balance = 12; D) A & C E) B & C

E - A will reference a balance that is NOT the instance variable balance, so it's right out (by declaring a new one you make it impossible to access the instance variable), B is correct as if you refer to balance in the BankAccount class without anything declaring a new balance, you're referring to the instance variable, C is corect since this refers to the current instanace of your class, and thus the correct balance, ad D is not correct because A is not correct.

5) Which of the following segments of code outputs 0? A) System.out.println(“Hello”.length-5); B) System.out.println(“Hello”.size()-5); C) System.out.println(“Hello”.length()-”5”); D) System.out.println(“Hello”-”Hello”); E) none of the above

E - Remember, to get the length of a String, you need to call the method .length().  A correct answer would be System.out.println("Hello".lenght()-5);

6) Assuming you have a properly defined Random object called fred, which of the following code segments will return a Random value between 10 and 20 inclusive? A) fred.nextInt(10, 20); B) fred.nextInt(20)-10; C) fred.nextInt(11)+10; D) fred.nextInt(200); E) none of the above

C - fred.nextInt(11) will create a random value between 0 and 10, and if you add 10 to that it becomes between 10 and 20 inclusive.

7) What is the value of x printed by this coding?

int x = 6;

do {

x += 3;

}while (x % 2 == 1);

System.out.println (x);


A) 3 B) 6 C) 9 D) 12 E) none of the above


D - when it starts x is 6, and then you add 3 which makes it 9, and 9 % 2 does == 1, so then it becomes 12, and 12 % 2 == 0, so it stops the loop, and at that point x is 12.

8) The Child class has the heading "class Child extends Parent". Choose the option that fills in the blank so that calling the Child constructor begins by calling the constructor in the Parent class that also has the same double parameter.


public Child (double data) { _______________ (data) ; spot = 17; }

A) this B) constructor C) super.constructor D) super E) none of the above

D - this refers to the current instance, so it's defintly not what you want, constructor isn't a keyword in java, so both of those answers is wrong, but super does refer to the superclass, and if you make a call simply saying super() or super with a paremeter betweeen the parethesis, you ARE calling the constructor of the super class.

9) The Child class has the heading "class Child extends Parent". A method named stuff is in the Child class. Which of the following variables in Parent can the coding within stuff refer to? A) a private variable declared outside of all methods B) a public variable declared outside of all methods C) a parameter of some other method D) B & C E) none of the above

B - you cannot see private variables of classes you extend so A is wrong, you can access public instance variables in extended classes, so B is correct, and you can't access variables from other methods be they paremeters or not so C is wrong.

10) 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


E - Actually D nearly works, but because parenthesis are not around it, it will actually make the statement grossPay = 40 * ratePerHour + hoursWorked - 40 * ratePerHour * 1.5.  Since * has higher precedence than - it will mean that it will subtrack off 40 * ratePerHour * 1.5 off of hoursWorked.  With parenthesis surrounding option D it would work since those have higher priority than the other operators. 

11) 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

B - You want to access the 3rd item, so you go for the 3rd item, and since indices start counting at 0, that means you want index 2.

12) 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

A - this is the only one that both accesses the auto array, and properly gets the correct index of a car out of it.

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

B - not much to say

14) 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

D - A nearly works, but you don't extend interfaces, you implement them

15) 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

D - Java won't let you compile a class that implements Comparable unless you have that method.

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

B, C or E - B works since Java will copy a subclass into a superclass reference without a cast, C works since it certainly doesn't mind a cast even if it doesn't need it, and E is correct because there was only supposed to be one correct answer, and thus it was invalid.

17) 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

C - by default (int) means truncate the value down to the lower int.  So 2.00 - 2.99 will round down to 2.  If we add .5 to that we make it 2.5 to 3.49, which when trunctated will have everything smaller than 3 drop down to to (those were originally 2.00 through 2.49) and everything above go to 3.  That's exactly the rounding to the nearest int.

18) Given a BufferedReader called foo, which of the following will return true if the end of the file is reached?  A) foo.endOfFile() B) foo == null C) (foo.readLine()==null) D) (foo.eof() == true) E) none of the above

C - by definition readLine() returns null when it hits the end of the file

19) At the end of the first pass of the inner loop of the SelectionSort as defined below, what can be said for sure?

public static void selectionSort(int[] data) {

for (int a = data.length-1; a > 1; a--) {

for (int b = 0; b < a; b++) {

if (data[a] > data[b]) {

int temp = data[b];

data[b] = data[a];

data[a] = temp;}}}}

A) all the data is sorted B) the largest value is at the last position in the array C) the smallest value is at the last position in the array D) A & B E) no conclusions can be made

C - by definition of selection sort you know at the end of every pass it makes sure the last value of the array that it looks at is the largest value


20) In order to find data from an unordered array, which search algorithm will be the quickest to find the data? A) linear search B) binary search C) bubble sort D) quick sort E) merge sort

A - if it's unordered you can't use binary search, and the other options are sorts, not searches

21) What is the approximate O(N) for the following code?

public int findit(int[] items, int item) {

for (int a= 0; a < items.length; a++) {

if (items[a] == item) return a;
}      

return -1;}

A) 1 B) N C) N^2 D) N*Log(N) E) N!

B - it will run through every item once in the worst case, so if you have 100 items, it will take 100 tries.  So it's O(N)

22) In the average case of each of these sorts, which of the following algorithms is the fastest for a large set of data? A) QuickSort B) InsertionSort C) BubbleSort D) SelectionSort E) they all should be about the same

A - On average QuickSort wll take N * log(N) tries to sort the array, the others on average will take some fraction of N^2

23) What method built into the ArrayList class returns the number of elements stored in the ArrayList? A) length B) length() C) size D) size() E) getSize()

D - there is nothing else to say

24) What is a valid limitation of an ArrayList? A) you cannot store Objects in an ArrayList B) you can not store more than 100 elements in an ArrayList C) you cannot change the size of an ArrayList once it is created D) you cannot store int values directly in the ArrayList E) none of the above

D - you can store Objects in ArrayLists, and only Objects or other instances of classes that extend Objects.  int values are NOT Objects as they are a basic data type.  To store those in an ArrayList you must first place them into a wrapper class object.

25) What method in the Integer class allows you to return the int?

A) parseInt(String s) B) getInt() C) intValue() D) toString() E) none of the above

C - there is nothing else to say

26) An int is actually created when A) it is declared B) when the computer runs the new int() line C) only when an Integer object is created D) A & B E) A & C

A - int is not a class, so you never need to say new int() to create one. 

27) If an array has 100 elements, and it is sorted by a SelectionSort algorithm, which of the following is likely to be closest to the number of steps required to sort the code? A) 100 B) 50 C) 1 D) 10000 E) none of the above

A, D or E - Technically the number of steps will be around 5000 which is closer to 100 since it takes N^2/ 2 tries... but you should focus on the N^2 part.  Since both are correct, I'll also accept E since the question somewhat falls apart.

28) If one has a recursive algorithm that is missing a base case, what is likely to result? (choose the best answer) A) an invalid calculation B) recursive algorithms do not require base cases C) a compiler error D) infinite recursion (leading to a Stack Overflow) E) none of the above

D - it will be infinite recursion in theory, but in reality computer's memory would overflow before that becomes a problem and give you an error known as a Stack Overflow.  The main point is that it can't end in any friendly ways.

29) What is a pre-condition for performing a binary search through elements of an ArrayList. A) the ArrayList must contain only int values B) the ArrayList must have fewer than 1000 elements C) the ArrayList must be sorted in ascending order D) ArrayList itself must implement Comparable E) none of the above

C - Any ordering would work, but if it's in ascending order, it can definetly be searched using a binary search.

30) “Ambition is like a frog sitting on a Venus Flytrap. The flytrap can bite and bite, but it won't bother the frog because it only has little tiny plant teeth. But some other stuff could happen and it could be like ambition.A) True B) False C) What??? D) Ah, I get it! E) none of the above

I will leave this to you.