Abstraction
Programming Fundamentals
Recursion, Searching, and Sorting
Collections
Grab Bag
100

What interface is a class required to implement in order to sort object's of that class (using Collections.sort())?

Comparable

100

Name 2 types of loops built into Java.

"for" loop, "while" loop, and "do while" loop

100

What is the purpose of a base case in recursion?

To provide a condition that will terminate the chain of recursive calls.

100

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".

100

"The cake is a lie" is from what video game?

Portal

200

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.

200

Solve the following expression:

4 * 2 + 8 / 3 - (8 - 7 % 2)

3

200

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

200

Which has faster access to elements? A HashMap or a TreeMap?

HashMap

200

How many bits are in a byte?

8

300

How do you make a class variable (as opposed to an instance variable)?

Make a field in the class with the "static" keyword.

300

What type of error will be produced by the following:

int[] a = new int[4];

System.out.println(a[4]);

IndexOutOfBoundsException

300

What requirement must be met in order to use binary search on a list?

The list must be sorted.

300

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.

300

Who originally developed the Java language?

James Gosling

400

Fill in the blank:

A ___________ class accepts a data type as a parameter when it is created, like ArrayList<String>().

generic

400

What is the purpose of the Java compiler?

To convert source code (what the programmer writes) into machine code (that your CPU can interpret).

400

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.

400

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.

400

What is Big O notation?

A tool used to measure asymptotic growth of a function relative to its input size.

500

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.

500

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.

500

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

500

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.

500

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.