Swing
Streams, Comparators, and Optionals
2D Arrays / Recursion
100

Name four Swing components

JFrame, JPanel, JButton, JTextField, etc.

100

What is an advantage of using Optional<Object> over a nullable Object?

Valid answers:

-Forces users to account for the null case (no surprise runtime exceptions)

-Makes code more concise (replace if statement with .orElse())

100

Re-write the following function recursively:

public void countDown(int n) {
    while(n > 0) {
        System.out.println(n + "...");
        Thread.sleep(1000);
        n -= 1;
    }

}

Possible answer:

public void countDown(int n) {
        if(n == 0) {return;}
        System.out.println(n + "...");
        Thread.sleep(1000);
        countDown(n-1);
    }

200

At most, how many child components can be shown in a JFrame that uses BorderLayout?

5

200

Give an example of an intermediate Stream operation and a terminal Stream operation

Intermediate: map, filter

Terminal: collect, findAny, sum

200

What is the defining characteristic of tail recursion?

A few ways to think about it:

-The return value of every recursive call is the same

-The base case returns the final result

-The recursive call is at the very end of the function, after all the "work" is done

300

What is the Graphics object useful for?

It allows you to draw custom graphics on a component

300

What is the difference between map() and flatMap()

map returns a single object, flatMap returns a stream that is "flattened" into the current stream 

300

Write a function that takes in a 2D array, and converts it into a 1D array, going top-to-bottom and left-to-right.

public int[] flattenByColumn(int[][] arr) {}

[Answers will vary]

400

What function is called whenever a Swing component re-renders?

paintComponent(Graphics g)

400

You have created a FancyFurniture class, and you want to allow your furniture to be sorted by price (an instance variable of type double). You decide to have your class implement Comparable<FancyFurniture>. Write a compareTo function that will make your furniture's natural ordering be from cheapest to most expensive.

Something like:

public int compareTo(FancyFurniture o) {
        return this.price - o.getPrice();

}

400

Write a function that takes in two arrays, and creates a 2D array where the top row is 0 and then arr1, the leftmost column is 0 and then arr2, and every other cell is the sum of the top of its column and leftmost of its row

public int[][] sum(int[] arr1, int[] arr2) {}

[Answers will vary]