What interface is a class required to implement in order to sort object's of that class (using Collections.sort())?
Comparable
Name 2 types of loops built into Java.
"for" loop, "while" loop, and "do while" loop
What is the purpose of a base case in recursion?
To provide a condition that will terminate the chain of recursive calls.
What is the difference between a stack and a queue?
A stack uses "first-in last-out", while a queue uses "first-in first-out".
"The cake is a lie" is from what video game?
Portal
What are accessor methods and mutator methods?
Accessor methods give a user ACCESS to an object's state. Mutator methods allow a user to CHANGE an object's state.
Solve the following expression:
4 * 2 + 8 / 3 - (8 - 7 % 2)
3
In the worst case, using linear search, how many elements of a list would you have to search to find a certain element?
All of the elements in the list
Which has faster access to elements? A HashMap or a TreeMap?
HashMap
How many bits are in a byte?
8
How do you make a class variable (as opposed to an instance variable)?
Make a field in the class with the "static" keyword.
What type of error will be produced by the following:
int[] a = new int[4];
System.out.println(a[4]);
IndexOutOfBoundsException
What requirement must be met in order to use binary search on a list?
The list must be sorted.
How are linked-list elements stored?
Each element has its own node, and each node points to another node (except the last node) to make a chain of nodes.
Who originally developed the Java language?
James Gosling
Fill in the blank:
A ___________ class accepts a data type as a parameter when it is created, like ArrayList<String>().
generic
What is the purpose of the Java compiler?
To convert source code (what the programmer writes) into machine code (that your CPU can interpret).
Describe how binary search works.
The median element of a sorted collection is compared to the target value. If the target is less than the median value, repeat for the bottom half of the collection. If the target is greater than the median value, repeat for the upper half of the collection.
Why can an ArrayList can change size?
An ArrayList uses an array to store data, and when that array gets full, all of its elements are copied over to a new, larger array.
What is Big O notation?
A tool used to measure asymptotic growth of a function relative to its input size.
What is the difference between an abstract class and an interface?
You would use an abstract class when you want certain fields/methods defined already, but not all of them. Also, abstract classes can be extended, so they are used when they share an “is-a” relationship with something that can be instantiated, but it doesn’t make sense to instantiate it itself. For example, a triangle is-a shape, but it wouldn’t make sense to instantiate a shape. What does a shape look like?
You would implement an interface when certain method(s) must be implemented, but it is up to the programmer to decide how it is implemented.
Name a situation where the garbage collector is used in Java.
Many answers, but one example is whenever an object has no reference variables pointing to it.
What is the output produced by the following code?
public class Demo {
public static long fib(int n) {
if ((n == 0) || (n == 1))
return n;
else
return fib(n - 1) + fib(n - 2);
}
public static void main(String[] args) {
System.out.println(fib(8));
}
}
21
How could you store a HashMap<String, Integer>, a TreeMap<String, double>, and an ArrayList<Integer> all in one List?
Make the List of type Collection. That is what all of those object's would have in common. This is an example of polymorphism.
Describe how "hashing" works.
An algorithm is applied to an input and an integer is generated. This integer isn't necessarily unique, but good hash functions will make it rare that two unique inputs will share an integer output.