Abstraction
Inheritance
Recursion, Searching, and Sorting
Collections
Problem Solving
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

Write a method that takes in a String as a parameter, returns void, and prints each character of the string on its own line – in reverse, so the last character comes first and so forth.

Iterate through the word from the back and use the charAt method.
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

Write two loops that print the powers of two until the value exceeds 100 – one using a for loop and the other using a while loop. After implementing both, explain when a for loop is more appropriate and when a while loop is the better choice.

For loops are typically used when the number of iterations is known.

While loops are typically used when the number of iterations depends on some condition such as reaching 100 in this case.

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

Write a method wordCount to solve this classic word-count algorithm: 

Given an array of Strings, return a Map<String, Integer> with a key for each different String, with the value the number of times that String appears in the array. Make sure to set the words to lowercase to avoid having to keys for one word


Convert each word to lowercase to avoid case-sensitive duplicates (e.g., "Apple" and "apple" should be considered the same).

Traverse the array and check if each word is already a key in the map.

If the word is already in the map, increment the value (the count of occurrences).

If the word is not in the map, add it with a value of 1.

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

Write a method maxCount that takes in a sorted integer ArrayList and returns the number of occurrences of the most frequently occurring value in a sorted ArrayList of integers.

Have a variable to store the max count and another variable to keep track of the current count as you traverse the list.

Start with the first element and compare it to the next. If they are equal, increment the current count.

If the current element is not equal to the next one, compare the current count to the max count and update the max count if necessary. Then, reset the current count to 1, since you're now counting a new element.

After the loop ends, check the last current count in case the most frequent element was at the end of the list.

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

Consider the following statements:

public class Vehicle {...}

public class Car extends Vehicle {...}

public class SUV extends Car {...}

Which of the following are legal statements? If a statement is illegal, explain why it is. Circle them.

a. Car c = new SUV();

b. SUV s = new Car();

c. SUV s = new SUV();

d. Vehicle v = new SUV();

e. Vehicle v = new Car();

f. Car c = new Vehicle();

a. Car c = new SUV();

c. SUV s = new SUV();

d. Vehicle v = new SUV();

e. Vehicle v = new Car();


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

Write a recursive method called isPalindrome that takes a string as a parameter and returns true if the string is a palindrome and false if it is not. A palindrome is a string like "racecar" that has the same sequence of characters when written forwards and backwards. Notice that in a palindrome, the first and last characters match, as do the second and second-to-last, the third and third-to-last, and so on.

  • Check if the first and last characters match:
    • If they do match, recursively call the method again with the substring that excludes the first and last characters.
  • Base case:
    • If the string has one or no characters left, it is a palindrome by definition.
  • If at any point the first and last characters don't match, return false because it cannot be a palindrome.
M
e
n
u